为什么我的程序会收到这些错误?
undefined reference to `PizzaOrder::toppings_offered'
undefined reference to `PizzaOrder::arraySize'
undefined reference to `PizzaOrder::base_price'
undefined reference to `PizzaOrder::MedPizBase'
undefined reference to `PizzaOrder::base_price'
undefined reference to `PizzaOrder::LargePizBase'
...
这是我的计划:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class PizzaOrder
{
private:
int size;
int num_toppings;
string toppings[100];
public:
static const string toppings_offered[];
static const double topping_base_cost;
static const double base_price;
static const string defaultSize;
static const double MedPizBase;
static const double LargePizBase;
static const int arraySize;
PizzaOrder();
PizzaOrder(int size);
// MUTATORS & ACCESSORS & METHODS
int GetSize() { return size; }
int GetNumToppings() { return num_toppings; }
string GetToppings();
void GetPrice();
bool SetSize(int size);
void AddTopping(string topping);
void AddTopping(int n); //int represents position in array toppings_offered[]
string StringizeSize(int size);
void DisplayPizza();
};
const string toppings_offered[] = {"1. Onions", "2. Bell Peppers",
"3. Olives", "4. Pepperoni", "5. Sausage",
"6. Mushrooms", "7. Jalapenos"};
const string defaultSize = "Small ";
const double topping_base_cost = 1;
const double base_price = 7;
const double MedPizBase = 0.15;
const double LargePizBase = 0.25;
const int arraySize = sizeof(toppings_offered)/sizeof(toppings_offered[0]);
PizzaOrder::PizzaOrder()
{
SetSize(0);
}
PizzaOrder::PizzaOrder(int size)
{
if (!SetSize(size))
SetSize(0);
}
string PizzaOrder::StringizeSize(int size)
{
string pizzaSize;
if (size == 0)
pizzaSize = "Small";
if (size == 1)
pizzaSize = "Medium";
if (size == 2)
pizzaSize = "Large";
return pizzaSize;
}
bool PizzaOrder::SetSize(int size)
{
if (size != 0 && size != 1
&& size != 2)
return false;
this->size = size;
return true;
}
void PizzaOrder::AddTopping(string topping) // totally wrong
{
for (int i = 0; i < arraySize; i++)
{
if (topping == toppings_offered[i])
{
toppings[num_toppings] = topping;
num_toppings++;
}
}
}
void PizzaOrder::AddTopping(int n) //increments n by 1 for each valid topping chosen
{
if (n > 0 && n < 7)
n++;
n = num_toppings;
}
string PizzaOrder::GetToppings()
{
string result;
for(int i = 0; i < GetNumToppings(); i++)
{
result += toppings[i];
}
return result;
}
void PizzaOrder::DisplayPizza()
{
cout << "Your pizza order: " << PizzaOrder::StringizeSize(GetSize()) << ", "
<< PizzaOrder::GetNumToppings() << ", "
<< PizzaOrder::GetToppings();
}
void PizzaOrder::GetPrice()
{
double TotalPizPrice;
double MedPizzaPrice = base_price+(base_price*MedPizBase);
double LargePizzaPrice = base_price+(base_price*LargePizBase);
double topPrice = (GetNumToppings()*topping_base_cost);
if (GetSize() == 0)
TotalPizPrice = topPrice+base_price;
if (GetSize() == 1)
TotalPizPrice = topPrice+MedPizzaPrice;
if (GetSize() == 2)
TotalPizPrice = topPrice+LargePizzaPrice;
cout << "Your pizza's total price is: $" << TotalPizPrice;
}
int main()
{
PizzaOrder pizza;
string choice;
char selection;
int topChoice;
do
{
cout << "Size of pizza (Small, Medium, Large) or Quit?\n" << endl;
getline(cin, choice);
selection = choice[0];
if (selection == 'S' || selection == 's')
pizza.SetSize(0);
if (selection == 'M' || selection == 'm')
pizza.SetSize(1);
if (selection == 'L' || selection == 'l')
pizza.SetSize(2);
do
{
cout << "Current pizza: "
<< pizza.StringizeSize(pizza.GetSize())
<< pizza.GetToppings() << "\n"
<< "Select an item by number (0 when done):\n" << endl;
for (int i = 0; i < 8; i++)
{
cout << toppings_offered[i] << "\n";
}
cout <<"\nSelection: ";
cin >> topChoice;
pizza.AddTopping(topChoice);
}
while (topChoice != 0);
}
while(selection != 'q' && selection != 'Q');
pizza.DisplayPizza();
pizza.GetPrice();
return 0;
}
答案 0 :(得分:1)
静态成员变量的定义是错误的,它们应该是:
const string PizzaOrder::defaultSize = "Small ";
~~~~~~~~~~~~
...
另外,他们将只是全局变量。