C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 26th, 2005, 06:34 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old April 29th, 2005, 12:49 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Runtime error - function calls a function in a class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT