开发者

Invalid function declaration. DevC++

Why do I get invalid function declaration when I compile the code in DevC++ in Windows, but when I compile it in CodeBlocks on Linux it works fine.

#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
}


You're missing an assignment operator there:

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

Note that this is a C syntax though. You probably want to say

item sandwich("Sandwich", 3.00);

and add to item a constructor that takes a string and a double.


struct item sandwich{"Sandwich", 3.00}; is a usage of compound literal, which is legal in C (C99 standard) but not legal in C++ quite yet. But since most of the C++ compilers compile both C and C++ code, some decide to allow such structures in C++. Most do not though, not without special command line arguments.

So, for this to be legal and portable, you have to write a constructor for your item struct. This is easy, though

struct item {
    item(string const & name_, double price_) : name(name_), price(price_) {}
    string name;
    double price;
};

And now you can create new items with

item sandwich("Sandwich", 3.00);

P.S. Note, that I'd use named initializers in compound literals when you have fields with different meaning in single structure, it's just easier to understand what is what then.

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

That's not going to work in C++ as well, of course, but at least it looks better.

P.P.S. Turns out I wasn't paying enough attention to C++0x and there it's called initializer lists. Seems like you can't make it named though, it's a shame. So, instead of using C99 standard in C++ code, your Linux compiler has used C++0x experimental standards silently. Nevertheless, if you want crossplatform code, it's still better to stay away from those fancy features and use plain old constructors instead.


Dev-C++ uses an old version of the MinGW compiler. Using a newer version of gcc from the MinGW project that supports the extended initializer lists feature of C++0x (specifically the 4.4 series of gcc) should complain with a warning about the lines that are marked as erroneous:

testing.cc:14: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x testing.cc:15: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x testing.cc:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

My version of gcc 4.4.3 complained anyway... Not sure about yours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜