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とをとるコンストラクターに追加しstringますdouble

于 2010-05-05T01:18:58.217 に答える
1

struct item sandwich{"Sandwich", 3.00};C (C99 標準) では合法ですが、C++ ではまだ合法ではありませ。しかし、ほとんどの C++ コンパイラは C コードと C++ コードの両方をコンパイルするため、C++ でそのような構造を許可することを決定する人もいます。ただし、特別なコマンドライン引数がなければ、ほとんどはそうではありません。

したがって、これを合法的かつ移植可能にするためには、item 構造体のコンストラクターを作成する必要があります。これは簡単ですが、

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 に十分な注意を払っていなかったことが判明し、それは初期化リストと呼ばれています。名前は出せないようですが、残念です。そのため、C++ コードで C99 標準を使用する代わりに、Linux コンパイラは C++0x の実験的な標準を黙って使用しています。とはいえ、クロスプラットフォーム コードが必要な場合は、これらの派手な機能を避けて、代わりに単純な古いコンストラクターを使用することをお勧めします。

于 2010-05-05T01:24:06.220 に答える
0

Dev-C++ は古いバージョンの MinGW コンパイラを使用しています。C++0x の拡張イニシャライザ リスト機能 (具体的には gcc の 4.4 シリーズ) をサポートする MinGW プロジェクトの新しいバージョンの 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 に答える