473,837 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assign a struct variable into another struct variable



Hi, I would appreciate if someone could tell me what
I'm doing wrong ?
#include<iostre am>
using namepace std ;

struct person{
char *firstname ;
int age ;
} ;

struct person1{
char *firstname ;
int age ;
} ;

int main(int argc, char *argv[])
{
person me ;
person1 me1 ;

me.firstname = "me" ;
me.age = 150 ;

me1 = me ; // here's where the problem occurrs.....

*The Error Report I Get*

(structs.cpp: in function 'int main()':
structs.cpp:22: error: no match for 'operator=' in 'me1 = me'
structs.cpp:11: error: candidates are: main()::person1 &
main()::person1 ::operator=(con st main()::person1 &)
structs.cpp:29: 1: warning: no newline at end of file
)

Why can't I assign a struct to a struct like i can an int to an int, or
a char to a char, etc ? I can understand it would become
a problem if each struct had different members but these two don't!

thankyou much

Jul 23 '05 #1
18 4582
in************* @yahoo.com wrote:
Hi, I would appreciate if someone could tell me what
I'm doing wrong ?
#include<iostre am>
using namepace std ;

struct person{
char *firstname ;
int age ;
} ;

struct person1{
char *firstname ;
int age ;
} ;
The two types, 'person' and 'person1' are _distinct_ and _different_
AFA C++ is concerned. It doesn't matter that their internals are the
same. Since you defined two different types, they are different.

int main(int argc, char *argv[])
{
person me ;
person1 me1 ;

me.firstname = "me" ;
me.age = 150 ;

me1 = me ; // here's where the problem occurrs.....

*The Error Report I Get*

(structs.cpp: in function 'int main()':
structs.cpp:22: error: no match for 'operator=' in 'me1 = me'
structs.cpp:11: error: candidates are: main()::person1 &
main()::person1 ::operator=(con st main()::person1 &)
structs.cpp:29: 1: warning: no newline at end of file
)

Why can't I assign a struct to a struct like i can an int to an int, or
a char to a char, etc ? I can understand it would become
a problem if each struct had different members but these two don't!


You need to define (overload) the assignment operator for 'person' to
allow assigning from a 'person1' value. Read about operator overloading
in your favourite C++ book.

V
Jul 23 '05 #2
in************* @yahoo.com wrote:
Hi, I would appreciate if someone could tell me what
I'm doing wrong ?
#include<iostre am>
using namepace std ;

struct person{
char *firstname ;
int age ;
} ;

struct person1{
char *firstname ;
int age ;
} ;

int main(int argc, char *argv[])
{
person me ;
person1 me1 ;

me.firstname = "me" ;
me.age = 150 ;

me1 = me ; // here's where the problem occurrs.....

*The Error Report I Get*

(structs.cpp: in function 'int main()':
structs.cpp:22: error: no match for 'operator=' in 'me1 = me'
structs.cpp:11: error: candidates are: main()::person1 &
main()::person1 ::operator=(con st main()::person1 &)
structs.cpp:29: 1: warning: no newline at end of file
)

Why can't I assign a struct to a struct like i can an int to an int, or
a char to a char, etc ? I can understand it would become
a problem if each struct had different members but these two don't!


Doesn't matter. These two structs are different because of their names.
Even if their members are identical, they are still apples and bananas
to the compiler.
Jonathan

Jul 23 '05 #3


in************* @yahoo.com wrote:
[snip]
*The Error Report I Get*

(structs.cpp: in function 'int main()':
structs.cpp:22: error: no match for 'operator=' in 'me1 = me'
structs.cpp:11: error: candidates are: main()::person1 &
main()::person1 ::operator=(con st main()::person1 &) That's the important error.
Why can't I assign a struct to a struct like i can an int to an int, or
a char to a char, etc ? I can understand it would become
a problem if each struct had different members but these two don't!


The problem is the same as trying to assign two variables of
incompatible types.Even though the variables have an identical layout,
the variable have different types.It's like trying to assign a 'float*'
to an 'int*'.They possibly have similar layout,but different meanings.A
person1 isn't a person,although they have the same members.

Hope I was helpful.

Jul 23 '05 #4
Could you give me a brief discription of
overloading the assignment operator ?
You've all been so helpful! :-)

Jul 23 '05 #5
in************* @yahoo.com wrote:
Could you give me a brief discription of
overloading the assignment operator ?
You've all been so helpful! :-)


struct B;
struct A {
A& operator=(B const&);
};

struct B { };

A& A::operator =(B const& b)
{
// do what you need to copy stuff from 'b' to '*this'
return *this;
}

int main() {
A a;
B b;
a = b;
}

V

Jul 23 '05 #6
struct B;
struct A {
A& operator=(B const&);
};

struct B { };
A& A::operator =(B const& b)
{
// do what you need to copy stuff from 'b' to '*this'
return *this;
}
int main() {
A a;
B b;
a = b;
}
V
This is great! Thanks so much! I'll study how you did that code snippet
second thing when I get up
tomorrow. I'll also look up operator overloading. Hey, u think we could
discuss loading an ascii text
file into a struct like say, struct phonebook{} ? That would be great,
I've always wanted to be able to do that and it would further my C++
career! You guys/gals have been great!

Jul 23 '05 #7
On 2005-07-19 20:01:20 +0100, in************* @yahoo.com said:
Hi, I would appreciate if someone could tell me what
I'm doing wrong ?
#include<iostre am>
using namepace std ;

struct person{
char *firstname ;
int age ;
} ;
struct person1{
char *firstname ;
int age ;
} ;

int main(int argc, char *argv[])
{
person me ;
person1 me1 ;

me.firstname = "me" ;
me.age = 150 ;

me1 = me ; // here's where the problem occurrs.....


The other posters have very valid points, but just two things:

If what you wrote, really, is what you meant, then you can
solve this "the old way" :

* (person *) &me1 = me ;

or using modern syntax:

* static_cast<per son *>(&me1) = me ;

The second thing is, did you really mean you wrote?
What's the purpose of defining two different structs
which are otherwise identical as to their members?

My take is what you need really is:

struct person {
char * name ;
int age ;
} ;

int main() {
person me ;
person me1 ;

me.name = "me" ;
me.age = 15 ;

me1 = me ;
}

--
JFB

Jul 23 '05 #8
Thanks JFB and again to everyone else who helped
me with this . I just got out of bed and was having dreams about
programming! That happens when I program right before I go to bed.
Since this has all sunk in now, I'm going to have to find a way to read
a text file into a struct because
I'm working on another program which requires it.
An example of that would be load people.dat
into struct person. I tried this, since you can do struct person me =
{"me",150}, I had the same
format in my text file. Comma separated values.
It gave me errors ... Think anyone could help with that issue as well ?
I love comp.lang.c++ !
thanks again!

Jul 23 '05 #9
in************* @yahoo.com wrote:

Thanks JFB and again to everyone else who helped
me with this . I just got out of bed and was having dreams about
programming! That happens when I program right before I go to bed.
Since this has all sunk in now, I'm going to have to find a way to read
a text file into a struct because
I'm working on another program which requires it.
An example of that would be load people.dat
into struct person. I tried this, since you can do struct person me =
{"me",150}, I had the same
format in my text file. Comma separated values.
It gave me errors ... Think anyone could help with that issue as well ?
I love comp.lang.c++ !
thanks again!


What exactly are your errors and what exactly does your program look like?
Just the part of the programming which deals with reading from file
would be enough. But make sure, that the thing you post is by itself
a complete program. That is: We can cut&paste it into our development
environments, compile it and see the same errors as you do (no missing
data types, no missing function declarations).
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #10

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

Similar topics

0
2306
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It includes support to pack and unpack the c types: byte, char, short, long, long long, char, *, and...
4
2508
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
1
2249
by: J | last post by:
I'm interfacing with a C api (via Interop) which uses the following typedef struct... typedef struct _columnflags { BYTE bNoUpdate : 1; BYTE bSetToNull : 1; BYTE bDefault : 1; } columnflags;
3
5625
by: 雪云鹰 | last post by:
Hi,all I have a trouble about struct variable,the detail is : I define a new struct which name ServiceProperty,then I declare a variable like this: ServiceProperty instService = null; but compile throw error: error CS0037: 无法将 NULL 转换成
26
7110
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
10
2286
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it for non-trivial tasks, I always happen to write small wrapper functions to adjust the values returned by struct. An example API would be the following: ============================================
5
7831
by: Orange | last post by:
hi, How to initialize the data variable. typedef struct { long len; BYTE data; }tag;
11
3790
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
9
2367
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of "using { Pen mypen = new Pen(Color.Black, 1)) {}), which I think makes a difference: I find the following assignment does not work: //myPenTest instantiated in the normal constructor, as was baseline,
0
9852
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
10902
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9420
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing, and deployment梬ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7014
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.