C++: vectors: why i lose data?

i have 1 vector public inside of a class.
after create a class, i create an instance\object.
now i call the class function that add information on vector.
on where i see the vector size '1'(correct).
after finish the function, i call the vector size, and i get zero.. why i can lose these data?
(the vector instance is on class, on global\outside the function, and it's public.)
It's hard to guess what the problem is without seeing the code. Could you perhaps post a minimum example that demonstrates the problem?
Perhaps you're working on a copy of the vector, instead of the original.

Hard to guess without seeing any code.
after i declare them static, i don't lose the data... i'm confused why i can't use non-static :(
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//................
static bool blnMainFunction;

    static void ValidRules(vector<TokensSimplification> tokens)
    {

        //variable declarion:
        if(tokens[0].Description=="var")
        {
            for(unsigned int index=1, item=0; index<tokens.size(); index++)
            {
                if(tokens[index].Description==",")
                {
                    if(item<4)
                    {
                        ErrorList.push_back("variable declarion wrong");
                        break;
                    }

                    item=0;
                    continue;
                }
                //Must be declared with value:
                else if(index==(tokens.size()-1) && (item <4 || item <3))
                {
                    ErrorList.push_back("You must assign the variable.");
                }
                else if(tokens.size()<6)
                {
                    ErrorList.push_back("Variable declaration incompleted.");
                    break;
                }

                //the varname can't start with a number:
                else if(isdigit(tokens[index].Description[0])==true && item==0)
                {
                    ErrorList.push_back("variable name can't start with a number");
                    break;
                }
                //the varname can't be a another variable or a key word:
                else if(IsKeyWord(tokens[index].Description)==true && item==0)
                {
                    ErrorList.push_back( "variable name can't be a keyword");
                    break;
                }
                else if(tokens[index].Description!="as" && item==1)
                {
                    ErrorList.push_back("missing 'as' for add the typename");
                    break;
                }
                else if(IsKeyWord(tokens[index].Description)==false && item==2)
                {
                    ErrorList.push_back("invalid typename");
                    break;
                }
                else if(tokens[index].Description!="=" && item==3)
                {
                    ErrorList.push_back("variable must be assignment");
                    break;
                }
                if(item ==0)
                {
                    VarList.push_back(tokens[index].Description);
                    cout << VarList.size();
                }

                item++;
            }
        }

        //functions and procedures:
        else if((tokens[0].Description=="function")||(tokens[0].Description=="procedure"))
        {
            if(IsKeyWord(tokens[1].Description)==true)
            {
                ErrorList.push_back("The function\\procedure name can't be a KeyWord!!!");
            }
            else if(tokens[1].Description=="main")
            {
                blnMainFunction=true;
            }
            else if(tokens[2].Description!="(")
            {
                ErrorList.push_back("After a function\\procedure name, we must add parenteses");
            }
            else if(tokens[tokens.size()-1].Description!=")" &&( tokens[0].Description!="function" || tokens[0].Description!="procedure"))
            {
                ErrorList.push_back("A function\\procedure must have a closed parenteses");
            }
            else if(tokens[3].Description!=")" && tokens[3].Description!=",")
            {
                if(tokens.size()>4)
                {
                    for(unsigned int i=3, item=0; i <tokens.size(); i++)
                    {
                        if (tokens[i].Description==",")
                        {
                            item=0;
                            continue;
                        }
                        else if(IsKeyWord(tokens[i].Description) && item ==0)
                        {
                            ErrorList.push_back("the parameter name can't be a keyword");
                            break;
                        }
                        else if(tokens[i].Description!="as" && item ==1)
                        {
                            ErrorList.push_back("the parameter variable must have 'as', before the type name");
                            break;
                        }
                        else if(!IsKeyWord(tokens[i].Description) && item == 2)
                        {
                            ErrorList.push_back("you must add valid type name");
                            break;
                        }
                        else if(tokens[i].Description!="=" && item==3)
                        {
                            ErrorList.push_back("you must add assignment before the value");
                            break;
                        }
                        item++;
                    }
                }
            }
        }
    }

    void ConvertCode(vector<TokensSimplification> tokens)
    {

       //variable declarion:
        if(tokens[0].Description=="var")
        {
            for(unsigned int i=1; i <tokens.size(); i+=6)
            {
                cout << tokens[i+2].Description<< " " << tokens[i].Description << " = " << tokens[i+4].Description;
                if(tokens[i+4].Description=="-" || tokens[i+4].Description=="+" )
                    cout << tokens[i+5].Description;
                cout << ";\n";
            }
        }

        //end function\procedure:
        else if(tokens[0].Description=="end" && (tokens[1].Description=="function" || tokens[1].Description=="procedure"))
        {
            cout << "}";
        }

        //return:
        else if(tokens[0].Description=="return")
        {
            cout << "return " << tokens[1].Description << ";";
        }

        //procedure declarion:
        else if(tokens[0].Description=="procedure" ||  tokens[0].Description=="function")
        {
            if(tokens[0].Description=="function")
            {
                cout << tokens[tokens.size()-1].Description << " " << tokens[1].Description << tokens[2].Description;
            }
            else
            {
                cout << "void "<< tokens[1].Description << tokens[2].Description;
            }
            if(tokens.size()>4)
            {
                for(unsigned int i=3; i <tokens.size(); i+=5)
                {
                    if(tokens[i].Description==")")
                    {

                        break;
                    }

                    cout << tokens[i+2].Description<< " " << tokens[i].Description << " = " << tokens[i+4].Description ;
                    if(tokens[i+5].Description==",")
                    {
                        cout  << ",";
                    }
                    if(tokens[i+5].Description==")")
                    {
                        cout << "){\n";
                    }
                }
            }
        }
    }
    static vector <string> ErrorList;
    static vector <string> VarList;
    static vector <string> ClassList;

    compiler()
    {
        //nothing;
    }

    compiler(string strLineCode)
    {
        vector<TokensSimplification> tokens=CatchTokens(strLineCode);


           if(tokens.size()==0)
                return;
        ValidRules(tokens);


        if (ErrorList.size()==0)
        {
            ConvertCode(tokens);
        }
    }
};
compiler Compile;
bool compiler::blnMainFunction =false;
vector<string> compiler::VarList(0);
vector<string> compiler::ErrorList(0);

too much code :(
Where is the function that adds data to the vector?
ValidRules function.
Yes, my guess is that the vector you were editing is not the same as the one you then looked at.

By making the vector a static class member, all instances of the class share it.

This would suggest that before, you have more than one instance of the class and you were looking at different instances.
i found the error:
1
2
3
4
for(unsigned int i =0; i<strCompile.size(); i++)
    {
        Compile=compiler((string)strCompile[i]); //the loop calls the constructor
    }

the constructor clean all non-static variables.
thank you so much for all
Last edited on
Topic archived. No new replies allowed.