为什么当我在Windows下用DevC++编译代码时得到无效的函数声明,但是当我在Linux上用CodeBlocks编译它时它工作得很好。
#include <iostream>
#include <vector>
using namespace std;
//structure to hold item information
struct item{
string name;
double price;
};
//define sandwich, chips, and drink
struct item sandwich{"Sandwich", 3.00}; **** error is here *****
struct item chips{"Chips", 1.50}; **** error is here *****
struct item drink{"Large Drink", 2.00}; **** error is here *****
vector<item> cart; //vector to hold the items
double total = 0.0; //total
const double tax = 0.0825; //tax
//gets item choice from user
char getChoice(){
cout << "Select an item:" << endl;
cout << "S: Sandwich. $3.00" << endl;
cout << "C: Chips. $1.50" << endl;
cout << "D: Drink. $2.00" << endl;
cout << "X: Cancel. Start over" << endl;
cout << "T: Total" << endl;
char choice;
cin >> choice;
return choice;
}
//displays current items in cart and total
void displayCart(){
cout << "\nCart:" << endl;
for(unsigned int i=0; i<cart.size(); i++){
cout << cart.at(i).name << ". $" << cart.at(i).price << endl;
}
cout << "Total: $" << total << endl << endl;
}
//adds item to the cart
void addItem(struct item bought){
cart.push_back(bought);
total += bought.price;
displayCart();
}
//displays the receipt, items, prices, subtotal, taxes, and total
void displayReceipt(){
cout << "\nReceipt:" << endl;
cout << "Items: " << cart.size() << endl;
for(unsigned int i=0; i<cart.size(); i++){
cout << (i+1) << ". " << cart.at(i).name << ". $" << cart.at(i).price << endl;
}
cout << "----------------------------" << endl;
cout << "Subtotal: $" << total << endl;
double taxes = total*tax;
cout << "Tax: $" << taxes << endl;
cout << "Total: $" << (total + taxes) << endl;
}
int main(){
//sentinel to stop the loop
bool stop = false;
char choice;
while (stop == false ){
choice = getChoice();
//add sandwich
if( choice == 's' || choice == 'S' ){
addItem(sandwich);
}
//add chips
else if( choice == 'c' || choice == 'C' ){
addItem(chips);
}
//add drink
else if( choice == 'd' || choice == 'D' ){
addItem(drink);
}
//remove everything from cart
else if( choice == 'x' || choice == 'X' ){
cart.clear();
total = 0.0;
cout << "\n***** Transcation Canceled *****\n" << endl;
}
//calcualte total
else if( choice == 't' || choice == 'T' ){
displayReceipt();
stop = true;
}
//or wront item picked
else{
cout << choice << " is not a valid choice. Try again\n" << endl;
}
}//end while loop
return 0;
//end of program
}发布于 2010-05-05 09:18:58
这里缺少一个赋值运算符:
struct item sandwich = {"Sandwich", 3.00};请注意,这是一种C语法。你可能想说
item sandwich("Sandwich", 3.00);并向item添加一个接受string和double参数的构造函数。
发布于 2010-05-05 09:24:06
struct item sandwich{"Sandwich", 3.00};是compound literal的一种用法,它在C (C99标准)中是合法的,但在C++中还不合法。但由于大多数C++编译器同时编译C和C++代码,因此一些编译器决定在C++中允许这样的结构。但是,如果没有特殊的命令行参数,大多数都不会。
因此,为了使其合法和可移植,您必须为您的项结构编写一个构造函数。不过,这很简单。
struct item {
item(string const & name_, double price_) : name(name_), price(price_) {}
string name;
double price;
};现在,您可以使用以下命令创建新项目
item sandwich("Sandwich", 3.00);P.S.注意,当你在单个结构中有不同含义的字段时,我会在复合文字中使用命名初始值设定项,这样更容易理解什么是什么。
struct item sandwich = {.name = "Sandwich", .price = 3.0};当然,这在C++中也行不通,但至少它看起来更好。
P.P.S.发现我没有对C++0x给予足够的关注,于是它被称为initializer lists。看起来你不能给它命名,这是个遗憾。因此,您的Linux编译器没有在C++代码中使用C99标准,而是默默地使用了C++0x实验标准。然而,如果你想要跨平台的代码,最好还是远离那些花哨的特性,而使用普通的老式构造函数。
发布于 2010-05-05 09:36:31
Dev-C++使用旧版本的MinGW编译器。使用支持C++0x扩展初始化器列表功能的MinGW项目中的较新版本的gcc (特别是C++0x的4.4系列),应该会对标记为错误的行发出警告:
testing.cc:14:警告:扩展初始值设定项列表仅适用于-std=c++0x或-std=gnu++0x testing.cc:15:警告:扩展初始值设定项列表仅适用于-std=c++0x或-std=gnu++0x testing.cc:16:警告:扩展初始值设定项列表仅适用于-std=c++0x或-std=gnu++0x
我的版本的gcc 4.4.3抱怨不管怎样…对你的不太确定。
https://stackoverflow.com/questions/2769990
复制相似问题