views:

76

answers:

3

Can someone help me in this error?

in "cDef.h" :

#pragma once

class cDef 
{
public:

    static int STATE_LOGO;
    static int STATE_MENU;

    static int MESSAGE_ENTER;
    static int MESSAGE_UPDATE;
    static int MESSAGE_PAINT;
    static int MESSAGE_EXIT;
};

in "GameState.h":

#pragma once
#ifndef _GameState_
#define _GameState_
#include "cDef.h"

class MainGame;
class GameState;


class GameState
{
public:

    MainGame *mg;
    int GAME_STATE_DEF;

    virtual void MessengeEnter(int message) = 0;
    virtual void MessengeUpdate(int message,int keys) = 0;
    virtual void MessengePaint(int message,CDC *pDc) = 0;

void StateHandler(int message,CDC *pDc,int keys);

public:
    GameState(void);
public:
    ~GameState(void);
};
#endif

in "GameState.cpp":

#include "StdAfx.h"
#include "GameState.h"

GameState::GameState(void)
{
    GAME_STATE_DEF = -1;
}

GameState::~GameState(void)
{
}

void GameState::StateHandler(int message,CDC *pDc,int keys)
{
    if(message == cDef.MESSAGE_ENTER)
    {
        MessageEnter(message);
    }

    if(message == cDef.MESSAGE_UPDATE)
    {
        MessageUpdate(message,keys);
    }

    if(message == cDef.MESSAGE_PAINT)
    {
        MessagePaint(message,pDC);
    }
}

error:

warning C4832: token '.' is illegal after UDT 'cDef'
see declaration of 'cDef'
error C3861: 'MessageUpdate': identifier not found
error C3861: 'MessageEnter': identifier not found
error C3861: 'MessagePaint': identifier not found
...

Thanks in advance!

+1  A: 

You access static variables by using the scope operator :: instead of the member access operator .

Example:

cDef::MESSAGE_ENTER

Also you should be initializing the members of cDef in your cpp file to some value. In this scenario an enum is probably better by the way.

Brian R. Bondy
+2  A: 

Here's what I used to hammer into my students who had to learn C++ coming from Java and always were confused about when to use ::, ., and ->:

  • A::B means A is either a class or a namespace name.
  • A.B means A is either an object or a reference to an object.
  • A->B means A is either a pointer or an object of a type that has operator-> overloaded (a.k.a. "smart pointer")

If you know these, you can also apply them backwards, so that if you have an A and a B, you know what to put in between.

(I think these rules would have to be extended for C++11, but I'm not sure. If you know, feel free to add this.)

sbi
+1  A: 

This seems to be obvious once you see it - you declare the member function

virtual void MessengeEnter(int message) = 0;

but call

MessageEnter(message);

(notice the difference between Messenge and Message)

hjhill