Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with using a class in another

Status
Not open for further replies.

rukkyg

Programmer
Jun 1, 2002
2
US
Here's the errors:

Compiling...
knowledge.cpp
c:\my documents\comp sci ap\ai\vc++\ai\person.h(32) : error C2146: syntax error : missing ';' before identifier 'myKnowledge'
c:\my documents\comp sci ap\ai\vc++\ai\person.h(32) : error C2501: 'Knowledge' : missing storage-class or type specifiers
c:\my documents\comp sci ap\ai\vc++\ai\person.h(32) : error C2501: 'myKnowledge' : missing storage-class or type specifiers
person.cpp
c:\my documents\comp sci ap\ai\vc++\ai\person.h(32) : error C2146: syntax error : missing ';' before identifier 'myKnowledge'
c:\my documents\comp sci ap\ai\vc++\ai\person.h(32) : error C2501: 'Knowledge' : missing storage-class or type specifiers
c:\my documents\comp sci ap\ai\vc++\ai\person.h(32) : error C2501: 'myKnowledge' : missing storage-class or type specifiers

-------------------------------------------------
knowledge.h

#include <fstream.h>
#include &quot;include\apstring.h&quot;
#include &quot;nodetypes.h&quot;
#include &quot;include\apvector.h&quot;

class Knowledge
{
...
};

-------------------------------------------------

person.h


#include &quot;knowledge.h&quot;
#include &quot;include\apstring.h&quot;
#include &quot;utils.h&quot;

#include <fstream.h>

class Person
{
public:
Person();
Person(apstring &);
Person(const apstring & , int);
~Person();

apstring Ask(apstring question);
void Say(const apstring &);
bool isUser();
private:
bool amUser;
void LoadMemory();


const char* file;
apstring myName;

Knowledge myKnowledge; //line 32
};

 
My wild guess is that you have some circular header inclusions there. Does the &quot;person.h&quot; gets included from other header files specified in Knowledge.h?

If so, just mention the Knowledge class name before defining Person; this is called a class declaration (or empty type specification) and it is a helper for the compiler when such situations are present:

#include &quot;knowledge.h&quot;
#include &quot;include\apstring.h&quot;
#include &quot;utils.h&quot;

#include <fstream.h>

class Knowledge;

class Person
{
public:
Person();
Person(apstring &);
Person(const apstring & , int);
~Person();
...

I guess this should work. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
The class declaration only works when you just need a pointer or reference to an object of that class, and you don't invoke any functions through it. In the case of Person, there is an actual Knowledge object, not just a pointer thereto, so a class declaration wouldn't be sufficient there.

 
I was including person.h in the nodetypes. I took that away and then saw that I had apparantly not included person.h in to person.cpp. I probably took it out when i was changing all the files trying to get it work. It Compiles now. Now to get it working. Thanks for help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top