Fix "does not name a type" without forward declaration

Pages: 12
Apr 30, 2013 at 12:58pm
closed account (DEUX92yv)
All of my header files have guards, so I won't post those in this case.
An extremely simplified version of my program, with some relevant stuff:

A class used in main (provided for comparison):
1
2
3
4
5
6
7
8
//character.h
#include <QGraphicsPixmapItem>
#include <QObject>
class Character: public QObject, public QGraphicsPixmapItem {
    Q_OBJECT
    public:
        Character(int x, int y);  // defined in character.cpp
};


A class inherited by five others to allow for easily keeping track of them:
1
2
3
4
5
6
7
// baseclass.h
#include <QGraphicsPixmapItem>
class BaseClass: public QGraphicsPixmapItem {
    public:
        BaseClass(int x, int y); // defined in baseclass.cpp;
                                 // is different than that for Character()
};


One of these five classes; the one I'm having trouble with:
1
2
3
4
5
6
// enemy.h
#include "baseclass.h"
class Enemy: public BaseClass {
    public:
        Enemy(int x, int y);
};

And its implementation:
1
2
3
4
5
6
7
// enemy.cpp
#include "enemy.h"
Enemy::Enemy(int x, int y)
    : BaseClass(x, y)
{
    // other members' initializations
}


The main window class, where the error occurs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// window.h
#include // Qt headers, from all of which an object is used in Window,
         // or Window inherits from it

// Forward declarations
class Character; // causes no problems
class Enemy; // doesn't work

class Window: public /* relevant Qheader */ {
    Q_OBJECT
    private:
        Character* character; // no problem
        Enemy* enemy; // 'Enemy' does not name a type
};
Last edited on Apr 30, 2013 at 1:05pm
Apr 30, 2013 at 1:11pm
Does problem persist in this stripped down programm? Because it won't for me.
Apr 30, 2013 at 1:15pm
VS2012 cl generates object code from this with no errors.
1
2
3
4
5
6
7
8
9
class Character;
class Enemy;

class Window {

    private:
        Character* character;
        Enemy* enemy;
};

Apr 30, 2013 at 1:37pm
Copy your project to a new folder and start stripping things out until it compiles. That's the easiest way to figure it out with us being able to see your code.
Last edited on Apr 30, 2013 at 1:37pm
Apr 30, 2013 at 1:40pm
closed account (DEUX92yv)
I have determined the problem. In my Window class, I have an enum State. One of the elements of State is named Enemy. Poor naming practice on my behalf caused Enemy* to be read as 6*, which as we all know is a pointer to 6.

Credit to VS2012's syntax highlighter not quite highlighting the word Enemy! (As it does with all other classes)

Such a simple problem for many minds much greater than mine to have pondered; I apologize for wasting your time. Thank you very much for your help!
Last edited on Apr 30, 2013 at 1:43pm
Apr 30, 2013 at 1:49pm
In C++11 you should use an enum class for proper scoping ;)
Apr 30, 2013 at 1:56pm
closed account (DEUX92yv)
I'm not sure what you mean.
Apr 30, 2013 at 1:59pm
Apr 30, 2013 at 2:04pm
closed account (DEUX92yv)
I see. I didn't realize that enums were now available as a class, but even I in my minute understanding of C++ see the value of that improvement (at least the part that I can understand). Thanks all for your time.
Apr 30, 2013 at 2:09pm
closed account (DEUX92yv)
Something in that article caught my attention. If NULL is 0, why does the keyword NULL exist at all? Is it only for human-readable code?

Also, what is useful about a function that only accepts an empty pointer? The one thing I can think of (which is HUGELY valuable, don't get me wrong) is that you can't overwrite dynamically allocated memory this way. Other than that...?
Apr 30, 2013 at 2:20pm
why does the keyword NULL exist at all
For readability and as a backward compatibility with C. Today you should use nullptr.
Actually NULL isn't always 0. In gcc on 64x machines it is 0LL (of long long type, where 0 is int type). 0LL (or NULL as my compiler set to 64bit mode) gives me an ambiguos overload, 0 calls int function and nullptr cals char* function.
what is useful about a function that only accepts an empty pointer
No idea.
Apr 30, 2013 at 2:49pm
trojansdestroy wrote:
what is useful about a function that only accepts an empty pointer?
If you are overloading functions for many pointer types
1
2
void foo(char*);
void foo(int*);

calling foo(nullptr) would be ambiguous. Adding an overload for std::nullptr_t fixes the problem.

void foo(std::nullptr_t);
Topic archived. No new replies allowed.
Pages: 12