gpt4 book ai didi

c++ - 函数声明无效。开发C++

转载 作者:行者123 更新时间:2023-11-28 03:57:46 24 4
gpt4 key购买 nike

为什么我在 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
}

最佳答案

你在那里缺少赋值运算符:

struct item sandwich = {"Sandwich", 3.00};

请注意,这是 C 语法。你可能想说

item sandwich("Sandwich", 3.00);

并向 item 添加一个构造函数,该构造函数接受一个 string 和一个 double

关于c++ - 函数声明无效。开发C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2769990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com