Public Group

# Object member does not name a type C++

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

## 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 on other sites

This line is missing a comma:

PP(person& person):cPerson(person) /* , */ nameRandz()

My mistake.

##### 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.

##### 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 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 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 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 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 10
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634146
• Total Posts
3015779
×