T
tat
Dear C++ experts,
I have a piece of code (see below).
Alignment object has a data member that is a vector of Sequence
objects.
Sequence object, in turn, has a data member that is a vector of Residue
objects.
Residue object has a data member called m_Number.
After initializing, I want to update data member (m_Number of Residue
object) via method setNumber(const string& number). This setNumber
method is supposed to change m_Number in the Residue object. However,
it does not (There is nothing on the third column of output). Changing
"string m_Number" to "mutable string m_Number" did not help.
Can someone explain me why?
Thanks,
tat
----------------------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
class Residue {
public:
Residue(): m_Name(""), m_Number(""), m_Pos(0) {};
~Residue() {};
void initializeResidue(const char& name1, const int& align_pos) {
m_Name = name1;
m_Pos = align_pos;
}
void setName(const string& name)
{
m_Name = name;
}
string name() const
{
return m_Name;
}
void setNumber(const string& no)
{
m_Number = no;
}
string number() const {
return m_Number;
}
void setPos(const int& no)
{
m_Pos = no;
}
int pos() const {
return m_Pos;
}
private:
string m_Name;
string m_Number;
int m_Pos;
};
class Sequence {
string m_SeqName;
string m_Sequence;
vector<Residue> m_Residues;
public:
Sequence(): m_SeqName(""), m_Sequence(""), m_Residues(0) {};
Sequence(const string& n, const string& s)
: m_SeqName(n), m_Sequence(s),m_Residues(0)
{
m_Residues.resize(m_Sequence.length());
for (int i = 0; i < int(m_Sequence.length()); i++) {
m_Residues.at(i).initializeResidue(m_Sequence.at(i),i);
}
}
~Sequence() { m_Residues.clear();};
void setSequence(const string& seq) {
m_Sequence = seq;
m_Residues.resize(int(seq.length()));
for (int i = 0; i < int(seq.length()); i++) {
m_Residues.at(i).initializeResidue(seq.at(i),i);
}
}
string sequence() const {
return m_Sequence;
}
void setSeqName(const string& seq_name)
{
m_SeqName = seq_name;
}
string seqName() const {
return m_SeqName;
}
int length() const {
return m_Sequence.length();
}
Residue residue(const int& position) {
if (position >= m_Residues.size() || position < 0) {
cerr << "Index out of range\n";
exit(1);
}
else return m_Residues.at(position);
}
vector<Residue> residues() const {
return m_Residues;
}
void addResidue(Residue& res) {
m_Residues.push_back(res);
}
Sequence& operator=(const Sequence& xxx) {
if (this != &xxx) {
m_SeqName = xxx.seqName();
m_Sequence = xxx.sequence();
m_Residues = xxx.residues();
}
return *this;
}
};
typedef vector<Sequence> VecS;
class Alignment {
Sequence m_RefSeq;
VecS m_ListOfSequences;
public:
Alignment(): m_ListOfSequences(0) {};
~Alignment() { m_ListOfSequences.clear(); };
Sequence refSeq() {
return m_RefSeq;
}
VecS listOfSeqs() const
{
return m_ListOfSequences;
}
void addSeq(Sequence& seq) {
m_ListOfSequences.push_back(seq);
}
void setRefSeq(const string& id) {
for (int i=0; i < int(m_ListOfSequences.size()); i++) {
if (m_ListOfSequences.at(i).seqName().find(id) != string::npos) {
m_RefSeq = m_ListOfSequences.at(i);
}
}
}
};
////////////////////////// MAIN ///////////////////////////////////
int main(int argc, char* argv[]) {
Alignment align;
Sequence seq1("1cje","KITVHFINRD");
Sequence seq2("Q5PPU4_XENLA","KLTVNFINRN");
Sequence seq3("Q9VZE1_DROME",".VNITFVRAN");
align.addSeq(seq1);align.addSeq(seq2);align.addSeq(seq3);
align.setRefSeq("1cje");
cout << align.refSeq().seqName() << "\t" << align.refSeq().sequence()
<< endl;
for (int i=0; i < align.refSeq().residues().size(); i++) {
cout << align.refSeq().residue(i).name() << "\t";
cout << align.refSeq().residue(i).pos() << "\t";
align.refSeq().residue(i).setNumber("pp");
cout << align.refSeq().residue(i).number() << "\n";
}
return 0;
}
I have a piece of code (see below).
Alignment object has a data member that is a vector of Sequence
objects.
Sequence object, in turn, has a data member that is a vector of Residue
objects.
Residue object has a data member called m_Number.
After initializing, I want to update data member (m_Number of Residue
object) via method setNumber(const string& number). This setNumber
method is supposed to change m_Number in the Residue object. However,
it does not (There is nothing on the third column of output). Changing
"string m_Number" to "mutable string m_Number" did not help.
Can someone explain me why?
Thanks,
tat
----------------------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
class Residue {
public:
Residue(): m_Name(""), m_Number(""), m_Pos(0) {};
~Residue() {};
void initializeResidue(const char& name1, const int& align_pos) {
m_Name = name1;
m_Pos = align_pos;
}
void setName(const string& name)
{
m_Name = name;
}
string name() const
{
return m_Name;
}
void setNumber(const string& no)
{
m_Number = no;
}
string number() const {
return m_Number;
}
void setPos(const int& no)
{
m_Pos = no;
}
int pos() const {
return m_Pos;
}
private:
string m_Name;
string m_Number;
int m_Pos;
};
class Sequence {
string m_SeqName;
string m_Sequence;
vector<Residue> m_Residues;
public:
Sequence(): m_SeqName(""), m_Sequence(""), m_Residues(0) {};
Sequence(const string& n, const string& s)
: m_SeqName(n), m_Sequence(s),m_Residues(0)
{
m_Residues.resize(m_Sequence.length());
for (int i = 0; i < int(m_Sequence.length()); i++) {
m_Residues.at(i).initializeResidue(m_Sequence.at(i),i);
}
}
~Sequence() { m_Residues.clear();};
void setSequence(const string& seq) {
m_Sequence = seq;
m_Residues.resize(int(seq.length()));
for (int i = 0; i < int(seq.length()); i++) {
m_Residues.at(i).initializeResidue(seq.at(i),i);
}
}
string sequence() const {
return m_Sequence;
}
void setSeqName(const string& seq_name)
{
m_SeqName = seq_name;
}
string seqName() const {
return m_SeqName;
}
int length() const {
return m_Sequence.length();
}
Residue residue(const int& position) {
if (position >= m_Residues.size() || position < 0) {
cerr << "Index out of range\n";
exit(1);
}
else return m_Residues.at(position);
}
vector<Residue> residues() const {
return m_Residues;
}
void addResidue(Residue& res) {
m_Residues.push_back(res);
}
Sequence& operator=(const Sequence& xxx) {
if (this != &xxx) {
m_SeqName = xxx.seqName();
m_Sequence = xxx.sequence();
m_Residues = xxx.residues();
}
return *this;
}
};
typedef vector<Sequence> VecS;
class Alignment {
Sequence m_RefSeq;
VecS m_ListOfSequences;
public:
Alignment(): m_ListOfSequences(0) {};
~Alignment() { m_ListOfSequences.clear(); };
Sequence refSeq() {
return m_RefSeq;
}
VecS listOfSeqs() const
{
return m_ListOfSequences;
}
void addSeq(Sequence& seq) {
m_ListOfSequences.push_back(seq);
}
void setRefSeq(const string& id) {
for (int i=0; i < int(m_ListOfSequences.size()); i++) {
if (m_ListOfSequences.at(i).seqName().find(id) != string::npos) {
m_RefSeq = m_ListOfSequences.at(i);
}
}
}
};
////////////////////////// MAIN ///////////////////////////////////
int main(int argc, char* argv[]) {
Alignment align;
Sequence seq1("1cje","KITVHFINRD");
Sequence seq2("Q5PPU4_XENLA","KLTVNFINRN");
Sequence seq3("Q9VZE1_DROME",".VNITFVRAN");
align.addSeq(seq1);align.addSeq(seq2);align.addSeq(seq3);
align.setRefSeq("1cje");
cout << align.refSeq().seqName() << "\t" << align.refSeq().sequence()
<< endl;
for (int i=0; i < align.refSeq().residues().size(); i++) {
cout << align.refSeq().residue(i).name() << "\t";
cout << align.refSeq().residue(i).pos() << "\t";
align.refSeq().residue(i).setNumber("pp");
cout << align.refSeq().residue(i).number() << "\n";
}
return 0;
}