标题:显示这行错误
只看楼主
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
结帖率:90.38%
已结贴  问题点数:10 回复次数:6 
显示这行错误
显示这行错误:
total.maker=a->maker;

错误信息为:
[Error] invalid array assignment
如何修改,代码如下:
程序代码:
#include<iostream>
using namespace std;
struct box{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void function(box a){
    cout<<a.maker<<endl<<a.height<<endl<<a.width<<endl<<a.length<<endl<<a.volume<<endl<<endl;
}
box function1(box *a){
    box total;
    total.maker=a->maker;
    total.height=a->height;
    total.width=a->width;
    total.length=a->length;
    total.volume=(a->height*a->width*a->length);
    return total;
}
int main(void){
    box aa={"asfdji",2.3,5.6,4.7,7.8};
    function(aa);
    box bb=function1(&aa);
    cout<<bb.maker<<endl<<bb.height<<endl<<bb.width<<endl<<bb.length<<endl<<bb.volume;
    return 0;
}
搜索更多相关主题的帖子: 显示 float box total length 
2020-11-30 02:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
字符串赋值用 strcpy
数组赋值(包括字符数组)用 memcpy
2020-11-30 08:24
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
程序代码:
#include <iostream>

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

std::ostream& operator<<( std::ostream& os, const box& a )
{
    return os << a.maker  << '\n'
                << a.height << '\n'
                << a.width  << '\n'
                << a.length << '\n'
                << a.volume << '\n';
}

using namespace std;

int main( void )
{
    box a = { "asfdji", 2.3f, 5.6f, 4.7f, 7.8f };
    cout << a << endl;

    box b = a;
    cout << b << endl;
}
2020-11-30 08:28
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
得分:0 
以下是引用rjsp在2020-11-30 08:28:35的发言:

#include <iostream>

struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

std::ostream& operator<<( std::ostream& os, const box& a )
{
    return os << a.maker  << '\n'
                << a.height << '\n'
                << a.width  << '\n'
                << a.length << '\n'
                << a.volume << '\n';
}

using namespace std;

int main( void )
{
    box a = { "asfdji", 2.3f, 5.6f, 4.7f, 7.8f };
    cout << a << endl;

    box b = a;
    cout << b << endl;
}

这个方法虽然可行,但是这一行看不懂:
std::ostream& operator<<( std::ostream& os, const box& a )

对于新手来说,我希望用更加基本的方法来做,修改如下:
程序代码:
#include<iostream>
using namespace std;
struct box{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void function(box a){
    cout<<a.maker<<endl<<a.height<<endl<<a.width<<endl<<a.length<<endl<<a.volume<<endl<<endl;
}
box function1(box *a){
    box total;
    memcpy(total.maker,a->maker,8);
    total.height=a->height;
    total.width=a->width;
    total.length=a->length;
    total.volume=(a->height*a->width*a->length);
    return total;
}
int main(void){
    box aa={"asfdji",2.3,5.6,4.7,7.8};
    function(aa);
    box bb=function1(&aa);
    cout<<bb.maker<<endl<<bb.height<<endl<<bb.width<<endl<<bb.length<<endl<<bb.volume;
    return 0;
}
2020-11-30 16:03
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
回复 4楼 hffjhhh
加上 #include <cstring>

再将 memcpy(total.maker,a->maker,8);
改为 memcpy(total.maker,a->maker,sizeof box::maker);
2020-11-30 19:13
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
得分:0 
以下是引用rjsp在2020-11-30 19:13:25的发言:

加上 #include <cstring>

再将 memcpy(total.maker,a->maker,8);
改为 memcpy(total.maker,a->maker,sizeof box::maker);

发现将 memcpy(total.maker,a->maker,8)改为:
memcpy(total.maker,a->maker,sizeof box::maker);

或:
memcpy(total.maker,a->maker,sizeof(a->maker));

或:
memcpy(total.maker,a->maker,sizeof(total.maker));

编译器都没有报错,是不是这几种表示方法都可以?

[此贴子已经被作者于2020-11-30 21:46编辑过]

2020-11-30 19:55
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
回复 6楼 hffjhhh
一样的,完全等价
2020-11-30 23:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-504067-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.128613 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved