Multiplication instead of pointer declaration

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

  1. Stefan Weber

    Stefan Weber Guest

    Hi,

    I have the following problem:

    MyClass.h
    ---------------

    class MyClass {};

    This class is included somewhere in another header file:

    Foo.h
    --------

    #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 :)

    Thanks,

    Stefan Weber
     
    Stefan Weber, May 2, 2007
    #1
    1. Advertising

  2. * Stefan Weber:
    > Hi,
    >
    > I have the following problem:
    >
    > MyClass.h
    > ---------------
    >
    > class MyClass {};
    >


    Missing include guards.


    > This class is included somewhere in another header file:
    >
    > Foo.h
    > --------
    >
    > #include "MyClass.h"
    >
    > class Foo {
    > MyClass* bar;
    > }


    Missing semicolon.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 2, 2007
    #2
    1. Advertising

  3. Stefan Weber

    Stefan Weber Guest

    > Missing include guards.
    >
    > [...]
    >
    > Missing semicolon.


    Now, it looks like that:

    MyClass.h
    ---------------

    #ifndef __MyClass_h__
    #define __MyClass_h__

    class MyClass {};

    #endif

    Foo.h
    --------

    #include "MyClass.h"

    class Foo {
    MyClass* bar;

    } ;

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

    Zeppe Guest

    Stefan Weber wrote:
    >> Missing include guards.
    >>
    >> [...]
    >>
    >> Missing semicolon.

    >
    > Now, it looks like that:
    >
    > MyClass.h
    > ---------------
    >
    > #ifndef __MyClass_h__
    > #define __MyClass_h__
    >
    > class MyClass {};
    >
    > #endif
    >
    > Foo.h
    > --------
    >
    > #include "MyClass.h"
    >
    > class Foo {
    > MyClass* bar;
    >
    > } ;
    >
    > However, the problem remains.
    >


    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
     
    Zeppe, May 2, 2007
    #4
  5. * Stefan Weber:
    >> Missing include guards.
    >>
    >> [...]
    >>
    >> Missing semicolon.

    >
    > Now, it looks like that:
    >
    > MyClass.h
    > ---------------
    >
    > #ifndef __MyClass_h__
    > #define __MyClass_h__
    >
    > class MyClass {};
    >
    > #endif
    >
    > Foo.h
    > --------
    >
    > #include "MyClass.h"
    >
    > class Foo {
    > MyClass* bar;
    >
    > } ;
    >
    > However, the problem remains.


    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?

    Hth.,

    - 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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 2, 2007
    #5
  6. Stefan Weber

    Gavin Deane Guest

    On 2 May, 11:13, Stefan Weber <> wrote:
    > > Missing include guards.

    >
    > > [...]

    >
    > > Missing semicolon.

    >
    > Now, it looks like that:
    >
    > MyClass.h
    > ---------------
    >
    > #ifndef __MyClass_h__
    > #define __MyClass_h__


    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).


    > class MyClass {};
    >
    > #endif
    >
    > Foo.h
    > --------
    >
    > #include "MyClass.h"
    >
    > class Foo {
    > MyClass* bar;
    >
    > } ;
    >
    > However, the problem remains.


    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
    #6
  7. Stefan Weber

    Greg Herlihy Guest

    On May 2, 2:59 am, Stefan Weber <> wrote:
    > Hi,
    >
    > I have the following problem:
    >
    > MyClass.h
    > ---------------
    >
    > class MyClass {};
    >
    > This class is included somewhere in another header file:
    >
    > Foo.h
    > --------
    >
    > #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:


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

    class MyClass;

    class Foo {
    MyClass * bar;
    };

    Greg
     
    Greg Herlihy, May 2, 2007
    #7
  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?

    Thanks,

    Stefan
     
    Stefan Weber, May 2, 2007
    #8
  9. Stefan Weber

    Gavin Deane Guest

    On 2 May, 14:51, Stefan Weber <> wrote:
    > 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?


    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:

    http://en.wikipedia.org/wiki/Include_guard

    Gavin Deane
     
    Gavin Deane, May 2, 2007
    #9
  10. Le 02.05.2007 15:51, Stefan Weber a écrit :

    > 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.


    Maybe some hard-to-see typo; kind of like this one:

    class SmallObject;

    class Box
    {
    Small0bject *p;
    };

    --
    ___________
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_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
    #10
  11. Stefan Weber

    Default User Guest

    Stefan Weber wrote:

    > Hi,
    >
    > I have the following problem:


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




    Brian
     
    Default User, May 2, 2007
    #11
    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. Noah
    Replies:
    5
    Views:
    969
  2. Alex Vinokur
    Replies:
    4
    Views:
    603
    Jonathan Turkanis
    Apr 5, 2004
  3. Bolin
    Replies:
    4
    Views:
    414
  4. karthikbalaguru
    Replies:
    38
    Views:
    9,101
    Dik T. Winter
    Mar 9, 2009
  5. William Hughes
    Replies:
    13
    Views:
    1,237
    Ben Bacarisse
    Mar 15, 2010
Loading...

Share This Page