Fix "does not name a type" without forward declaration

Pages: 12
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?
Last edited on
Do you have header guards in your headers?
If you don't, and don't know what they are, just put

#pragma once

at the beginning of all of your header files.
Just in case the compiler does not support that pragma:

http://en.wikipedia.org/wiki/Include_guard
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.
Last edited on
Cna you please show the header for problem file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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 
Last edited on
Why are you defining the class Class in both main.h and class.h?
CLASS_H

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.
Peter: That was a mistake; I've corrected it now.

The five inheriting classes each have their own header guard (i.e., class1, class2, etc.). I've made sure of this.
Last edited on
If you inherit from QObject, shouldn't you use the Q_OBJECT macro to get the moc behave?
Yes, I do use that macro. I just didn't find it relevant to the error I'm experiencing.
It's impossible to know what the problem is without seeing real code.
It's for an assignment, and I am not allowed to publicly post said real code. Could you private message me your email address?
Last edited on
¡¿class Main?!
http://www.cplusplus.com/forum/articles/10627/ (4)
forward declare B if: A contains a B pointer or reference: B* myb;


Provide a test case. http://eel.is/c++/testcase/
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.
Last edited on
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.
Last edited on
Pages: 12