![]() |
|
#1
|
||||
|
||||
Class ProblemI'm writing a rogue-like RPG in c++, and right now I'm having a problem with one of my classes. Here is my main file:
CPP / C++ / C Code:
Most of the code here is irrelevant (I think), but the program generates a runtime error. When I comment out the line marked "PROBLEM LINE", the program runs ok. However, I kind of need the environment class for my entire game. Here is its code: CPP / C++ / C Code:
Also, method declarations are included. The program compiles with no errors and no warnings. However, when I try to declare the Environment object, a pop-up error message is displayed and the program exits. The Map object also has the same problem. What's wrong with my code? |
|
#2
|
||||
|
||||
|
Could you post the code for your Map class? Since the crash occurs when you declare either Map or Environment, and since an Environment contains a Map, I suspect the problem is in Map.
Matthew |
|
#3
|
||||
|
||||
|
Ok, sure. Here it is:
CPP / C++ / C Code:
|
|
#4
|
||||
|
||||
|
Hmm... I am sorry to say that I do not see the source of the error in the code you have posted (maybe I'm missing it). BUT, what I do see is that you are passing complex objects to functions as by-value parameters, rather than by-reference.
CPP / C++ / C Code:
CPP / C++ / C Code:
Another thing I noticed is that you have not defined copy constructors or assignment operators for your objects. This means that the compiler will generate default definitions for these functions, which may not do the right thing. For example, if your object contains a member variable that is a pointer to another object, then the default copy constructor (and assignment operator) will make only a shallow copy of the member; that is, a copy of the pointer address, not a new object instance that is like the original member variable. What I am thinking at this point is that the error probably results from the program making a copy of a temporary object (by constructor or assignment), and then the temporary is destroyed and the copy contains a pointer to some out object that no longer exists. Then, somewhere the program tries to use that non-existent object, and CRASH! The classes Tile and/or Monster are likely suspects from the portions of code I can see here. I may be way off here regarding the cause of the error, but I do recommend not using pass-by-value in C++ with complex objects. Unnecessary overhead for the copying, plus it may not be doing what you think it is if default copy constructor and assignment are used. To try to solve this yourself, try placing some printf(), or cout, statements throughout the code to help pinpoint where exactly this error is occurring. I understand that this can be very tedious if you have a lot of code and no idea where to start. I do have an idea about that, however. That's about all I can come up with unless you can post some debug output or additonal code. Matthew P.S. I also noticed that your classes do not have destructors declared. It is very possible that for your some of your classes the default destructor is not appropriate. |
|
#5
|
|||
|
|||
maybe this is the problemEnvironment(){}
i usually do it this way Environment(); i could be wrong.. |
|
#6
|
||||
|
||||
Pointer ProblemOk, I'm replacing object refrences in the class with object pointers. However, I'm adding them the the Environment class like this:
CPP / C++ / C Code:
|
|
#7
|
||||
|
||||
|
Quote:
Is this in the class declaration or when you're creating an Environment object? My compile (Dev-c++) throws an error when I try to create an object that way... |
|
#8
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
If you are managing these Monsters using pointers inside your classes, then the method addMonster() should take a pointer or non-const-reference to a Monster instead of a Monster by-value. Then, your method call might look like this, CPP / C++ / C Code:
I am intending here to provide some insight related to your question, and not to suggest that might code example be used as it is. At this point, I don't have enough understanding of your specific program to make such a suggestion. I'll be happy to try answering further questions. Matthew |
|
#9
|
||||
|
||||
|
Quote:
Either usage is correct. Note that that with your version the constructor still needs to be defined somewhere. Probably, outside the class declaration, in an implementation .cpp rather than the header file. Though it is often preferred to define methods, including constructors, outside the class declaration, it is still correct to both declare and define in one shot. Matthew |
Recent GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Inheritance problem | CaptnB | C++ Forum | 2 | 27-Jan-2005 08:09 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | C++ Forum | 2 | 19-Dec-2004 06:06 |
| hashing help | saiz66 | C++ Forum | 1 | 06-Jul-2004 06:16 |
| problem with creating class | mohammed | C++ Forum | 1 | 11-Oct-2003 09:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The