#include<iostream>
#include <conio.h>
using namespace std;
struct book
{ int bookid;
char title[20];
float price;
}b2;
int main()
{
b2={100,"c++ by saurabh",105.2}; //values initialised during variable declaration
cout<<"\n"<<b2.bookid;
cout<<b2.title<<" "<<b2.price;
return 0;
getch();
}
这上面的代码显示在输出误差这样的:敌不过“运算符=”(操作数的类型是“书”和“<大括号包围的初始化列表>”)
C:\Users\shandude\Documents\codeblock\cprog\struct2.cpp|13|error: no match for 'operator=' (operand types are 'book' and '')|
C:\Users\shandude\Documents\codeblock\cprog\struct2.cpp|5|note: no known conversion for argument 1 from '' to 'const book&'|
你觉得'b2 = {100,“C++ by saurabh”,105.2}; '应该这样做? – Galen
关闭主题,但最后两行'return 0;'和'getch();'应该颠倒过来。 'getch();'什么都不做。应用程序将在此行被调用之前返回。另外:在全局范围内使用名称空间标准不是一个好主意,也不是好的做法。 –
应该编写_initialize_'b2'(创建'b2'的一部分)还是给'b2(创建后的值)赋一个值? – chux