X does not name a type error in C++
When the compiler compiles the class User
and gets to the MyMessageBox
line, MyMessageBox
has not yet been defined. The compiler has no idea MyMessageBox
exists, so cannot understand the meaning of your class member.
You need to make sure MyMessageBox
is defined before you use it as a member. This is solved by reversing the definition order. However, you have a cyclic dependency: if you move MyMessageBox
above User
, then in the definition of MyMessageBox
the name User
won't be defined!
What you can do is forward declare User
; that is, declare it but don't define it. During compilation, a type that is declared but not defined is called an incomplete type.
Consider the simpler example:
struct foo; // foo is *declared* to be a struct, but that struct is not yet defined
struct bar
{
// this is okay, it's just a pointer;
// we can point to something without knowing how that something is defined
foo* fp;
// likewise, we can form a reference to it
void some_func(foo& fr);
// but this would be an error, as before, because it requires a definition
/* foo fooMember; */
};
struct foo // okay, now define foo!
{
int fooInt;
double fooDouble;
};
void bar::some_func(foo& fr)
{
// now that foo is defined, we can read that reference:
fr.fooInt = 111605;
fr.foDouble = 123.456;
}
By forward declaring User
, MyMessageBox
can still form a pointer or reference to it:
class User; // let the compiler know such a class will be defined
class MyMessageBox
{
public:
// this is ok, no definitions needed yet for User (or Message)
void sendMessage(Message *msg, User *recvr);
Message receiveMessage();
vector<Message>* dataMessageList;
};
class User
{
public:
// also ok, since it's now defined
MyMessageBox dataMsgBox;
};
You cannot do this the other way around: as mentioned, a class member needs to have a definition. (The reason is that the compiler needs to know how much memory User
takes up, and to know that it needs to know the size of its members.) If you were to say:
class MyMessageBox;
class User
{
public:
// size not available! it's an incomplete type
MyMessageBox dataMsgBox;
};
It wouldn't work, since it doesn't know the size yet.
On a side note, this function:
void sendMessage(Message *msg, User *recvr);
Probably shouldn't take either of those by pointer. You can't send a message without a message, nor can you send a message without a user to send it to. And both of those situations are expressible by passing null as an argument to either parameter (null is a perfectly valid pointer value!)
Rather, use a reference (possibly const):
void sendMessage(const Message& msg, User& recvr);
Class name does not name a type in C++
The preprocessor inserts the contents of the files A.h
and B.h
exactly where the include
statement occurs (this is really just copy/paste). When the compiler then parses A.cpp
, it finds the declaration of class A
before it knows about class B
. This causes the error you see. There are two ways to solve this:
- Include
B.h
inA.h
. It is generally a good idea to include header files in the files where they are needed. If you rely on indirect inclusion though another header, or a special order of includes in the compilation unit (cpp-file), this will only confuse you and others as the project gets bigger. If you use member variable of type
B
in classA
, the compiler needs to know the exact and complete declaration ofB
, because it needs to create the memory-layout forA
. If, on the other hand, you were using a pointer or reference toB
, then a forward declaration would suffice, because the memory the compiler needs to reserve for a pointer or reference is independent of the class definition. This would look like this:class B; // forward declaration
class A {
public:
A(int id);
private:
int _id;
B & _b;
};This is very useful to avoid circular dependencies among headers.
I hope this helps.
error: 'x' does not name a type
Include the declaration for "Game" in a header
notepad main.h =>
#ifndef MAIN_H
#define MAIN_H
class Game
{
private:
...
public:
...
};
#endif
// main.h
notepad main.cpp =>
#include "main.h"
Game g; // We should be OK now :)
int
main(int argc, char* args[])
{
return 0;
}
gcc -g -Wall -pedantic -I../include -o main main.cpp
Note how you:
1) Define your classes (along with any typedefs, constants, etc) in a header
2) #include the header in any .cpp file that needs those definitions
3) Compile with "-I" to specify the directory (or directories) containing your headers
'Hope that helps
X does not name a type in c++
Your errors have nothing to do with inheritance. The same code would be an error without any inheritance.
In C++ statements like value = 10;
must be placed inside functions or constructors. So this is OK (statement inside a constructor)
class MainClass{
int value= 10;
MainClass()
{
value = 10;
}
};
so is this (statement inside a function)
class MainClass{
int value= 10;
void function()
{
value = 10;
}
};
On the other hand int value = 10;
is a declaration so it can go outside of a function.
This is very basic C++ syntax and semantics. You need to understand the difference between a statement and a declaration. It suggests to me that you need to spend some more time learning the basics of C++ before you tackle inheritance.
Another x does not name a type error
The compiler is trying to parse line 458 as a declaration. It is not, it is a statement. Statements must be written inside a function. Like this:
void initialize()
{
stack[0] = &stackbase;
}
Y does not name a type error in C++
You cannot put assignments outside the context of a function in C++. If you're puzzled by the fact that you sometimes saw the =
symbol being used outside the context of a function, such as:
int x = 42; // <== THIS IS NOT AN ASSIGNMENT!
int main()
{
// ...
}
That's because the =
symbol can be used for initialization as well. In your example, you are not initializing the data members width
and height
, you are assigning a value to them.
pointer name does not name a type error
These:
front = NULL;
rear = NULL;
are assignment statements. And this:
Node *front = NULL, *rear = NULL;
is a declaration (definition, initialization) statement using in-class initializers.
Assignment statements are not allowed to appear in the class declaration body, whereas the initialization statements are.
Does not name a type, C++ error?
Replace:
#ifdef DISPLAY_H
// ...
#endif
with:
#ifndef DISPLAY_H
// ...
#endif
Your compiler basically sees Display.h as an empty file, because DISPLAY_H
is not defined, and #ifdef
skips out the whole class declaration in the header file.
Related Topics
What Does Std::Match_Results::Size Return
Same Random Numbers Every Loop Iteration
What Does It Mean When a Numeric Constant in C/C++ Is Prefixed With a 0
Fast Ceiling of an Integer Division in C/C++
What Does the Thread_Local Mean in C++11
C++: Constructor Initializer For Arrays
Split a String into Words by Multiple Delimiters
What Kind of Optimization Does Const Offer in C/C++
Mismatch Detected For 'Runtimelibrary'
What Are the Different Calling Conventions in C/C++ and What Do Each Mean
What Is Constructor Inheritance
How to Redirect Qdebug, Qwarning, Qcritical etc Output
Difference Between Static, Auto, Global and Local Variable in the Context of C and C++