• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

敌不过“运算符=”(操作数的类型是“书”和“<大括号包围的初始化列表>”)

c/c++ 来源:shandude 3次浏览
#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已经在早些时候宣布。您需要在变量声明点初始化:

struct book 
    { int bookid; 
     char title[20]; 
     float price; 
    } b2 = {100,"c++ by saurabh",105.2}; //values initialised during variable declaration 

int main() 
{ 
    cout<<"\n"<<b2.bookid; 
    cout<<b2.title<<" "<<b2.price; 
    return 0; 
    getch(); 
} 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)