将struct变量分配给另一个struct变量 [英] assign a struct variable into another struct variable

查看:86
本文介绍了将struct变量分配给另一个struct变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果有人能告诉我什么,我会很感激

我做错了吗?

#包含< iostream>

使用namepace std;


struct person {

char * firstname;

int age;

};


struct person1 {

char * firstname;

int年龄;

};


int main(int argc,char * argv [])

{

person me;

person1 me1;


me.firstname =" me" ;

me.age = 150;


me1 = me; //这里是问题发生的地方.....


*我得到的错误报告*


(structs.cpp :在函数''int main()''中:

structs.cpp:22:错误:在''me1 = me'中没有匹配''operator ='''

structs.cpp:11:错误:候选者是:main():: person1&

main():: person1 :: operator =(const main():: person1&)

structs.cpp:29:1:警告:文件末尾没有换行




为什么我不能为结构分配一个结构,比如我可以将int转换为int,或者将
a char分配给char等等?我能理解,如果每个结构都有不同的成员,那么这将成为问题。但是这两个人不会!


比你多得多

>



Hi, I would appreciate if someone could tell me what
I''m doing wrong ?
#include<iostream>
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=(const 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

推荐答案

**** *********@yahoo.com 写道:
如果有人能告诉我什么,我会很感激
我做错了吗? br />
#include< iostream>
使用namepace std;

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


两种类型,''person''和''person1''是_distinct_和_different_

AFA C ++。他们的内部是相同的

并不重要。由于你定义了两种不同的类型,它们是不同的。

int main(int argc,char * argv [])
{
我是人;
person1 me1;

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

me1 = me; //这里是问题发生的地方.....

*我得到的错误报告*

(structs.cpp:在函数''int main( )'':
structs.cpp:22:错误:''运算符=''匹配''me1 = me''
structs.cpp:11:错误:候选者是:main( ):: person1&
main():: person1 :: operator =(const main():: person1&)
structs.cpp:29:1:警告:文件末尾没有换行符


为什么我不能给结构分配一个结构,比如我可以将int转换为int,或者将char转换为char等等?我可以理解,如果每个结构都有不同的成员,但这两个都不会成为问题!
Hi, I would appreciate if someone could tell me what
I''m doing wrong ?
#include<iostream>
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=(const 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!




你需要定义(重载) ''person''的赋值运算符

允许从''person1''赋值。阅读你最喜欢的C ++书中的运算符重载



V



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


中的mailto:在************* @ yahoo.com>写道:​​
in*************@yahoo.com wrote:
我很感激,如果有人能告诉我是什么
我做错了吗?

#include< iostream>
使用namepace std;

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

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

int main(int argc,char * argv [])
{
我是人;
person1 me1 ;

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

me1 = me; //这里是问题发生的地方.....

*我得到的错误报告*

(structs.cpp:在函数''int main( )'':
structs.cpp:22:错误:''运算符=''匹配''me1 = me''
structs.cpp:11:错误:候选者是:main( ):: person1&
main():: person1 :: operator =(const main():: person1&)
structs.cpp:29:1:警告:文件末尾没有换行符


为什么我不能给结构分配一个结构,比如我可以将int转换为int,或者将char转换为char等等?我能理解,如果每个结构都有不同的成员,那么这将成为一个问题,但这两个不是!
Hi, I would appreciate if someone could tell me what
I''m doing wrong ?
#include<iostream>
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=(const 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!




无所谓。这两个结构因其名称而不同。

即使他们的成员相同,他们仍然是苹果和香蕉

给编译器。

Jonathan



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




在************* @ yahoo.com 上写道:

[snip]


in*************@yahoo.com wrote:
[snip]
*我得到的错误报告*

(structs.cpp:在函数''int main()''中:
structs.cpp:22:错误:''operator ='不匹配'in''me1 = me''
structs.cpp:11:错误:候选人是:main():: person1&
main():: person1 :: operator =(const main() :: person1&)
这是重要的错误。

为什么我不能将结构分配给结构,就像我可以将int转换为int,或者
char to char等?我能理解,如果每个结构都有不同的成员,那么这将成为一个问题,但这两个不是!
*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=(const 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!




问题是s因为尝试分配两个变量

不兼容的类型。即使变量具有相同的布局,

变量有不同的类型。这就像尝试分配一个''浮动*''

到一个''int *''。它们可能有类似的布局,但意义不同.A

person1不是一个人,虽然他们有相同的成员。


希望我能提供帮助。



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.


这篇关于将struct变量分配给另一个struct变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆