If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Help, getting really strange errors!

Hello, I'm taking a C++ class, and I keep running into a strange occurrence. I'm writing a simulation program with people sending emails to one another and one of the requirements is we use abstract base classes when dealing with email forwarding strategies. The two ABC's in my project are EvaluateMessage and EvaluateRecipient. The Person class holds a pointer to each and when a Person is created a child class is chosen (based on an input file) to instantiate the pointers to. EvaluateMessage compiles and works just fine, however EvaluateRecipient is giving me problems and I don't know why. EvaluateRecipient works in other classes without a problem and the code all compiles and the input is all read fine, but when I include it in Person.h its gives me an error list this long in Visual Studio:

(The actual error list is much much longer but it's repetitive). I'm using guards in my header files, but reading the errors it seems that by add EvaluateRecipient to Person, I'm stripping all references of Person out of the project somehow. I don't know what's going on.

See the problem? In EvaluateRecipient.h, you are including Person.h, which includes EvaluateRecipient.h, which includes Person.h, which includes EvaluateRecipient.h, etc.

So it is an infinite recursion error. You usually solve this by using pointers to these objects and using forward declaration.

Secondly, don't put "using namespace std" in a header file. The reason is that if I were to use your header, you are forcing the entire std namespace into my C++ module. What if I don't want std namespace in my module, as it may cause compilation errors with other definitions I may have?

Instead, either prepend "std::" to the definitions that are in the std namespace (std::vector, std::list, etc), or state with the using clause exactly what you are using out of the std namespace:

Re: Help, getting really strange errors!

Excuse the double post but now I'm getting another error. I suspect it is once again due to me not setting up my hierarchy correctly. (Additionally if you have a link to a good hierarchy article I'd be much appreciated).

So I changed:

Code:

#include "EvaluateRecipient.h"

to the forward declaration:

Code:

class EvaluateRecipient;

and I recieved error "C2061: syntax error: identfier" in this block of code:

Code:

// Get the person's strategy on who to forward to in their contact list
inputFile >> tempInt;
switch(tempInt)
{
// Forward the message to all members of the contact list
case 1:
(people.back()->rStrategy) = new RStrategy1();
break;
// Forward the message to all members of the contact list except (possibly) the sender
case 2:
(people.back()->rStrategy) = new RStrategy2();
break;
// Forward the message to the "next" member on the person's contact list
case 3:
(people.back()->rStrategy) = new RStrategy3();
break;
// For error catching, sets the person's strategy to type 1
default:
(people.back()->rStrategy) = new RStrategy1();
break;
}

So I changed Person.h once again to include:

Code:

class RStrategy1;
class RStrategy2;
class RStrategy3;

and now I'm receiving error "C2512 : no appropriate default constructor available" in that same block instead. Is this because I included all child classes in the same header file as EvaluateRecipient? Also, why does EvaluateMessage get to be an "#include" instead of a forward declaration when its built almost exactly the same as EvaluateRecipient? (Note: EvaluateMessage doesn't compile if I try to make it a forward declaration instead of #include). Thanks for your patience.

Re: Help, getting really strange errors!

- And that fixed it! I guess my concern at this point is that I'm taking an intro C++ and the teacher is really failing in all regards, and I really don't know how to set up a hierarchy properly and I know that's going to hurt me down the line. I'm going to Google the topic, but if any of you have a good article or link I'd be really grateful. Thank you all for your help!

Re: Help, getting really strange errors!

I don't have any links but the general rule is to try to separate things as much as possible. Also, I guess the wisdom in how to achieve the perfect hierarchy has as many prophets as there are programmers...

Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.