structs problem

D

Daniele M.

Hi folks!
I'm almost newbie in c++ programming, so please don't blame me :)

I have a little (i hope) problem with following code and i really can't
understand what i'm missing :(
Hope you can help me!

The following code is from my simple GUI class.



// in my gui.h definition file, i have:
struct _placed_items {
struct _items *item;
struct _placed_items *next;
};
struct _windows {
struct _placed_items *placeditems;
}
struct _items {
// some stuff
}

// in a private function of the class gui.cpp, i have:
struct _items *newitem=new _items;
struct _placed_items *newplaceditem=new _placed_items;
newplaceditem->item=newitem;


When i try to compile it, i have this error:
gui.cpp(180) : error C2440: '=' : cannot convert from 'struct
GUI::_items *' to 'struct _items *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
(Wrong line is: newplaceditem->item=newitem;)


For read pleasure, you can find a well formatted version of the code here:
http://nopaste.simosnap.com/1151

Thanks in advance for your time!!!
 
V

Victor Bazarov

Daniele said:
Hi folks!
I'm almost newbie in c++ programming, so please don't blame me :)

Who's to blame, then? ;)
I have a little (i hope) problem with following code and i really
can't understand what i'm missing :(
Hope you can help me!

The following code is from my simple GUI class.



// in my gui.h definition file, i have:
struct _placed_items {

The use of a global name that begins with an underscode is reserved by the
compiler. Simply put: don't do that.
struct _items *item;

You don't need to use the keyword 'struct' here (and in other object
declarations and definitions). The name of the type is sufficient.
struct _placed_items *next;
};
struct _windows {

Same comment. A global name you make up must not start with an underscore.
struct _placed_items *placeditems;
}
^^^
Missing semicolon here.
struct _items {
// some stuff
}
^^^
Missing semicolon here as well.
// in a private function of the class gui.cpp, i have:
struct _items *newitem=new _items;
struct _placed_items *newplaceditem=new _placed_items;
newplaceditem->item=newitem;


When i try to compile it, i have this error:
gui.cpp(180) : error C2440: '=' : cannot convert from 'struct
GUI::_items *' to 'struct _items *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
(Wrong line is: newplaceditem->item=newitem;)

You apparently have some naming conflicts in your program. You need
to resolve them. There is not enough information in your posting (or
on the page you offered for "well formatted version") to make a good
suggestion as to what you need to do to resolve your conflict. It
seems, however, that you define some structs in the GUI namespace and
some other structs outside, but using the former ones... Possibly
you have a misplaced "using" directive.

It is a good idea _not_ to use 'using' directives until you have
a better grasp of how names are resolved.

Also, the use of underscores in your names made my eyes hurt.

V
 
D

Daniele M.

Victor Bazarov ha scritto:
[cut]
You apparently have some naming conflicts in your program. You need
to resolve them. There is not enough information in your posting (or
on the page you offered for "well formatted version") to make a good
suggestion as to what you need to do to resolve your conflict. It
seems, however, that you define some structs in the GUI namespace and
some other structs outside, but using the former ones... Possibly
you have a misplaced "using" directive.

It is a good idea _not_ to use 'using' directives until you have
a better grasp of how names are resolved.

Also, the use of underscores in your names made my eyes hurt.

Thanks for your patience, i'll make the changes you suggested and will
try again :)

Daniele.
 
E

evilmaio

Victor,

declaring structs prototypes did the trick ;)


class GUI {

struct a;
struct b;
struct c;

private:
struct a {
struct b *pointB;
struct C *pointC;
};
struct b {
struct a *pointA;
struct C *pointC;
};
struct c {
struct a *pointA;
struct b *pointC;
};
};

Now it compiles well; do you think that code is functional?
(well actually it works, i don't know if this is the best way to do
that :D)

Thanks.

Daniele.
 
V

Victor Bazarov

Victor,

declaring structs prototypes did the trick ;)

You would have avoided that mistake if you dropped the keyword
'struct' from the member declarations. If you don't tell the
compiler that 'b' is a 'struct' when you declare 'pointB', for
example, then the compiler is forced to figure out where 'b' comes
from and will complain if 'b' is undefined. You will then have
to either define or declare it (like you did here). Otherwise the
compiler assumes that the definition will come from somewhere and
it definitely does not place the name into 'class GUI', but instead
presumes it's "::a" (i.e. coming from the enclosing namespace).
class GUI {

struct a;
struct b;
struct c;

private:
struct a {
struct b *pointB;

Drop 'struct' and make it

b *pointB;

And do the same in the rest of the member declarations.
struct C *pointC;
};
struct b {
struct a *pointA;
struct C *pointC;
};
struct c {
struct a *pointA;
struct b *pointC;
};
};

Now it compiles well; do you think that code is functional?

If it works [for you], use it.
(well actually it works, i don't know if this is the best way to do
that :D)

You didn't present any requirements for this code, so how can I tell
if the code meets them?

V
 
D

Daniele M.

Victor Bazarov ha scritto:
You didn't present any requirements for this code, so how can I tell
if the code meets them?

Yep, you're right ;)
The code in my first post simply didn't compile, so i supposed
requirements details was not necessary :D
Btw actually my knowledge level is too low for letting me search the
nicest way for my solutions
Mmm...for now i'll use "if it works, it's ok" way ;)

Just tried removing "struct" as you suggested, it works well too; i
always thought that keeping the "struct" word, could be useful for easy
reading; i never suspected this could generate a problem!

Thanks again for your support!

Daniele.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top