Vector of classes problem


Hello, im passing a vector of a class to a function of another class.
But i cant access the data on the classes inside the vector.

Something like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CDummy{
  ...
  public:
    string m_name; 
  ...
}

class CAnother{
  ...
  public:
    void readName(vector<CDummy> vec);
  ...
}
void CAnother::readName(vector<CDummy> vec){
  cout<<vec.at(0).m_name<<endl; //this statement prints nothing
}



Is it possible?

ps:
Im creating the vector on main() and using push_back with a pointer to an initialized CDummy instance
Last edited on
You don't supply enough information to make an informed guess.

You're using pointers... but not storing them? Does the "initialized CDummy instance" initialize it's m_name member to something other than an empty string? Why does CAnother::readName take a vector by value?
Last edited on
On main i got this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
int main(){

 CDummy dummy;
 dummy.m_name = "sample name";
 
 vector<CDummy> dummyVect;
 dummyVect.push_back(dummy);

 CAnother another;
 another.readName(dummyVect);


}

 But readName() always print an empty string... I tried with pointer to vector too

Last edited on
My guess would be that you've supplied a copy constructor for CDummy that does the wrong thing.
Ill check for that
THANK YOU cire!!

The error was on my copy ctor.

1
2
3
4
CDummy::CDummy(const CDummy& orig) {
    
    this->m_name = orig.m_name;
}


Now its working like a charm
Last edited on
Topic archived. No new replies allowed.