JT.
JT.

Reputation: 291

Does Not Name A Type in C++

in C++ when i get an error that says xxxxx does not name a type in yyy.h

What does that mean?

yyy.h has included the header that xxxx is in.

Example, I use:

typedef CP_M_ReferenceCounted FxRC;

and I have included CP_M_ReferenceCounted.h in yyy.h

I am missing some basic understanding, what is it?

Upvotes: 10

Views: 82682

Answers (5)

Owl
Owl

Reputation: 1562

Although possibly unrelated to OP's original question... this is an error I just had and shows how this error could occur.

When you define a type in a C++ class and you return it, you need to specify the class in which the type belongs.

For example:

class ClassName{
public:
 typedef vector<int> TypeName;
 TypeName GetData();
};

Then GetData() must be defined as:

ClassName::TypeName ClassName::GetData(){...}

not

TypeName ClassName::GetData(){...}

Otherwise the compiler will come back with the error:

error: 'TypeName' does not name a type

Upvotes: 3

skittlebiz
skittlebiz

Reputation: 377

Be sure you didn't copy paste this yyy.h file from some other class header and keep the same "YYY_H__" in the new #ifndef as in the original file. I just did this and realized my mistake. Make sure to update your new YYY_H__ to represent the new class. Otherwise, obviously, YYY_H__ will already be defined from the original file and the important stuff in this header will be skipped.

Upvotes: 1

wangzheqie
wangzheqie

Reputation: 93

Yes, you should try to check the namespace first.

Upvotes: 1

Test
Test

Reputation: 1727

That seems you need to refer to the namespace accordingly. For example, the following yyy.h and test.cpp have the same problem as yours:

//yyy.h
#ifndef YYY_H__
#define YYY_H__

namespace Yyy {

class CP_M_ReferenceCounted
{
};

}

#endif

//test.cpp
#include "yyy.h"

typedef CP_M_ReferenceCounted FxRC;


int main(int argc, char **argv)
{

        return 0;
}

The error would be

...error: CP_M_ReferenceCounted does not name a type

But add a line "using namespace Yyy;" fixes the problem as below:

//test.cpp
#include "yyy.h"
// add this line
using namespace Yyy;

typedef CP_M_ReferenceCounted FxRC;
...

So please check the namespace scope in your .h headers.

Upvotes: 7

John Weldon
John Weldon

Reputation: 40749

The inclusion of the CP_M_ReferenceCounted type is probably lexically AFTER the typedef... can you link to the two files directly, or reproduce the problem in a simple sample?

Upvotes: 3

Related Questions