| 
想请问各位大佬下面这段代码在Visual C++ 6.0运行不出来是错在哪里?报错也看不太懂
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 #include <iostream>
 using namespace std;
 #include <string>
 
 struct Student
 {
 string name;
 int age;
 int scores;
 };
 
 int main()
 {
 struct Student arr[3] =
 {
 {"张三",20,100},
 {"李四",30,90},
 {"王五",40,80}
 };
 
 arr[1].name = "赵六";
 arr[1].age = 50;
 arr[1].scores = 70;
 
 for (int i = 0; i < 3; i++)
 {
 cout << "姓名:" << arr[i].name
 << "年龄:" << arr[i].age
 << "分数:" << arr[i].scores << endl;
 }
 
 system("pause");
 return 0;
 }
 
 下面是报错内容:
 error C2440: 'initializing' : cannot convert from 'char [7]' to 'struct Student'
 No constructor could take the source type, or constructor overload resolution was ambiguous
 error C2440: 'initializing' : cannot convert from 'const int' to 'struct Student'
 No constructor could take the source type, or constructor overload resolution was ambiguous
 error C2440: 'initializing' : cannot convert from 'const int' to 'struct Student'
 No constructor could take the source type, or constructor overload resolution was ambiguous
 fatal error C1903: unable to recover from previous error(s); stopping compilation
 执行 cl.exe 时出错.
 
 本帖最后由 临时号 于 2021-7-24 00:45 编辑 
 你的错误在于Student这个结构中定义了string类型的成员,从而导致了不能用initializer list 进行初始化;string是std的定义的一个容器,从它的实现上看它含有一个保护成员,所以string不能用initializer list 进行初始化;所以你的程序出错了。当然,你的VC6.0太老了,我在Dev C++ 上编译就没问题
 |