| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Runtime error - function calls a function in a class
My project (for now) is a single-player text RPG game. The code is over 400 lines, so I'll only include it if somebody feels they need to see all of it.
My problem is that there is a runtime error and I have determined the source but can't determine the cause and don't know how to avoid it. This is my main() function. Code:
int main(){
srand(time(NULL));
string Command;
Sword * mySwords = new Sword[2];
mySwords[1] = Sword("Custom Sword",10);
Living * Player1 = new Living("Jimmy",18,0);
do{
for (int i=0;i<2;i++){
cout << i+1 << " ";
mySwords[i].weaponReturn();
}
getline(cin,Command);
inCommand(Command);
}while (!quitRequest(Command));
cout << mySwords[1].Name();
if (DeleteAll()) cout << "Dynamic objects deleted.\nGood bye.";
return 0;
}
The program runs just fine, until something is inputted where it calls a class function which involves a class variable. In other words, it can run a class function which does -not- involve a single variable, but if one does, it crashes. Here is one of the functions: Code:
string GameObjectClass::Name(){
return _name;
}
Here are the relevant pieces of the function that calls it (I only cut out a large piece of "if" statements) Code:
void inCommand(string myCommand){
string oneCommand = TruncateStringAtOccurance(myCommand,' ');
int twoCommand = atoi(KeepAfterOccurance(myCommand,' ').c_str());
twoCommand --;
if (oneCommand=="stats"){
for (int i=0;i<2;i++){
mySwords[i].Name();
}
}
}
It crashes as soon as return _name; is called, in the function GameObjectClass::Name. In fact, even a function such as this: Code:
void inCommand(string myCommand){
mySwords[0].Name();
}
never gets past the first line. The thing is, if I call mySwords[0].Name() from main(), it does not crash. But as soon as it is inside another function, there is a runtime error. The function doesn't even have to do anything before it crashes. I tried adding the function as a friend of each class, but it didn't work. Can somebody help? |
|
#2
|
|||
|
|||
|
It's been a few days now and I can't figure it out.. I've asked any programming friends and they don't know... can anyone help?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Runtime error - function calls a function in a class |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|