0

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

3 回答 3

1

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

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

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

item sandwich("Sandwich", 3.00);

并添加到item采用 astring和 a的构造函数中double

于 2010-05-05T01:18:58.217 回答
1

struct item sandwich{"Sandwich", 3.00};复合文字的用法,这在 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);

PS 注意,当您在单个结构中具有不同含义的字段时,我会在复合文字中使用命名初始化器,这样更容易理解什么是什么。

struct item sandwich = {.name = "Sandwich", .price = 3.0};

当然,这在 C++ 中也行不通,但至少看起来更好。

PPS 原来我没有对 C++0x 给予足够的关注,它被称为初始化列表。好像你不能让它命名,这是一个耻辱。因此,您的 Linux 编译器没有在 C++ 代码中使用 C99 标准,而是默默地使用了 C++0x 实验标准。不过,如果您想要跨平台代码,最好还是远离那些花哨的功能,而改用普通的旧构造函数。

于 2010-05-05T01:24:06.220 回答
0

Dev-C++ 使用旧版本的 MinGW 编译器。使用 MinGW 项目中支持 C++0x 的扩展初始化列表功能(特别是 4.4 系列的 gcc)的更新版本的 gcc 应该抱怨关于标记为错误的行的警告:

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 版本抱怨......不确定你的。

于 2010-05-05T01:36:31.053 回答