我找不到適合這個問題的主題,因為我沒有收到正確的錯誤訊息。
我正在嘗試為一家主要提供比薩餅和其他食物(意大利面、雞翅等)的餐廳創建一個管理系統。我希望這個系統被作業人員使用。我創建了一個抽象類,命名為Foods可以被其他食物繼承。到目前為止,我已經創建了一個繼承自Foodsnamed的類Pizza。下面是我的代碼。
PS:我曾經分別用于namespaces整理食物和作業人員。據我所知,有些人不推薦namespace,如果您是其中之一,我深表歉意。
介面.h
#include <vector>
#include <string>
namespace foods{
class Food{
double price;
// since the sauces and drinks are given with foods.
static const std::vector<std::string> sauces;
static const std::vector<std::string> drinks;
public:
virtual int retPrice() = 0;
virtual void ask() = 0; // ask user what to add
virtual ~Food() = default;
};
const std::vector<std::string> Food::sauces = {"Blue cheese", "Garlic", "Honey BBQ", "Marinara"};
const std::vector<std::string> Food::drinks = {"Pepsi", "Mountain Dew", "Coca Cola"};
class Pizza: public Food{
const double price;
const std::string pizzaType; // whether it is chicken, beef, etc.
const std::string size; // small, medium or large
int crust = 1; // how crust it is from 1-5
std::vector<std::string> toppings; // to store toppings
public:
Pizza(): price(15), pizzaType(" "), size(" "){}
int retPrice() override; // the price should change according to the type
void ask() override; // ask the customer for a pizza
void createACustom(); // create a custom pizza with desired toppings
};
};
函式.cpp
#include <iostream>
#include "interfaces.h"
namespace foods{
int Pizza::retPrice(){
return (price 5);
}
void Pizza::ask(){
std::cout << "Hello World!";
}
}
測驗.cpp
#include "interfaces.h"
int main(){
foods::Pizza* pizza = new foods::Pizza();
}
而且我收到以下錯誤。
/usr/bin/ld: /tmp/ccQRR5B8.o: warning: relocation against `_ZTVN5foods5PizzaE' in read-only section `.text._ZN5foods5PizzaC2Ev[_ZN5foods5PizzaC5Ev]'
/usr/bin/ld: /tmp/ccQRR5B8.o: in function `foods::Pizza::Pizza()':
test.cpp:(.text._ZN5foods5PizzaC2Ev[_ZN5foods5PizzaC5Ev] 0x2b): undefined reference to `vtable for foods::Pizza'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
我嘗試使用關鍵字override并創建了一個默認解構器,但似乎沒有任何效果。我想知道此錯誤訊息的含義以及解決方案。除此之外還有什么vtable?
感謝您的時間和答案。
編輯 1
我用 編譯它g -Wall -Wextra test.cpp functions.cpp -o test,這是錯誤的,然后我做了g -Wall -Wextra test.cpp functions.cpp -o test,我得到了以下錯誤。
/usr/bin/ld: /tmp/ccmv2G17.o:(.bss 0x0): multiple definition of `foods::Food::sauces[abi:cxx11]'; /tmp/ccuBNQjX.o:(.bss 0x0): first defined here
/usr/bin/ld: /tmp/ccmv2G17.o:(.bss 0x20): multiple definition of `foods::Food::drinks[abi:cxx11]'; /tmp/ccuBNQjX.o:(.bss 0x20): first defined here
collect2: error: ld returned 1 exit status
為什么說它有多個定義?
uj5u.com熱心網友回復:
您需要實作靜態成員變數sauces和drinksinfunctions.cpp而不是 in interfaces.h。
函式.cpp
namespace foods {
int Pizza::retPrice() {
return (price 5);
}
void Pizza::ask() {
std::cout << "Hello World!";
}
// implement the static variables here.
const std::vector<std::string> Food::sauces = { "Blue cheese", "Garlic", "Honey BBQ", "Marinara" };
const std::vector<std::string> Food::drinks = { "Pepsi", "Mountain Dew", "Coca Cola" };
}
并將它們從interfaces.h.
如果您在其中實作它們,interfaces.h它們最終會在包含 .cpp 的每個 .cpp 檔案中實作interfaces.h。
這與在 .h 檔案中定義全域變數基本相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429025.html
上一篇:使用python-attrs模塊時使用子類啟動基的問題
下一篇:確定覆寫特定方法的類
