Odd #include problem in VC++ - Programmer

This is a discussion on Odd #include problem in VC++ - Programmer ; I've been programming C and C++ for about 14 years, and ran into something I can't explain today. I have a header file myclass.h (simplified) that contains one class CMyClass::CMyClass() { .... } In my first CPP file, I have ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Odd #include problem in VC++

  1. Odd #include problem in VC++

    I've been programming C and C++ for about 14 years, and ran into something I
    can't explain today.

    I have a header file myclass.h (simplified)
    that contains one class
    CMyClass::CMyClass() {
    ....
    }

    In my first CPP file, I have
    #include zot.h
    #include zap.h

    (which contains
    #include myclass.h
    CZapper::CZapper() {
    ....
    private:
    CMyClass m_myclass;
    }


    Now, this compiled (and built) fine, but if I make a
    econd CPP file, I have
    #include zot.h
    #include myclass.h // just moved this

    #include zap.h
    (which now contains
    CZapper::CZapper() {
    ....
    private:
    CMyClass m_myclass;
    }

    the compiler complains that it does not know what CMyClass is. All I did
    was move
    #include myclass.h
    out of one file to another. I was under the impression that the precompiler
    just reads all includes into the compile stream.

    Its a scope problem of some kind I would guess, but I'm dammed if I can see
    why this would matter.

    Ideas anyone?

    Stewart DIBBS




  2. Re: Odd #include problem in VC++

    Stewart DIBBS wrote:

    > Its a scope problem of some kind I would guess, but I'm dammed
    > if I can see why this would matter.


    Not really.

    > Ideas anyone?


    If you have code like this in a header file:

    CZapper::CZapper() {
    ...
    private:
    CMyClass m_myclass;
    }

    you need to write the code like this:

    #include myclass.h

    CZapper::CZapper() {
    ...
    private:
    CMyClass m_myclass;
    }

    and this ensure that CMyClass is always defined.

    But in the case you describe the compiler has come across
    the m_myclass item which it knows is of type CMyClass but
    since it has not yet read the myclass.h file it also has
    definition of what a CMyClass is.

    Jussi Jumppanen
    Author of: Zeus for Windows, Win32 (Brief, Emacs, etc) FTP Text Editor
    "The C/C++, Java, HTML, FTP, Python, PHP, Perl programmer's editor"
    Home Page: http://www.zeusedit.com

  3. Re: Odd #include problem in VC++

    Hmmm. guess I was having a "senior programmer moment". I looked closer (ie
    scrolled up) at the build output, and the problem was in another file, a
    simple missing #include ... think I'll go the pub now, should be safer for
    the project tonite..

    Stewart DIBBS






+ Reply to Thread