函数声明无效。 DevC++
为什么我在Windows中使用DevC++编译代码时遇到无效函数声明,但是当我在Linux上的CodeBlocks中编译它时,它工作正常。函数声明无效。 DevC++
#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
}
你错过了一个赋值操作符有:
struct item sandwich = {"Sandwich", 3.00};
注意,这是一个C语法虽然。你可能要说
item sandwich("Sandwich", 3.00);
,并加入到item
一个构造函数一个string
和double
。
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);
附注:创建新项目 请注意,当在单个结构中具有不同含义的字段时,我会在复合文字中使用命名的初始值设定项,它只是更容易理解那是什么。
struct item sandwich = {.name = "Sandwich", .price = 3.0};
这当然也不会在C++中起作用,但至少它看起来更好。
P.P.S. 原来我对C++ 0x没有足够的重视,在那里它被称为initializer lists。似乎你不能命名它,这是一个耻辱。 因此,您的Linux编译器不是在C++代码中使用C99标准,而是无声地使用了C++ 0x实验标准。尽管如此,如果你想跨平台的代码,最好远离那些奇特的特性,而使用普通的构造函数。
Dev-C++使用旧版本的MinGW编译器。
:使用从支持的C++ 0x(特别是4.4系列的gcc)的扩展初始化列表特征MinGW的项目的gcc较新的版本应该与关于被标记为错误的行警告抱怨testing.cc:14:warning:extended 初始值设定项列表仅适用于 -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版本无论如何都抱怨...不知道你的。
大声笑_“这不会奏效......但至少看起来更好”_ – wilhelmtell 2010-05-05 01:34:48