Определение Fuction не разрешено. RetailItem - PullRequest
0 голосов
/ 07 декабря 2018

У меня возникла проблема при запуске моего кодирования.Я получил 2 отдельных файла для создания класса RetailItem и создания основного.Я создаю оба в проекте.

Ниже приведены main.cpp

//main 
#include "retailitem.h"
#include <iostream>
#include <iomanip>

using namespace std;
using std::cout;
void displayItem(RetailItem *, const int);
int main()
{
   const int Item = 3;
   RetailItem ritem[Item] ={ { "Jacket", 12, 59.95 },
                          { "Designer Jeans", 40, 34.95 },
                          { "Shirt", 20, 24.95 } };

   //cout << fixed << setprecision(2);
    void displayItem(RetailItem *ritem, const int Item){

  cout <<"      DESCRIPTION UNITS ON HAND   PRICE";

cout<<"=================================================================\n";

for (int i = 0; i < Item; i++)
{
    cout << setw(12) << ritem[i].getDesc();
    cout << setw(12) << ritem[i].getUnits();
    cout << setw(8) << ritem[i].getPrice();

}

cout << "===================================================================";
}
return 0;

}

и еще один файл retailitem.h

//RetailItem class
#include <string>

using namespace std;

class RetailItem
{
private:
    string description;
    int unitsOnHand;
    double price;

public:
RetailItem(string,int,double);
    void setDesc(string d);
    void setUnits(int u);
    void setPrice(double p);

    string getDesc();
    int getUnits();
    double getPrice();
};
RetailItem::RetailItem(string desc, int units, double cost)
   {
       description = desc;
       unitsOnHand = units;
       price = cost;
   }
void RetailItem::setDesc(string d)
{
description = d;
}
void RetailItem::setUnits(int u)
{
unitsOnHand = u;
}
void RetailItem::setPrice(double p)
{
price = p;
}

string RetailItem::getDesc()
{
return description;
}
int RetailItem::getUnits()
{
return unitsOnHand;
}
double RetailItem::getPrice()
{
return price;
}

при компиляции и запуске main,

  • [Ошибка] определение функции здесь не допускается до '{' токена

  • [Ошибка] ожидается '}' в конце ввода

Не знаю, что исправить, как я могу это решить?

1 Ответ

0 голосов
/ 07 декабря 2018

Сообщение об ошибке, несомненно, содержало номер строки , в котором говорилось, в чем проблема.Это важная часть описания проблемы.Но здесь это очевидно: void displayItem(RetailItem *ritem, const int Item){ - начало определения функции.Вы не можете определить функцию внутри другой функции.Переместите это за пределы main.

...