Jump to content
  • Advertisement
Sign in to follow this  
KenWill

Object member does not name a type C++

This topic is 980 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I actually have two errors and i hope i can get an explination of why this doesn't work and how i can make them work.

The error

[attachment=29642:error.PNG]

And my code

PP.h

#ifndef PP_H
#define PP_H
#include "person.h"
#include "nameRandomizer.h"
#include <string>


class PP
{
private:
   person cPerson;
   nameRand nameRandz;
public:
    PP(person& person):cPerson(person) nameRandz()
        {};

};

#endif // PP_H

nameRandomizer.h

#ifndef NAMERANDOMIZER_H
#define NAMERANDOMIZER_H
#include "PP.h"

class nameRand
{
public:
    nameRand(){runRand();}
    void runRand();
};

#endif // NAMERANDOMIZER_H

nameRandomizer.cpp

#include "nameRandomizer.h"

void nameRand::runRand()
{
    person.setName("qwer");
}

Share this post


Link to post
Share on other sites
Advertisement

1. #include "nameRandomizer.h"

2. #define NAMERANDOMIZER_H

3. #include "PP.h"

4. #define PP_H

5. #include "nameRandomizer.h"

6. skip nameRandomizer (due to 2)

7. nameRand nameRandz;  has no defiition for "nameRand"

 

Don't cyclically #include things, include more files from the .cpp

Um...i have no idea what you're talking about, sorry.

Share this post


Link to post
Share on other sites
See Organizing Code Files in C and C++. The C preprocessor is not very smart and will inline the contents of a file when you include a file. If you have A include B and B include A, then you have a cyclic include, and will cause all sorts of trouble. You will need to remove uneeded includes, move them to where they're used (the source files), use references/pointers, and/or forward declaration. Edited by fastcall22

Share this post


Link to post
Share on other sites

See Organizing Code Files in C and C++. The C preprocessor is not very smart and will inline the contents of a file when you include a file. If you have A include B and B include A, then you have a cyclic include, and will cause all sorts of trouble. You will need to remove uneeded includes, move them to where they're used (the source files), use references/pointers, and/or forward declaration.

Thanks a lot!

Share this post


Link to post
Share on other sites

 

1. #include "nameRandomizer.h"

2. #define NAMERANDOMIZER_H

3. #include "PP.h"

4. #define PP_H

5. #include "nameRandomizer.h"

6. skip nameRandomizer (due to 2)

7. nameRand nameRandz;  has no defiition for "nameRand"

 

Don't cyclically #include things, include more files from the .cpp

Um...i have no idea what you're talking about, sorry.

 

Sorry, I should have been a little more clear what it means.

It's a list of actions that the C pre-processor does, just follow your own source code, and you will see it will encounter the things I listed.

Due to this sequence, the C preprocessor does not include the  "nameRand" definition before the compiler needs it, and gives you your first error.

 

To solve, break the cyclic dependency, nameRandomizer.h does not seem to need PP.h, so don't include it. Instead move that include line to the .cpp file

Share this post


Link to post
Share on other sites

Hey this too

  1. PP(person& person):cPerson(person) nameRandz()
  2. {};
  3. to this
  4.  
  5. PP(person& person):cPerson(person) nameRandz()
  6. {}

Share this post


Link to post
Share on other sites

 

 

1. #include "nameRandomizer.h"

2. #define NAMERANDOMIZER_H

3. #include "PP.h"

4. #define PP_H

5. #include "nameRandomizer.h"

6. skip nameRandomizer (due to 2)

7. nameRand nameRandz;  has no defiition for "nameRand"

 

Don't cyclically #include things, include more files from the .cpp

Um...i have no idea what you're talking about, sorry.

 

Sorry, I should have been a little more clear what it means.

It's a list of actions that the C pre-processor does, just follow your own source code, and you will see it will encounter the things I listed.

Due to this sequence, the C preprocessor does not include the  "nameRand" definition before the compiler needs it, and gives you your first error.

 

To solve, break the cyclic dependency, nameRandomizer.h does not seem to need PP.h, so don't include it. Instead move that include line to the .cpp file

 

Yeah, i was thinking about not having a .cpp file and thats why i include it there, but then i changed my mind and forgot to change it. But i'm glad i forgot because i didn't know about this until now. I have another question if you can help me: in composition, can a subclass access the private members of the base class? Because i'm trying to do so yet i get an error saying "cPerson is private".

 

 

Hey this too

  1. PP(person& person):cPerson(person) nameRandz()
  2. {};
  3. to this
  4.  
  5. PP(person& person):cPerson(person) nameRandz()
  6. {}

 

Using ";" at the end of a function is personal choice now, right? xD I sometimes do that, i don't know why.

Edited by KenWill

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!