Object member does not name a type C++

Started by
12 comments, last by Khatharr 8 years, 5 months ago

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");
}

Advertisement

This line is missing a comma:

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

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

This line is missing a comma:


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

My mistake.

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.

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.

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!

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

Hey this too

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

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. PP(person& person):cPerson(person) nameRandz()
  5. {}

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

This topic is closed to new replies.

Advertisement