473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

structs problem

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!!!
Aug 2 '06 #1
5 1270
Daniele M. wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #2
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.
Aug 2 '06 #3
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.

Aug 3 '06 #4
ev******@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '06 #5
Victor Bazarov ha scritto:
>(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?
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.
Aug 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
6
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
16
by: Michi Henning | last post by:
Below is a bit of code that creates a delegate. The delegate invokes a member function of a struct. The code compiles, but has surprising behavior: using System; namespace ConsoleApplication1...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
5
by: Steve Edwards | last post by:
Hi, With much help from this forum I've been using stl containers, but always with other common - or stl - types as members. I've now run in to a problem while trying to use some of my own...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.