#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