c ++构造函数中未知的编译错误

时间:2018-12-06 10:52:40

标签: c++

我无法在c ++中运行我的项目。我有很多错误,所以我不知道哪个错误是真实的。我正在添加代码和错误日志。

#include <iostream>
#include <map>
#include <set>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

class Restaurant {
    Menu menu;
    static const int NUMBER_TABLES = 10;
    set<int> occupiedTables;
    Table tables[NUMBER_TABLES];

    public: Restaurant() {
        for (int i = 0; i < NUMBER_TABLES; i++) {
            tables[i] = Table();
        }
    }

    public: Table getTable(int number) {
        return tables[number];
        }

    public:bool newCustomer(int tableNumber) {
        if (tableNumber < 1 || tableNumber > NUMBER_TABLES || occupiedTables.find(tableNumber) != occupiedTables.end())
            return false;
        tables[tableNumber].setOccupied(true);
        occupiedTables.insert(tableNumber);
        return true;
    }

    public:bool leaveCustomer(int tableNumber) {
        if (tableNumber < 1 || tableNumber > NUMBER_TABLES || occupiedTables.find(tableNumber) == occupiedTables.end())
            return false;
        tables[tableNumber].setOccupied(false);
        occupiedTables.erase(tableNumber);
        return true;
    }

    public: void addToOrder(int tableNumber, int meal_id, int pieces = 1) {
        for (int i = 0; i < pieces; i++) {
            tables[tableNumber].getOrder().setOrder(meal_id);
        }
    }
};

class Table {
    Order order;
    bool occupied = false;

    public: Order getOrder() {
            return order;
        }

    public: bool isOccupied() {
        return occupied;
        }

    public: bool setOccupied(bool occupied) {
        this->isOccupied = occupied;
    }
};

class FoodDrink{
    int id;
    string name;
    double price;
    int preparationTime;

public: FoodDrink() {};

public: FoodDrink(int id, string name, double price, int preparationTime) {
        this->id = id;
        this->name = name;
        this->price = price;
        this->preparationTime = preparationTime;
    }

public: int getId() {
    return id;
}

public: string getName() {
    return name;
}

public: double getPrice() {
    return price;
}

public: int getPreparationTime() {
    return preparationTime;
}

};

class Menu {
    static const int SOUP_NUMBER = 4;
    static const int MAINMEAL_NUMBER = 5;
    static const int SIDEDISH_NUMBER = 3;
    static const int DRINK_NUMBER = 4;
    static const int ALL_MEAL_NUMBER = SOUP_NUMBER + MAINMEAL_NUMBER + SIDEDISH_NUMBER + DRINK_NUMBER;
    FoodDrink allMeals[ALL_MEAL_NUMBER] =
    { FoodDrink(0, "Garlic soup", 1.49, 8), FoodDrink(1, "Vegetable soup", 1.59, 10),
    FoodDrink(2, "Chicken soup", 1.69, 9), FoodDrink(3, "Beef soup", 1.69, 11),
    FoodDrink(4, "Potato dumplings with sheep cheese", 4.95, 16), FoodDrink(5, "Chicken with niva sauce", 6.75, 19),
        FoodDrink(6, "Spicy pork ribs", 7.49, 23), FoodDrink(7, "Beef on mushrooms", 8.25, 22),
        FoodDrink(8, "Vegetable risotto", 4.99, 15), FoodDrink(9, "Chips", 1.79, 7), FoodDrink(10, "Potatos", 1.59, 12),
        FoodDrink(11, "Rice", 1.49, 11), FoodDrink(12, "Beer", 1.79, 2), FoodDrink(13, "Coca-cola", 1.59, 1),
        FoodDrink(14, "Orange juice", 1.99, 1), FoodDrink(15, "Mineral watter", 1.39, 1)
    };

public: FoodDrink getMeal(int id) {
    return allMeals[id];
}



public:void showMenu() {
    cout << "MENU" << endl;
    cout << setw(5) << "Id" << setw(40) << "Meal/drink" << setw(15) << "Price(eur)" << setw(20) << "Preparation(min)" << endl;
    cout << "---------------------------------------------------------------------------" << endl;
    cout << "Soups" << endl;
    for (auto i = 0; i < ALL_MEAL_NUMBER; i++) {
        if (i == SOUP_NUMBER)
            cout << "Main meals" << endl;
        else if (i == SOUP_NUMBER + MAINMEAL_NUMBER)
            cout << "Side dishes" << endl;
        else if (i == SOUP_NUMBER + MAINMEAL_NUMBER + SIDEDISH_NUMBER)
            cout << "Drinks" << endl;
        cout << setw(5) << allMeals[i].getId() << setw(40) << allMeals[i].getName() << setw(13)
            << allMeals[i].getPrice() << setw(12) << allMeals[i].getPreparationTime() << endl;
    }
    }
};

class Order {
    vector<FoodDrink> order;
    bool isPaid = false;
    Menu menu;

    public: void setOrder(int id){
        order.push_back(menu.getMeal(id));
    }


    public: double calculateOrder() {
        double total = 0;
        for (unsigned int i = 0; i < order.size(); i++) {
                total += order[i].getPrice();
        }
        return total;
    }

    public: void pay() {
        isPaid = true;
    }



};

    int main() {
        Menu m;
        Restaurant r;
        r.newCustomer(1);
        r.addToOrder(1, 5);
        r.addToOrder(1, 12, 2);
        cout << r.getTable(1).getOrder().calculateOrder() << endl;
    }

错误日志:

  

1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(11):
  错误C3646:“菜单”:未知的覆盖说明符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(11):
  错误C4430:缺少类型说明符-假定为int。注意:C ++不会   支持default-int
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(14):
  错误C3646:“表”:未知的覆盖说明符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(14):
  错误C2143:语法错误:在'['
前缺少','   1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(14):
  错误C2143:语法错误:';'之前缺少')'
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(14):
  错误C2238:';'之前的意外标记
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(22):
  错误C3646:“ getTable”:未知的覆盖说明符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(22):
  错误C2059:语法错误:'('
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(22):
  错误C2334:'{'之前的意外标记;明显跳过   功能体
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(18):
  错误C2065:“表”:未声明的标识符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(18):
  错误C3861:“表”:找不到标识符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(29):
  错误C2065:“表”:未声明的标识符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(37):
  错误C2065:“表”:未声明的标识符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(44):
  错误C2065:“表”:未声明的标识符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(50):
  错误C3646:“订单”:未知的替代说明符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(50):
  错误C4430:缺少类型说明符-假定为int。注意:C ++不会   支持default-int
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(53):
  错误C3646:“ getOrder”:未知的覆盖说明符
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(53):
  错误C2059:语法错误:'('
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(53):
  错误C2334:'{'之前的意外标记;明显跳过   功能体
  1> c:\ users \ jmize \ source \ repos \ restauracia \ restauracia \ source.cpp(62):
  错误C2659:'=':用作左操作数1>完成建筑项目
  “ Restauracia.vcxproj”-失败。

2 个答案:

答案 0 :(得分:1)

在将它们用作数据成员之前,需要定义类型,和/或在使用它们的指针或引用之前声明它们。

按以下顺序重新排列您的定义

class FoodDrink
class Menu
class Order
class Table
class Restaurant
int main()

请注意,每个类中不需要多个public:标记,访问描述符将一直适用,直到找到另一个。

答案 1 :(得分:1)

类声明的顺序不正确。这是正确的类声明顺序。 setOccupied函数也出错。

<header>
    <div class="container"> 
        <div class="header-top">
            <h1><a href=""></a> tik</h1>


            <nav>
                <a class="hvr-bob" href="">Home</a>
                <a class="hvr-bob" href="">Problems</a>
                <a class="hvr-bob" href="">Discussion</a>
                <a class="hvr-bob" href="">About</a>
           </nav>
           <section>
                    <a class="hvr-sweep-to-bottom" href="">Sign In</a>
                    <a class="hvr-sweep-to-bottom" href="">Register</a>
                </section>
            </div>
        </div>
</header>

body {
    font-family: Raleway, sans-serif;
}

header{
    background-image: radial-gradient(circle, #cbb695, #b4a78c, #9f9984, 
#8b897c, #7a7a74);
    padding: 1.5em 1em;
}
header h1 {
    margin: 0;
    font-size: 2em;
    font-weight: bold;
}
.container{
    max-width: 1200px;
    margin: 0 auto;
}
.header-top{
    display: flex;
    justify-content: space-between;
    font-weight: bold;
    align-items:center;
    color: white;

}
header a{
    color: white;
    padding-left: 5px ; 
    padding-right: 5px;
}