vector as a private class member

Hi!
I get a problem with the vector as a private class member:
When I did't initialize the vector in constructor(which means the size of the vector would be 0), I used a class function to add two elements to the vector and it worked (because I added a "printf" to output the size of the vector and the elements within that function). However, when I used another class function to visit that vector, no element was in and the size became 0.
Then I tried to add two elements to the vector during the construction, and it turned out that these two elements could be stored in the vector while other elements added through class functions could not.

I guess there may be some problems on the scope of the function. But I feel the class member should not be effected by the scope of the class function.

Does any one know why this happen and how to solve it? Thank you for your help!
Posting the segment of your code where this happens could help show the problem.
class A{
AddToVector(int i)
{ vec.puch_back(i);}
FunctionA( )
{ printf("%d\n",vec.size();}
FunctionB(int x, int y)
{ AddToVector(x); AddToVector(y);}
private:
std::vector<int> vec;
};

void main(){
A a;
a.FunctionB(x,y);
a.FunctionA();
}

Here's an example. I thought the output should be 2 but it turned to be 0....

If I add a printf to print the size in FunctionB, the result would be 2 and 0.

If I use the constructor to add two elements in initialization, the result would be 4 and 2.

There may exist grammer error. But I feel that's not the point. After all the program can execute. So I feel the point may be the management of the class or the vector.
Last edited on
a.FunctionA;

This does nothing because you forgot the parenthesis:

Should be:
a.FunctionA();


Also it should be int main, not void main.
class methods should also be set to public since they default to private unless it otherwise stated
like
1
2
3
4
5
6
7
8
9
10
11
class A{
   private:
      vector<int> vec;
   public:
      void AddToVector(int i)
      {
         vec.push_back(i);
      }
      void FunctionA()
      {
        ....
Last edited on
Thanks for the above. But still not the case. My progam can execute normally despite the unexpected results.....So not about public or private...
you might want to try it instead of automatically saying thats not the issue....
using gcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <vector>
using std::vector;
using std::cout;
using std::endl;

class A{
   private:
      vector<int> vec;
   public:
      void AddToVector(int i)
      {
         vec.push_back(i);
      }
      void FunctionA()
      {
         cout << vec.size() << endl;
      }
      void FunctionB(int x, int y)
      {
         AddToVector(x);
         AddToVector(y);
      }
};

int main(){
A a;
int x =23, y =3;
a.FunctionB(2,2);
a.FunctionA();
return 0;
}

works perfectly and prints out 2. all that was done to it was fixed your many errors and added public to methods.....
Last edited on
@OP: the code you posted does not reproduce your issue.
It has other problems, it wouldn't even compile.

Please post the offending code.
Regards
Topic archived. No new replies allowed.