I've got a base class, and five classes that inherit from it using public BaseClass. In each derived class's header, I #include baseclass.h .
In a different header file, I #include all five of these classes (but not the base class), and declare a pointer to one of each. The problem is, I only get the
'class' does not name a type
error for one of these classes. I'm positive I spelled it right, so that's not the problem. I also tried forward declaration (which gave me more errors). What could the problem be?
I do have header guards.
I've also tried declaring the pointer this way: BaseClass* instance;
thinking that polymorphism compiles. Then g++ decides that the inheriting class is an int*, which it is most definitely not.
// baseclass.h
#ifndef BASECLASS_H
#define BASECLASS_H
#include // some Qt headers
class BaseClass: public /* Qt object */ {
public:
BaseClass(int x, int y);
// methods
protected:
// members
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13
// class.h
#ifndef CLASS_H
#define CLASS_H
#include "baseclass.h"
class Class: public BaseClass {
public:
Class(int x, int y);
};
#endif
1 2 3 4 5 6 7 8 9
// class.cpp
#include "class.h"
Class::Class(int x, int y)
: BaseClass(x, y)
{
// do stuff
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// main.h
#ifndef MAIN_H
#define MAIN_H
#include // more Qt headers
#include "class.h" // and 4 other classes
class Main {
public:
// methods
private:
//members
Class* classPtr; // <--- The error: 'Class' does not name a type
};
#endif
Check your header guards, if there is two same header guards, it will prevent your second class from including.
And I hope that your class in main does not have the same name as problem class like you showed.
A great read, thanks for the link.
Since my main.h only uses a pointer, I went ahead and forward declared the class (whose parent is BaseClass, and thus includes baseclass.h) in main.h. This does not resolve the error.
Start by stripping out all unnecesary things (actual uses of classes, make function definition empty, remove inheritance of QTobject) while checking if problem persist. Then rename all classes, check problem again and post it. That way we will see your problem and you will not show any actual code.
It took longer than I expected to whittle the code down.
@ne555, L B: I know for the sake of legibility, it's bad practice to have multiple classes/methods/members/etc with the same name (especially main) -- and I do NOT do this in my program (or any of them) -- but is there anything stopping me from creating a class Main?
@trojansdestroy: There is nothing wrong with a class named Main, in this case it is a design pattern in C++ ;)
I was linking @ne555 to an example of the design pattern being used in one of my other projects.