Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to define global const class variable [modified]

how to define global const class variable [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpvisual-studiodata-structurestutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Electronic75
    wrote on last edited by
    #1

    Hello :) , I'm trying to define a global const [Class] varibles in a MFC application. This class holds an array of variables that I like to assign at the beginning and I will use this variable [and its stored const values] in my program in different classes[]. Now what I'm doing is to add a blank cpp file to my project and then I define this class variable there.

    #include "MyInfoArrayClass.h"

    const CMyInfoArrayClass g_Info;
    g_Info.Add(data1);
    g_Info.Add(data2);
    ...

    but compiler dose not see the definition and intellisense shows the type of g_Info as int and returns SYNTAX error and redefinition of basic type. Even when I place class definition in a header file and include header file in the source file compiler returns same errors. [I have done this with an char type or int type, I mean I have defined a global char or int variable, and I had no problem but when I want to define a class type variable I encounter trouble.] Where I made a mistake:confused: thanks :)

    modified on Saturday, August 30, 2008 4:13 AM

    O 1 Reply Last reply
    0
    • E Electronic75

      Hello :) , I'm trying to define a global const [Class] varibles in a MFC application. This class holds an array of variables that I like to assign at the beginning and I will use this variable [and its stored const values] in my program in different classes[]. Now what I'm doing is to add a blank cpp file to my project and then I define this class variable there.

      #include "MyInfoArrayClass.h"

      const CMyInfoArrayClass g_Info;
      g_Info.Add(data1);
      g_Info.Add(data2);
      ...

      but compiler dose not see the definition and intellisense shows the type of g_Info as int and returns SYNTAX error and redefinition of basic type. Even when I place class definition in a header file and include header file in the source file compiler returns same errors. [I have done this with an char type or int type, I mean I have defined a global char or int variable, and I had no problem but when I want to define a class type variable I encounter trouble.] Where I made a mistake:confused: thanks :)

      modified on Saturday, August 30, 2008 4:13 AM

      O Offline
      O Offline
      oobimoo
      wrote on last edited by
      #2

      You must declare extern the g_Info in the MyInfoArrayClass.h header, under the declaration of the class (or add a forward declaration of your class above the const extern CMyInfoArrayClass g_Info;).

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups