PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Unknown error when trying to add elements to an array of my structwithin my class.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Unknown error when trying to add elements to an array of my structwithin my class.

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 04h09   #1
tugnutt7@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 06/02/2008, 04h33   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unknown error when trying to add elements to an array of my structwithin my class.

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.
  Réponse avec citation
Vieux 06/02/2008, 04h38   #3
C++ Enthusiast
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unknown error when trying to add elements to an array of mystruct within my class.

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
  Réponse avec citation
Vieux 06/02/2008, 05h03   #4
tugnutt7@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unknown error when trying to add elements to an array of mystruct within my class.

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.

  Réponse avec citation
Vieux 06/02/2008, 05h54   #5
clemd@acm.org
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unknown error when trying to add elements to an array of mystruct within my class.

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.
  Réponse avec citation
Vieux 06/02/2008, 13h38   #6
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unknown error when trying to add elements to an array of my struct within my class.

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
  Réponse avec citation
Vieux 07/02/2008, 09h52   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Unknown error when trying to add elements to an array of mystruct within my class.

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h55.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16846 seconds with 15 queries