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

标签 c++ g++ compiler-errors dev-c++

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

相关文章:

c++ - 使用 LIBUSB 的 USB 鼠标驱动程序

c++ - 测量分支被错误预测的频率

c++ - 从非 constexpression 初始化数组

使用自定义类类型作为键的 C++ unordered_map

visual-studio-2008 - 在友好类中声明的友元函数,GCC 不编译

linux - 如何将 8086 emu 汇编程序转换为 linux 汇编兼容

c++ - 返回设备 GUID 和 Bios ID、C++、Linux

c++ - std::cin 中的运算符 >> 不匹配

haskell - 无法理解简单的 Haskell 编译错误

C 编译器错误 - 初始值设定项不是常量