注册 登录
编程论坛 C++教室

显示这行错误

hffjhhh 发布于 2020-11-30 02:18, 1375 次点击
显示这行错误:
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;
}
6 回复
#2
rjsp2020-11-30 08:24
字符串赋值用 strcpy
数组赋值(包括字符数组)用 memcpy
#3
rjsp2020-11-30 08:28
程序代码:
#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;
}
#4
hffjhhh2020-11-30 16:03
以下是引用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;
}
#5
rjsp2020-11-30 19:13
回复 4楼 hffjhhh
加上 #include <cstring>

再将 memcpy(total.maker,a->maker,8);
改为 memcpy(total.maker,a->maker,sizeof box::maker);
#6
hffjhhh2020-11-30 19:55
以下是引用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编辑过]

#7
rjsp2020-11-30 23:25
回复 6楼 hffjhhh
一样的,完全等价
1