Multiplication instead of pointer declaration

Discussion in 'C++' started by Stefan Weber, May 2, 2007.

  1. Stefan Weber

    Stefan Weber Guest


    I have the following problem:


    class MyClass {};

    This class is included somewhere in another header file:


    #include "MyClass.h"

    class Foo {
    MyClass* bar;

    Unfortunately, this does not seem to work. I'm using Visual Studio
    2005 and it provides the following error:

    error C2143: syntax error : missing ';' before '*'
    error C4430: missing type specifier - int assumed. Note: C++ does not
    support default-int
    error C4430: missing type specifier - int assumed. Note: C++ does not
    support default-int

    Obviously, VS does not recognize the type properly, but I can't see
    where the problem is.

    Some more facts about the project: It's a COM DLL for MS Windows using
    ATL, precompiled headers are turned on.

    Any solution or help to track down the problem is welcome :)


    Stefan Weber
    Stefan Weber, May 2, 2007
    1. Advertisements

  2. * Stefan Weber:
    Missing include guards.

    Missing semicolon.
    Alf P. Steinbach, May 2, 2007
    1. Advertisements

  3. Stefan Weber

    Stefan Weber Guest

    Missing include guards.
    Now, it looks like that:


    #ifndef __MyClass_h__
    #define __MyClass_h__

    class MyClass {};



    #include "MyClass.h"

    class Foo {
    MyClass* bar;

    } ;

    However, the problem remains.
    Stefan Weber, May 2, 2007
  4. Stefan Weber

    Zeppe Guest

    Of course, the problems were not related with what you are
    experiencing... from the two files you shown everything seems ok, is it
    possible that the problem is related to some other header?

    Zeppe, May 2, 2007
  5. * Stefan Weber:
    The problem is not with the code, which apart from the name
    __MyClass_h__[1] is OK.

    The problem is a misleading diagnostic, combined with something, e.g.
    picking up the wrong the include file, that means you don't have a
    declaration or definition of MyClass in sight in the main program.

    Just get used to misleading diagnostics... And check what "MyClass.h"
    file is actually included. Perhaps you have the main program and the
    "MyClass.h" file in different folders, with an old copy of "MyClass.h"
    in the same folder as the main program?


    - Alf

    [1] "__" is reserved for the implementation, which means the name is
    technically invalid (also, "_" followed by uppercase is reserved), and
    macro names should by convention be all uppercase.
    Alf P. Steinbach, May 2, 2007
  6. Stefan Weber

    Gavin Deane Guest

    That's not a valid name for an include guard. You aren't allowed to
    use double underscores (there are restrictions on leading single
    underscores too).

    Simplifying slightly, if I put the following, as a single translation
    unit (i.e. not a separate header and source file) into Comeau online,
    it compiles OK

    class MyClass {};

    class Foo {
    MyClass* bar;
    } ;

    Now, changing to VC++ 2005, I put your separate MyClass.h and Foo.h
    into a brand new project (<OT> you say you are using VC++ 2005 - I set
    up a brand new, empty, console program, with precompiled headers off </
    OT>) and created a cpp file containing simply

    #include "Foo.h"
    int main() {}

    the whole program builds OK. Are you sure there is no other code
    involved in your program anywhere?

    Note that if you find that changing your project setup in any way
    makes the problem go away, the best people to ask about understanding
    that will be in a Visual C++ group.

    Gavin Deane
    Gavin Deane, May 2, 2007
  7. Stefan Weber

    Greg Herlihy Guest

    Lose the #include directive and use a forward declaration instead:

    class MyClass;

    class Foo {
    MyClass * bar;

    Greg Herlihy, May 2, 2007
  8. Stefan Weber

    Stefan Weber Guest

    Thanks a lot for all your answers. I could not find the error yet, but
    I decided to start a new Visual Studio Project and will copy
    everything step by step from the old project (luckily, the project is
    still very small...). Now it works, but I'd rather understand why
    there were problems before, so I'm still searching.

    Anyway, can someone explain to me what the problem of a multiple
    include could be? I mean, I'm sure that I included the header file
    with the class in question. Is there a way that this class will be
    undeclared when something with the header files is messed up?


    Stefan Weber, May 2, 2007
  9. Stefan Weber

    Gavin Deane Guest

    Unless I've missed it, the answer to this one isn't in the FAQ,
    although the question is at least as frequently asked as others that
    are in the FAQ. But the next best thing is Google, which found:

    Gavin Deane
    Gavin Deane, May 2, 2007
  10. Le 02.05.2007 15:51, Stefan Weber a écrit :
    Maybe some hard-to-see typo; kind of like this one:

    class SmallObject;

    class Box
    Small0bject *p;

    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad
    \ \_L_) Pour bien répondre avec Google, ne pas cliquer
    -'(__) « Répondre », mais « Afficher les options »,
    _/___(_) puis cliquer « Répondre » (parmi les options).
    Serge Paccalin, May 2, 2007
  11. Stefan Weber

    Default User Guest

    Post a COMPLETE MINIMAL program that demonstrates the problem. Often in
    creating the minimal program, the problem will reveal itself.

    Default User, May 2, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.