include - .h and static consts, strange behavior

Discussion in 'C++' started by Christopher Pisz, Mar 17, 2012.

  1. I normally never define a global in a header file, but some preexisting
    code I am working on does the following:

    -------
    //a.h
    // Someone wrote a comment that this header must be C compilable

    // I assume MSVC defines this
    #ifdef __cplusplus
    static const long someGlobal1 = 100;
    static const long someGlobal2 = 100;
    // etc.

    #else
    #define someGlobal1 (100);
    #define someGlobal2 (157);
    //etc.

    #endif


    -------
    //b.h

    #include "a.h"

    class MyClass
    {
    public:
    // Snip
    private
    SomeType[someGlobal] m_anArray;
    };
    -----

    I get a compile error that someGlobal is undefined. I think what was
    done is legal, at least on whatever standard VC 2008 uses. What am I
    missing?
     
    Christopher Pisz, Mar 17, 2012
    #1
    1. Advertising

  2. Christopher Pisz

    Dombo Guest

    Op 17-Mar-12 20:04, Christopher Pisz schreef:
    > I normally never define a global in a header file, but some preexisting
    > code I am working on does the following:
    >
    > -------
    > //a.h
    > // Someone wrote a comment that this header must be C compilable
    >
    > // I assume MSVC defines this
    > #ifdef __cplusplus
    > static const long someGlobal1 = 100;
    > static const long someGlobal2 = 100;
    > // etc.
    >
    > #else
    > #define someGlobal1 (100);
    > #define someGlobal2 (157);
    > //etc.
    >
    > #endif
    >
    >
    > -------
    > //b.h
    >
    > #include "a.h"
    >
    > class MyClass
    > {
    > public:
    > // Snip
    > private
    > SomeType[someGlobal] m_anArray;
    > };
    > -----
    >
    > I get a compile error that someGlobal is undefined. I think what was
    > done is legal, at least on whatever standard VC 2008 uses. What am I
    > missing?


    A 1 or a 2.
     
    Dombo, Mar 17, 2012
    #2
    1. Advertising

  3. On 3/17/2012 2:12 PM, Dombo wrote:
    > A 1 or a 2.


    Whoops, that was just a typo while posting. There is a 1 there.


    -------
    //a.h
    // Someone wrote a comment that this header must be C compilable

    // I assume MSVC defines this
    #ifdef __cplusplus
    static const long someGlobal1 = 100;
    static const long someGlobal2 = 100;
    // etc.

    #else
    #define someGlobal1 (100);
    #define someGlobal2 (157);
    //etc.

    #endif


    -------
    //b.h

    #include "a.h"

    class MyClass
    {
    public:
    // Snip
    private
    SomeType[someGlobal1] m_anArray;
    };
    -----

    I get a compile error that someGlobal is undefined. I think what was
    done is legal, at least on whatever standard VC 2008 uses. What am I
    missing?
     
    Christopher Pisz, Mar 17, 2012
    #3
  4. Le 17/03/2012 20:04, Christopher Pisz a écrit :
    > I normally never define a global in a header file, but some preexisting
    > code I am working on does the following:
    >
    > -------
    > //a.h
    > // Someone wrote a comment that this header must be C compilable
    >
    > // I assume MSVC defines this
    > #ifdef __cplusplus
    > static const long someGlobal1 = 100;
    > static const long someGlobal2 = 100;
    > // etc.
    >
    > #else
    > #define someGlobal1 (100); <== error

    #define someGlobal1 100

    > #define someGlobal2 (157); <== error

    #define someGlobal2 157
    > //etc.
    >
    > #endif
    >
    >
    > -------
    > //b.h
    >
    > #include "a.h"
    >
    > class MyClass
    > {
    > public:
    > // Snip
    > private <== error

    private:
    > SomeType[someGlobal] m_anArray; <== error

    SomeType m_anArray[someGlobal];
    > };
    > -----
    >
    > I get a compile error that someGlobal is undefined. I think what was
    > done is legal, at least on whatever standard VC 2008 uses. What am I
    > missing?
    >
    >
     
    Vianney Lançon, Mar 17, 2012
    #4
  5. On 3/17/2012 2:29 PM, Vianney Lançon wrote:
    > SomeType m_anArray[someGlobal1];


    -------
    //a.h
    // Someone wrote a comment that this header must be C compilable

    // I assume MSVC defines this
    #ifdef __cplusplus
    static const long someGlobal1 = 100;
    static const long someGlobal2 = 100;
    // etc.

    #else
    #define someGlobal1 100
    #define someGlobal2 157
    //etc.

    #endif

    -------
    //b.h

    #include "a.h"

    class MyClass
    {
    public:
    // Snip

    private:

    SomeType m_anArray[someGlobal1];
    };


    Compiler still says someGlobal1 is undefined.
     
    Christopher Pisz, Mar 17, 2012
    #5
  6. On 3/17/2012 3:38 PM, Christopher Pisz wrote:
    > On 3/17/2012 2:29 PM, Vianney Lançon wrote:
    >> SomeType m_anArray[someGlobal1];

    >
    > -------
    > //a.h
    > // Someone wrote a comment that this header must be C compilable
    >
    > // I assume MSVC defines this
    > #ifdef __cplusplus
    > static const long someGlobal1 = 100;
    > static const long someGlobal2 = 100;
    > // etc.
    >
    > #else
    > #define someGlobal1 100
    > #define someGlobal2 157
    > //etc.
    >
    > #endif
    >
    > -------
    > //b.h
    >
    > #include "a.h"
    >
    > class MyClass
    > {
    > public:
    > // Snip
    >
    > private:
    >
    > SomeType m_anArray[someGlobal1];
    > };
    >
    >
    > Compiler still says someGlobal1 is undefined.


    Perhaps if you lose the 'static', you get it resolved... Not sure about
    C, though (it's been many years), consider asking in the C group (down
    the hall to the left).

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 17, 2012
    #6
  7. Christopher Pisz

    Geoff Guest

    On Sat, 17 Mar 2012 14:04:16 -0500, Christopher Pisz
    <> wrote:

    >I normally never define a global in a header file, but some preexisting
    >code I am working on does the following:
    >
    >-------
    >//a.h
    >// Someone wrote a comment that this header must be C compilable
    >
    >// I assume MSVC defines this
    >#ifdef __cplusplus
    >static const long someGlobal1 = 100;
    >static const long someGlobal2 = 100;
    >// etc.
    >
    >#else
    >#define someGlobal1 (100);
    >#define someGlobal2 (157);
    >//etc.
    >
    >#endif
    >
    >
    >-------
    >//b.h
    >
    >#include "a.h"
    >
    >class MyClass
    >{
    >public:
    > // Snip
    >private
    > SomeType[someGlobal1] m_anArray;
    >};
    >-----
    >
    >I get a compile error that someGlobal1 is undefined. I think what was
    >done is legal, at least on whatever standard VC 2008 uses. What am I
    >missing?
    >


    __cplusplus is implementation defined in C++ programs only.
    What makes a file a C++ program in Visual C++?
    Answer: The file name extension is .cpp
     
    Geoff, Mar 17, 2012
    #7
  8. Christopher Pisz <> wrote:
    > On 3/17/2012 2:29 PM, Vianney Lançon wrote:
    >> SomeType m_anArray[someGlobal1];

    >
    > -------
    > //a.h
    > // Someone wrote a comment that this header must be C compilable
    >
    > // I assume MSVC defines this
    > #ifdef __cplusplus
    > static const long someGlobal1 = 100;
    > static const long someGlobal2 = 100;
    > // etc.
    >
    > #else
    > #define someGlobal1 100
    > #define someGlobal2 157
    > //etc.
    >
    > #endif
    >
    > -------
    > //b.h
    >
    > #include "a.h"
    >
    > class MyClass
    > {
    > public:
    > // Snip
    >
    > private:
    >
    > SomeType m_anArray[someGlobal1];
    > };
    >
    >
    > Compiler still says someGlobal1 is undefined.


    Those are two header files. Header files are never compiled directly but
    included in .cpp files. So what does the cpp files look like?
    And did you really verify that this minimal example doesn't compile? (and
    not just the original source code where you extracted those lines).

    Tobi
     
    Tobias Müller, Mar 18, 2012
    #8
  9. On 3/18/2012 2:34 PM, Tobias Müller wrote:
    > Those are two header files. Header files are never compiled directly but
    > included in .cpp files. So what does the cpp files look like?
    > And did you really verify that this minimal example doesn't compile? (and
    > not just the original source code where you extracted those lines).
    >
    > Tobi


    I made a new console project day before yesterday. It is a bit different
    than the original source. The minimal example compiled OK.

    I then tried adding another factor to the minimal example, in that the
    one header was in a separate project. It still compiled OK.

    So, I am left thinking that my minimal example isn't really an example
    of what is going on at all and that the compile error in the original
    code is due to some other unknown factor. It being unknown, I can't make
    an example of it. So therefore, I suppose this topic is dead.

    Apologies.
     
    Christopher Pisz, Mar 19, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. trying_to_learn

    initializing non-static consts

    trying_to_learn, Nov 7, 2004, in forum: C++
    Replies:
    4
    Views:
    555
    Karthik Kumar
    Nov 7, 2004
  2. Baget
    Replies:
    5
    Views:
    388
    Tom Widmer
    Mar 21, 2005
  3. nick
    Replies:
    1
    Views:
    293
    Ivan Vecerina
    Nov 13, 2005
  4. Oliver S
    Replies:
    4
    Views:
    399
    Jonathan Mcdougall
    May 18, 2006
  5. Christopher

    consts in classes

    Christopher, Nov 8, 2007, in forum: C++
    Replies:
    7
    Views:
    365
    James Kanze
    Nov 9, 2007
Loading...

Share This Page