Unknown error when trying to add elements to an array of my structwithin my class.

T

tugnutt7

Ok so basically my spec file is set up as follows:
#ifndef INVENTORY
#define INVENTORY
#include <string>
using namespace std;
struct Product
{
int prodCode;
string description;
double price;
};

class Inventory
{
private:
Product items[25];
int maxItems;
int curNoProd;
int findProduct(int /*Product Code*/);
public:
Inventory(string /*File Name*/, int /*Max Items*/);
void addProduct(int /*Product Code*/, string /*Description*/,
double /*Price*/);
void displayProduct(int /*Product Code*/);
void writeToFile(ofstream&);
bool isArrayFull(){return (curNoProd == maxItems);};
int getCurrentNoElems() const {return curNoProd;};
void increasePrice(int /*Product Code*/, double /*Price
Increase*/);
};
#endif


Now in my implementation file i have most of the function defined
however my problem occurs when trying to define my addProduct and
increasePrice functions. Currently i have this:
void Inventory::addProduct(int newProductCode, string newDescription,
double newPrice)
{
if (newPrice <= 0 || newPrice > 50)
cout<<"Price is out of range, must be greater than 0, with a
max of 50"<<endl;
else if (newProductCode < 10000 || newProductCode > 99999)
cout<<"Product code must be a 5 digit number."<<endl;
else
{
int location = findProduct(newProductCode);
if (location != curNoProd)
cout<<"Product "<<newProductCode<<" already
exists."<<endl;
else if (isArrayFull())
cout<<"No more room for items, current max is
"<<maxItems<<endl;
else
{
items.prodCode[location] = newProductCode;
items.description[location] = newDescription;
items.price[location] = newPrice;
curNoProd++;
}
}
}

I am getting three errors on for each of my assignment statements.
request for member `prodCode' in `((Inventory*)this)-
Inventory::items', which is of non-class type `Product[25]'
request for member `description' in `((Inventory*)this)-
Inventory::items', which is of non-class type `Product[25]'
request for member `price' in `((Inventory*)this)->Inventory::items',
which is of non-class type `Product[25]'

those are my errors and i cannot figure out why these errors are
occuring.
 
I

Ian Collins

Ok so basically my spec file is set up as follows:
#ifndef INVENTORY
#define INVENTORY
#include <string>
using namespace std;

Never, ever put a using statement in a header!
items.prodCode[location] = newProductCode;
items.description[location] = newDescription;
items.price[location] = newPrice;
Should be items[location].prodCode = newProductCode; etc.
 
C

C++ Enthusiast

Ok so basically my spec file is set up as follows:
#ifndef INVENTORY
#define INVENTORY
#include <string>
using namespace std;
struct Product
{
       int prodCode;
       string description;
       double price;

};

class Inventory
{
   private:
      Product items[25];
      int maxItems;
      int curNoProd;
      int findProduct(int /*Product Code*/);
   public:
      Inventory(string /*File Name*/, int /*Max Items*/);
      void addProduct(int /*Product Code*/, string /*Description*/,
double /*Price*/);
      void displayProduct(int /*Product Code*/);
      void writeToFile(ofstream&);
      bool isArrayFull(){return (curNoProd == maxItems);};
      int getCurrentNoElems() const {return curNoProd;};
      void increasePrice(int /*Product Code*/, double /*Price
Increase*/);};

#endif

Now in my implementation file i have most of the function defined
however my problem occurs when trying to define my addProduct and
increasePrice functions. Currently i have this:
void Inventory::addProduct(int newProductCode, string newDescription,
double newPrice)
{
     if (newPrice <= 0 || newPrice > 50)
        cout<<"Price is out of range, must be greater than 0, with a
max of 50"<<endl;
     else if (newProductCode < 10000 || newProductCode > 99999)
            cout<<"Product code must be a 5 digit number."<<endl;
         else
         {
             int location = findProduct(newProductCode);
             if (location != curNoProd)
                cout<<"Product "<<newProductCode<<" already
exists."<<endl;
             else if (isArrayFull())
                     cout<<"No more room for items, current max is
"<<maxItems<<endl;
                  else
                  {
                      items.prodCode[location] = newProductCode;
                      items.description[location] = newDescription;
                      items.price[location] = newPrice;
                      curNoProd++;
                  }
         }

}

I am getting three errors on for each of my assignment statements.
request for member `prodCode' in `((Inventory*)this)->Inventory::items', which is of non-class type `Product[25]'

request for member `description' in `((Inventory*)this)->Inventory::items', which is of non-class type `Product[25]'

request for member `price' in `((Inventory*)this)->Inventory::items',
which is of non-class type `Product[25]'

those are my errors and i cannot figure out why these errors are
occuring.

It should be items[location].prodCode instead of
items.prodCode[location]

-Sunita
 
T

tugnutt7

omg, I am an idiot, of course. Little thing that i shouldve found
myself. Haha, sometimes it just takes a fresh pair of eyes i guess.
Thanks a million.
 
C

clemd

omg, I am an idiot, of course.

If it makes you feel any better, PL/I was (and probably still is) not
so picky about subscript placement. Given

DCL I FIXED BIN;
DCL
1 A (5),
2 B FIXED BIN(31);

A(I).B and A.B(I) were both acceptable.
 
J

Joe Greer

(e-mail address removed) wrote in (e-mail address removed):
Ok so basically my spec file is set up as follows:
#ifndef INVENTORY
#define INVENTORY
#include <string>
using namespace std;

Others have answered the problem you asked about, but I wanted to point out
that a 'using namespace' statement is generally a bad idea in a header
file. You can get by with it in small projects, but in larger projects
this can cause no end of problems as users of your class find that their
implementation files now have whatever arbitrary namespaces brought into
scope in the header file also in scope in their own code. It's much better
to let each implementation file make its own decision about which
namespaces to bring into scope. Just use the 'std::' notation.

Another item is that if you are going to declare a method using ofstream in
your header, you should probably include <iosfwd> to get forward
declarations of that class into your header.

HTH,
joe
 
J

James Kanze

(e-mail address removed) wrote in (e-mail address removed):
Another item is that if you are going to declare a method
using ofstream in your header, you should probably include
<iosfwd> to get forward declarations of that class into your
header.

Actually, he probably shouldn't declare a function to use
ofstream, but rather ostream. And of course, unless he includes
some header to get the declarations (and <iosfwd> is the most
light weight), then his code has undefined behavior. (Although
in practice, if it compiles, it will almost certainly do what he
expects. But it might not compile with the next release of the
compiler.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top