Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Multiplication instead of pointer declaration (http://www.velocityreviews.com/forums/t503522-multiplication-instead-of-pointer-declaration.html)

Stefan Weber 05-02-2007 09:59 AM

Multiplication instead of pointer declaration
 
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


Alf P. Steinbach 05-02-2007 10:06 AM

Re: Multiplication instead of pointer declaration
 
* 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?

Stefan Weber 05-02-2007 10:13 AM

Re: Multiplication instead of pointer declaration
 
> 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.


Zeppe 05-02-2007 10:52 AM

Re: Multiplication instead of pointer declaration
 
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

Alf P. Steinbach 05-02-2007 10:55 AM

Re: Multiplication instead of pointer declaration
 
* 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?

Gavin Deane 05-02-2007 10:59 AM

Re: Multiplication instead of pointer declaration
 
On 2 May, 11:13, Stefan Weber <stefan.we...@gmail.com> 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


Greg Herlihy 05-02-2007 11:19 AM

Re: Multiplication instead of pointer declaration
 
On May 2, 2:59 am, Stefan Weber <stefan.we...@gmail.com> 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


Stefan Weber 05-02-2007 01:51 PM

Re: Multiplication instead of pointer declaration
 
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


Gavin Deane 05-02-2007 03:16 PM

Re: Multiplication instead of pointer declaration
 
On 2 May, 14:51, Stefan Weber <stefan.we...@gmail.com> 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


Serge Paccalin 05-02-2007 03:39 PM

Re: Multiplication instead of pointer declaration
 
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).


All times are GMT. The time now is 09:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.