2016-03-08 70 views
1

为什么我的程序得到这些错误?未定义的引用错误:`ClassName :: ClassMember`

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

可能重复[什么是未定义的引用/无法解析的外部符号错误,以及如何解决它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-外部符号错误和怎么办我修复) –

回答

1

静态成员变量的定义是错误的,他们应该是:

const string PizzaOrder::defaultSize = "Small "; 
      ~~~~~~~~~~~~ 
... 

Otherwize他们会只是全局变量。

+0

我做到了,但现在它给我在运行时的另一个错误。在抛出'std :: length_error'实例后终止调用 what():basic_string :: _ S_create –

+0

@FarhanIshraq您可以使用调试器来找出运行时错误的原因。无论如何,这是另一个问题。 – songyuanyao