语言C++ 下面是完整的代码:为什么这个代码可以工作,但不能呢?
#include <iostream>
using namespace std;
struct part
{
int model_numb;
int part_numb;
float cost;
};
void main()
{
part part1, part2 = { 2022, 203, 2121.23F };
char dummy;
part1.model_numb = 12345;
part1.part_numb = 203;
part1.cost = 5050.30F;
cout << "******Part1*****" << endl;
cout << "\nModel Numb: " << part1.model_numb
<< "\nPart Numb: " << part1.part_numb
<< "\nCost: " << part1.cost << endl << endl;
cout << "******Part2*****" << endl;
cout << "\nModel Numb: " << part2.model_numb
<< "\nPart Numb: " << part2.part_numb
<< "\nCost: " << part2.cost << endl << endl;
part1 = part2;
cout << " part1 = part2" << endl << endl;
cout << "******Part1*****" << endl;
cout << "\nModel Numb: " << part1.model_numb
<< "\nPart Numb: " << part1.part_numb
<< "\nCost: " << part1.cost << endl << endl;
cout << "******Part2*****" << endl;
cout << "\nModel Numb: " << part2.model_numb
<< "\nPart Numb: " << part2.part_numb
<< "\nCost: " << part2.cost << endl << endl;
cin >> dummy;
}
如果我替换下面的行:
part part1, part2 = { 2022, 203, 2121.23F };
这些行:
part part1, part2;
part2 = { 2022, 203, 2121.23F };
的代码将无法正常工作。它给了我语法错误(在'{')之前缺少';'(在'}'之前缺少';')和(预期表达式)在这些行上。 WHYY ???除了上面提到的,我确定我没有对第二个程序做任何修改。
初始化和赋值是**不同**操作。 –
那是什么编译器? – billz
它适用于C++ 11 - http://ideone.com/3OUXRN – borisbn