-2

I got some problem when run my coding. I got 2 separate file to create RetailItem class and create main. I create both in project.

Below are 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;

}

and there one more file 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;
}

when compile and run main,

  • [Error] a function-definition is not allowed here before '{' token

  • [Error] expected '}' at end of input

I don't know what to fix, how can I solve it?

sirandy
  • 1,834
  • 5
  • 27
  • 32
SisLove
  • 1
  • 1

1 Answers1

2

The error message undoubtedly contained a line number that told you where the problem was. That's an important part of describing the problem. But here it happens to be obvious: void displayItem(RetailItem *ritem, const int Item){ is the start of a function definition. You can't define a function inside another function. Move this outside of main.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165