"Error: base does not name a type"

So I am wondering why I have obtained the error that says "does not name a type." What does this mean?

Build errors:
||=== Build: Debug in cpptest (compiler: GNU GCC Compiler) ===|
C:\Users\Kevin\Documents\Codeblocks\cpptest\test2.h|11|error: 'base' does not name a type|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Relevant Code:
1
2
3
4
5
6
7
8
9
10
11
//test2.h
#include <SDL.h>
class Test
{
   public:
      void a(int);

   private:
      SDL_Rect* base;
      base->x = 9;
};


1
2
3
4
5
6
7
8
9
10
11
//cpptest.cpp
#include <SDL.h>
#include "test2.h"

using namespace std;


int main()
{
    return 0;
}

Last edited on
It means that line 10 is illegal in the context of a class definition.
Can you initialize variables inside a class definition, or can you only declare them?
Inside a class definition you can supply a default initialization value which will be used on construction of an instance provided a relevant constructor doesn't specify its own initialization value, but that is not what you are doing here.

1
2
3
4
5
6
7
8
class Test
{
   public:
      void a(int);

   private:
      SDL_Rect* base = nullptr;   // provide a default initialization value.
};
Hmm...So, prior to c++11, you could only initialize a static const member in the class declaration.

That's been relaxed significantly since c++11.

However, what you are doing here is different.

It's recommended that you initialize the "base" pointer in the constructor(s) for Test. After that, you should move the expression base->x = 9 to the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <SDL.h>

class Test
{
   public:
      Test(SDL_rect* sdl_base) : base(sdl_base)
     {
          base->x = 9; 
     }
      void a(int);

   private:
      SDL_Rect* base;
};
Ok, I get what is meant by putting the pointer initialization in the constructor. However, what is it that I am trying to do here that is illegal? Initializing member variables of an object via a pointer?
Last edited on
:) Pretty sure that's illegal. In the class declaration, you cannot initialize a member variable of a member class/struct via a pointer or otherwise.
Topic archived. No new replies allowed.