在“.”之前需要不合格的 ID令牌

Expected unqualified-id before '.' token

我对 C++ 还很陌生,所以请跟我谈谈穴居人。我试图在一个循环中将代码获取到 运行,当循环完成时计算总数和所有订购的东西。我 运行 遇到了这个错误,我不确定为什么。

[错误] 在“.”之前需要不合格的 ID代币

#include <iostream>
#include<string>
#include <vector>
using namespace std;

string again;
char pType, pSize, topping, temp;
const int SMALL = 1;
int type = 0, size = 0;
const int MEDIUM = 2;
const int LARGE = 3;
const int DEEPDISH = 1;
const int HANDTOSSED = 2;
const int PAN = 3;
double total = 0;

class Pizza
{
    private:
          int type;
          int size;
          bool cheese;    
          bool pepperoni; 

    public:
          Pizza();
          int getType();
          int getSize();
          bool getCheese();
          bool getPepperoni();
          void setType(int t);
          void setSize(int s);
          void setCheese(bool choice);
          void setPepperoni(bool choice);

  void outputDescription();
  double computePrice();
};

class Order
{
private:
    vector<Pizza> c; 

public:
    Order();
    void customerOrder();
    void customerTotal();
    void customerinput();
};

Pizza::Pizza()
{
  type = DEEPDISH;
  size = SMALL;
  cheese = pepperoni = false;
}

int Pizza::getType()
{
  return type;
}

int Pizza::getSize()
{
  return size;
}

bool Pizza::getCheese()
{
  return cheese;
}

bool Pizza::getPepperoni()
{
  return pepperoni;
}

void Pizza::setType(int t)
{
  type = t;
}

void Pizza::setSize(int s)
{
  size = s;
}

void Pizza::setCheese(bool choice)
{
  cheese = choice;
}

void Pizza::setPepperoni(bool choice)
{
  pepperoni= choice;
}

void Pizza::outputDescription()
{
  switch (size)
  {
      case SMALL:
        cout << "Small "; break;
      case MEDIUM:
        cout << "Medium "; break;
      case LARGE:
        cout << "Large "; break;
      default:
        cout << "Unknown" ;
  }

  switch (type)
  {
      case DEEPDISH:
        cout << "deepdish "; break;
      case HANDTOSSED:
        cout << "hand tossed "; break;
      case PAN:
        cout << "pan "; break;
      default:
        cout << "Unknown";
  }

  cout << "pizza";
}

double Pizza::computePrice()
{
  double cost = 0.0;
  switch (size)
  {
  case SMALL:
    cost += 10; break;
  case MEDIUM:
    cost += 14; break;
  case LARGE:
    cost += 17; break;
  }

  if (cheese)
    cost += 2.0;
  if (pepperoni)
    cost += 2.0;

  return cost;
}

Order custmizedTotal;
Pizza myPizza;  
bool done=false;
void Order::customerinput(){
while ( again == "y"){  
        cout << "What sized pizza, please enter S, M OR L: ";
        cin >> pSize;
        cin.clear();

        switch(pSize)
        {
            case 'S': case 's':
            size = SMALL; break;
            case 'M': case 'm':
            size = MEDIUM; break;
            case 'L': case 'l':
            size = LARGE; break;
        }

        cout << "What type of pizza, enter D for Deepdish, H for Hand tossed, and P for Pan: ";
        cin >> pType;
        cin.clear();

        switch(pType)
        {
            case 'D': case 'd':
            type = DEEPDISH; break;
            case 'H': case 'h':
            type = HANDTOSSED; break;
            case 'P': case 'p':
            type = PAN; break;
        }


        myPizza.setSize(size);
        myPizza.setType(type);

        cout << "Would you like cheese (y/n)? ";
        cin >> topping;
        cin.clear();

        if (topping == 'Y' || topping == 'y')
            myPizza.setCheese(true);

        cout << "Would you like pepperoni (y/n)? ";
        cin >> topping;
        cin.clear();

        if (topping == 'Y' || topping == 'y')
            myPizza.setPepperoni(true);


      cout << endl
       << "Your order: ";
      myPizza.outputDescription();
      cout << endl;
      cout << "Price: $" << myPizza.computePrice() << endl;

      cout << "Again? (y/n)";
      cin >> again;


    }
}

void Order::customerTotal(){

    cout << "Your Total order is: " << endl;
    for(int i=0; i<c.size(); i++)
    { 
        c[i].outputDescription();
        cout << endl;
        cout << c[i].computePrice();
        cout << endl;
        total=total+c[i].computePrice();
    }       
    cout << "Totat Cost: $" << total;
    cout << endl;
    c.push_back(myPizza);
}

int main()
{   
    custmizedTotal.customerinput();
    //Order.customerinput(); 
    if(again != "y"){
        custmizedTotal.customerTotal();
    }
    return 0;
}   

替换

int main(){
    Order.customerinput(); //error is here
    if(again != "y"){
        custmizedTotal.customerTotal(); 
    }
    return 0;
}   

作者:

int main(){
    custmizedTotal.customerinput(); // Change this line
    if(again != "y"){
        custmizedTotal.customerTotal(); 
    }
    return 0;
}   

由于您忘记定义 Order 构造函数而导致的第二个错误。

将此添加到您的代码中(在 main() 方法之上):

Order::Order(){
    // Set the initial values for order
}

您还忘记添加 customerOrder 方法(但这不会导致错误,因为您没有使用此方法):

void Order::customerOrder() {
}