Redeclaration error when forward declaraing a structure

D

DhaneshNair

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

DhaneshNair

I didnt get u .. :( ??
* (e-mail address removed):

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?
 
H

Howard

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top