By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,599 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,599 IT Pros & Developers. It's quick & easy.

Redeclaration error when forward declaraing a structure

P: n/a
Hi all,

I hav a file which is actually linkage file (used as an reference
interface between c and c++ files). And this file has got two
structures in it .. When i include this file directly i dont have any
issues. but according to some norms in the review I am not supposed to
use this file direclty in the header file .. but its fine if i use that
inside the corresponding file.

To avoid compiler error, i forward declared the structure as in ex.

struct MyStruct;

class MyClass
{
private :
MyStruct * me;
}

But when the actual cpp file which includes the prev file i mentioned
in the beginning I get a compiler error in actual file which has the
structure defintion.

say like : structure redefined, see myclass.cpp (some thing like this)

So i tried replacing this structure in the linkage .h file to a class
and forward declared it on the MyClass.cpp file.

ex:
//typedef struct
//{
// char *myName;
}MyStruct;

to

class MyStruct
{
public:
char *myName;
}

Now the build is perfect once i forward declare the class..

Do find any reason for it .. Do you find any error in my forward
declaration of struct..
Can u please help me .. what could be the solution.?

Nov 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* Dh*********@gmail.com:
>
Now the build is perfect once i forward declare the class..
Good for you.

--
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?
Nov 16 '06 #2

P: n/a
I didnt get u .. :( ??

Alf P. Steinbach wrote:
* Dh*********@gmail.com:

Now the build is perfect once i forward declare the class..

Good for you.

--
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?
Nov 16 '06 #3

P: n/a

<Dh*********@gmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
Hi all,

I hav a file which is actually linkage file (used as an reference
interface between c and c++ files). And this file has got two
structures in it .. When i include this file directly i dont have any
issues. but according to some norms in the review I am not supposed to
use this file direclty in the header file .. but its fine if i use that
inside the corresponding file.

To avoid compiler error, i forward declared the structure as in ex.

struct MyStruct;

class MyClass
{
private :
MyStruct * me;
}

But when the actual cpp file which includes the prev file i mentioned
in the beginning I get a compiler error in actual file which has the
structure defintion.

say like : structure redefined, see myclass.cpp (some thing like this)
Redefinition errors are often caused by not using "include guards" in
headers which are included by multiple source files. (But given the rest of
your post, I doubt that's the problem here.)

It's difficult to tell what's wrong when you use such vague language (like
"the actual file"). Be specific, and include the actual code, where
possible.
So i tried replacing this structure in the linkage .h file to a class
and forward declared it on the MyClass.cpp file.

ex:
//typedef struct
//{
// char *myName;
}MyStruct;
That's an old-style C declaration. To define a struct in C++, do it like
this:

struct MyStruct
{
char* myName;
};

There's no need to change it to a class type if you don't need it to be. A
struct type will work also. Just use the modern definition style above, not
that old typedef stuff.
to

class MyStruct
{
public:
char *myName;
}

Now the build is perfect once i forward declare the class..
It shouldn't. It's missing the semicolon (;) at the end of the class
definition.
>
Do find any reason for it .. Do you find any error in my forward
declaration of struct..
Can u please help me .. what could be the solution.?
I'm guessing, but possibly because of the use of a typedef?

-Howard

Nov 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.