I am getting this error when trying to use a constant defined in a class in one file as an array size in a different file. I am using MSVC 2008.

Here is my code.
.h:

#ifndef GLOBALS_H
#define GLOBALS_H

#include "stdafx.h"

class GlobalVars
{
public:
  GlobalVars();
  static const long LKNR_STEPS=250;
  static const long NR_AXIS=3;
  static const long NR_PAR=6;

};

#endif

.cpp:

#include "stdafx.h"
#include "Globals.h"

GlobalVars::GlobalVars()
{
  ;
}

In separate .cpp file:

#include "Globals.h"

class CSingleFactor
{
public:
  CSingleFactor()
  {
    m_nAxis=0;
  };

  int m_nAxis;
  double m_dData[GlobalVars::LKNR_STEPS];

};

This gives me the following error:
error C2653: 'GlobalVars' : is not a class or namespace name

I have tried it as:
double m_dData[LKNR_STEPS];
But then I get:
error C2065: 'LKNR_STEPS' : undeclared identifier

Any clues?
Thanks

I see a couple things:

  1. You can't initialize a floating-point constant inside your class definition, some compilers allow it for integral types though
  2. You haven't declared "global" versions of your static member variables. In order for a static variable to work, you must provide a "global" declaration/initialization for it:
    class Example {
     public:
       static const double ExampleD;
    };
    
    const double Example::ExampleD = 3.141;
    
    int main () {
      cout << Example::ExampleD << endl;
      return 0;
    }

Edited 6 Years Ago by Fbody: fixed minor code goof

OK so I changed the class so that the constants aren't initialized inside definition.
.h file:

class GlobalVars
{
public:
GlobalVars();
static const long LKNR_STEPS;
static const long NR_AXIS;
static const long NR_PAR;
 
};

and added the init to the .cpp file:

#include "Globals.h"

const long GlobalVars::LKNR_STEPS=250;
const long GlobalVars::NR_AXIS=3;
const long GlobalVars::NR_PAR=6;

GlobalVars::GlobalVars()
{
  ;
}

But I still get the same error.

Also, I am not able to even declare a variable, like GlobalVars gVars;
in the header either. This gives the error: error C2146: syntax error : missing ';' before identifier 'gVars'

If I right-click on the GlobalVars and click goto definition, it goes right to the correct file and position, so it knows what I am trying to do, at least at the editor level.

CSingleFactor()
  {
    m_nAxis=0;
  }; //<---extraneous semi-colon

You have an extraneous semi-colon. It shouldn't, but remove it and see if it makes any difference. Other than that, I don't really see anything in the code that you've posted thus far that would warrant such an error.

You'll have to post the actual code where you are declaring the object. That error means you missed a semi-colon somewhere in the code above your attempted declaration. If I take your code, combine it with a simple main(), and declare a global GlobalVars object, it compiles just fine on my MSVC 2008.

Edited 6 Years Ago by Fbody: n/a

I tried compiling the header "Globals.h" and the first cpp defining GlobalVars().
It didn't give any error.

This article has been dead for over six months. Start a new discussion instead.