Multiplication instead of pointer declaration

S

Stefan Weber

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
 
A

Alf P. Steinbach

* Stefan Weber:
Hi,

I have the following problem:

MyClass.h

Missing include guards.

This class is included somewhere in another header file:

Foo.h
--------

#include "MyClass.h"

class Foo {
MyClass* bar;
}

Missing semicolon.
 
S

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

Zeppe

Stefan said:
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
 
A

Alf P. Steinbach

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

Gavin Deane

Missing include guards.

Missing semicolon.

Now, it looks like that:

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
 
G

Greg Herlihy

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
 
S

Stefan Weber

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
 
G

Gavin Deane

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
 
S

Serge Paccalin

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

Default User

Stefan said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top