无效的函数声明。 DEVC ++

时间:2010-05-05 01:14:47

标签: 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
}

3 个答案:

答案 0 :(得分:1)

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

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

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

item sandwich("Sandwich", 3.00);

并添加item一个带有stringdouble的构造函数。

答案 1 :(得分:1)

struct item sandwich{"Sandwich", 3.00};compound literal的用法,在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);

<强> P.S。 请注意,当您在单个结构中具有不同含义的字段时,我会在复合文字中使用命名初始值设定项,它更容易理解当时的内容。

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

当然,这在C ++中也不会起作用,但至少它看起来更好。

<强> P.P.S。 事实证明我没有对C ++ 0x给予足够的重视,并且它被称为initializer lists。看起来你不能命名,但这是一种耻辱。 因此,您的Linux编译器不是在C ++代码中使用C99标准,而是默默地使用C ++ 0x实验标准。不过,如果你想要跨平台代码,那么最好远离那些花哨的功能,而是使用普通的旧构造函数。

答案 2 :(得分: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:warning:extended   初始化列表仅适用于   -std = c ++ 0x或-std = gnu ++ 0x

我的gcc 4.4.3版本仍抱怨......不确定你的。