![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Dec 2009
Posts: 16
| Program crashes when trying to assign a value to a class variable Code: void find(vector<macros> &m, file &f)
{
char *c_file;
c_file=new char[f.oldLength()];
f.oldFile().copy(c_file, f.oldLength());
for(int x=0;x<f.oldLength();x++)
{
if(c_file[x]=='#')
{
x++;
if(isValidMacro(c_file,x))
{
x+=5;
f.addMacro();
m.push_back(macros());
m[f.numOfMacros()].atLoc(x);
m[f.numOfMacros()].toSymbol(getSymbol(c_file,x));
}
}
}
}
string getSymbol(char *c_file, int &pos)
{
stringstream ss_symbol;
string symbol;
do
{
pos++;
}while(c_file[pos]!=' ');
do
{
ss_symbol<<c_file[pos];
pos++;
}while(c_file[pos]!='('&&c_file[pos]!=' ');
ss_symbol>>symbol;
return symbol;
}
Code: void macros::toSymbol(string newSymbol)
{
pSymbol=newSymbol;
}
Code: class macros
{
public:
macros();
~macros();
void toSymbol(string newSymbol), toToken(string newToken), atLoc(int location);
string symbol(), token(), args(int num);
int numOfArgs(), loc();
private:
string pSymbol, pToken, *pArgs;
int pNumOfArgs, pLocation;
};
Thanks. |
| verxintRising is offline | |
| | #2 |
| CSharpener Join Date: Oct 2006
Posts: 5,556
| 1. why are you using char array instead of vector of chars? 2. why are you using operator[] instead of at() to access your macros vector 3. after first m.push_back(macros()); your vector contains 1 element I suppose. What is the value of f.numOfMacros() - is it zero at this time?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #3 | |
| Registered User Join Date: Dec 2009
Posts: 16
| Quote:
1. Never thought of that but it's a good idea. I never had a problem with using pointers for dynamic arrays, so I always did that. 2. Didn't know the at() function existed/was preferred. 3. This is probably an issue, f.numOfMacros() would be returning 1 right now. That solved the problem, but I don't understand why it was crashing on the line in the class function instead of the line that tried to call the nonexistent part of the vector. | |
| verxintRising is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| I need the code of few programs in c++.plzzzzz help...plzzz | NAVINKR20 | C++ Programming | 1 | 05-08-2009 09:13 AM |
| Need help with a class variable. | RealityFusion | C++ Programming | 11 | 10-20-2005 10:26 AM |
| fopen(); | GanglyLamb | C Programming | 8 | 11-03-2002 12:39 PM |