|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
tugnutt7@hotmail.com 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Feb 5, 8:09pm, tugnu...@hotmail.com wrote:
> 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, witha > 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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Feb 5, 9:03pm, tugnu...@hotmail.com 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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
tugnutt7@hotmail.com wrote in news:e78f3ce0-9dab-429a-9e56-
64320ddd278e@d21g2000prf.googlegroups.com: > 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Feb 6, 2:38 pm, Joe Greer <jgr...@doubletake.com> wrote:
> tugnu...@hotmail.com wrote in news:e78f3ce0-9dab-429a-9e56- > 64320ddd2...@d21g2000prf.googlegroups.com: > 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.) -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|