首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++ 结构类型的一些有关问题

2012-03-22 
C++ 结构类型的一些问题#include iostreamusing namespace std#include stringstruct Person{bool ge

C++ 结构类型的一些问题
#include <iostream>
using namespace std;
#include <string>

struct Person{bool gender;
string name;
int age;  
double salary;};

int main()
{  

struct Person p1;  
struct Person p2={false,"某",40,1234.5}; //错误就是这一行 错误代码为 error C2552: 'p2' : non-aggregates cannot be initialized with initializer list
Person p3 = p2;
p1.name="某";
p1.gender=true;
p1.age=40;
p1.salary=1234.5;
cout<<p1.name <<"是个"<<(p1.gender?"帅哥":"美女")<<",工资"<<p1.salary <<endl;

system("pause");


[解决办法]

C/C++ code
#include <iostream>#include <string>using namespace std;struct Person{    bool gender;    string name;    int age;            double salary;};int main(){       struct Person p1;       struct Person p2 = {false, "某", 40, 1234.5};    struct Person p3 = p2;    p1.name="某";    p1.gender=true;    p1.age=40;    p1.salary=1234.5;    cout<<p1.name <<"是个"<<(p1.gender?"帅哥":"美女")<<",工资"<<p1.salary <<endl;    system("pause");    return 0;}//输出:某是个帅哥,工资1234.5//VS2005,输出正常,请问你是用什么编译器?
[解决办法]
P2初始化方式换一下?编译器的问题吧。

热点排行