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



 DEVELOP > c-Plus-Plus > Unknown error when trying to add elements to an array of my structwithin my class.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 05 Feb 2008 10:09:26 PM
Object: Unknown error when trying to add elements to an array of my structwithin my class.
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.
.

User: "Ian Collins"

Title: Re: Unknown error when trying to add elements to an array of my structwithin my class. 05 Feb 2008 10:33:17 PM
wrote:

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.
--
Ian Collins.
.

User: "C++ Enthusiast"

Title: Re: Unknown error when trying to add elements to an array of mystruct within my class. 05 Feb 2008 10:38:47 PM
On Feb 5, 8:09=A0pm,
wrote:

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

};

class Inventory
{
=A0 =A0private:
=A0 =A0 =A0 Product items[25];
=A0 =A0 =A0 int maxItems;
=A0 =A0 =A0 int curNoProd;
=A0 =A0 =A0 int findProduct(int /*Product Code*/);
=A0 =A0public:
=A0 =A0 =A0 Inventory(string /*File Name*/, int /*Max Items*/);
=A0 =A0 =A0 void addProduct(int /*Product Code*/, string /*Description*/,
double /*Price*/);
=A0 =A0 =A0 void displayProduct(int /*Product Code*/);
=A0 =A0 =A0 void writeToFile(ofstream&);
=A0 =A0 =A0 bool isArrayFull(){return (curNoProd =3D=3D maxItems);};
=A0 =A0 =A0 int getCurrentNoElems() const {return curNoProd;};
=A0 =A0 =A0 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)
{
=A0 =A0 =A0if (newPrice <=3D 0 || newPrice > 50)
=A0 =A0 =A0 =A0 cout<<"Price is out of range, must be greater than 0, with=

a

max of 50"<<endl;
=A0 =A0 =A0else if (newProductCode < 10000 || newProductCode > 99999)
=A0 =A0 =A0 =A0 =A0 =A0 cout<<"Product code must be a 5 digit number."<<en=

dl;

=A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0{
=A0 =A0 =A0 =A0 =A0 =A0 =A0int location =3D findProduct(newProductCode);
=A0 =A0 =A0 =A0 =A0 =A0 =A0if (location !=3D curNoProd)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cout<<"Product "<<newProductCode<<" alread=

y

exists."<<endl;
=A0 =A0 =A0 =A0 =A0 =A0 =A0else if (isArrayFull())
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0cout<<"No more room for items, =

current max is

"<<maxItems<<endl;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 items.prodCode[location] =3D n=

ewProductCode;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 items.description[location] =

=3D newDescription;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 items.price[location] =3D newP=

rice;

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 curNoProd++;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
=A0 =A0 =A0 =A0 =A0}

}

I am getting three errors on for each of my assignment statements.
request for member `prodCode' in `((Inventory*)this)->Inventory::items', w=

hich 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
.
User: ""

Title: Re: Unknown error when trying to add elements to an array of mystruct within my class. 05 Feb 2008 11:03:20 PM
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.
.
User: ""

Title: Re: Unknown error when trying to add elements to an array of mystruct within my class. 05 Feb 2008 11:54:00 PM
On Feb 5, 9:03=A0pm,
wrote:

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.
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER