#ifndef ITEM
#define ITEM
#include <string>
#define MAXINV 10
class Item {
private:
string desc;
double weight;
public:
//Constructors/Destructor
Item():desc="",weight=0{}
Item(string d, double w);
~Item();
//Accessor functions
string getdesc(){
return desc;
}
double getweight(){
return weight;
}
//Functions
void setdesc(string d);
void setweight(double w);
};
class Inventory {
private:
Item inv[MAXINV];
public:
Inventory(){
for(int i=0;i<MAXINV;i++)
inv[i]=Item();
}
Inventory(Item i[]);
//Accessor function
void getinv() {
return inv;
}
//Functions
void add(Item i, int slot);
void remove(int slot);
};
#endif
Why is this telling me it's missing storage class or type specifiers?
Code:
Errors:
c:\Programs\Games\RPGClasses\Item.h(7): error C2146: syntax error : missing ';' before identifier 'desc'
c:\Programs\Games\RPGClasses\Item.h(7): error C2501: 'Item::desc' : missing storage-class or type specifiers
c:\Programs\Games\RPGClasses\Item.h(7): error C2501: 'Item::string' : missing storage-class or type specifiers
c:\Programs\Games\RPGClasses\Item.h(11): error C2059: syntax error : 'string'
c:\Programs\Games\RPGClasses\Item.h(11): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
c:\Programs\Games\RPGClasses\Item.h(11): error C2969: syntax error : '=' : expected member function definition to end with '}'
c:\Programs\Games\RPGClasses\Item.h(12): error C2238: unexpected token(s) preceding ';'
c:\Programs\Games\RPGClasses\Item.h(12): error C2629: unexpected 'Item ('
c:\Programs\Games\RPGClasses\Item.h(16): error C2146: syntax error : missing ';' before identifier 'getdesc'
c:\Programs\Games\RPGClasses\Item.h(16): error C2501: 'Item::string' : missing storage-class or type specifiers
c:\Programs\Games\RPGClasses\Item.h(18): warning C4183: 'getdesc': missing return type; assumed to be a member function returning 'int'
c:\Programs\Games\RPGClasses\Item.h(25): error C2061: syntax error : identifier 'string'
I imagine if i fix whats wrong it will fix everything, but i cant figure out whats wrong.
[edited by - Nanven on May 27, 2003 1:37:26 AM]
string = char[] or char*
no string in C or C++, use one of the above. Probably the array.
{particleG}
quote:Original post by particleG
string = char[] or char*
no string in C or C++, use one of the above. Probably the array.
{particleG}
Wrong. No string in C.
The original poster forgot to prepend std:: to string:
string desc;
should be std::string desc;
not only that, but I don''t think = operators are allowed in the initialization list.
i.e.: instead of this:
do this:
i.e.: instead of this:
Item() : desc = "", weight = 0 {}
do this:
Item() : desc( "" ), weight( 0 ) {}
Your Inventory constructor is screwy. Are you coming from a Java background? In C++ we distinguish between objects and references (or pointers) to those objects. Where you assign a new Item object (created via explicit constructor invocation) to each element of inv, you''re playing havok with your system. When you perform an assignment in C++ on a data type that lacks a native assignment operator (and copy constructor as a fallback), the compiler supplies a default which performs a bitwise copy. Which may not be desired behavior.
Nah, it was just a very early version of the class, heres my new one:
//|----------------------------------------------------|//|---Item.h - Item and Inventory class declarations---|//|----------------------------------------------------|//Make sure header is only defined once#ifndef ITEM#define ITEM//Header files#include <string>#include <iostream>using namespace std;//Macro for max inventory space#define MAXINV 10//|----------------|//|---Class Item---|//|----------------|class Item {private: //Attributes string desc; double weight;public: //Constructors/Destructor Item():desc(""),weight(0) {} Item(const string &d, double w); ~Item(); //Accessor functions string getdesc() { return desc; } double getweight() { return weight; } //Functions void setdesc(const string &d); void setweight(double w); void clear(); //Overloaded operators friend bool operator==(const Item &first,const Item &last) { if(first.desc == last.desc && first.weight == last.weight) return true; return false; }};//|---------------------|//|---Class Inventory---|//|---------------------|class Inventory {private: //Attributes Item inv[MAXINV];public: Inventory() { for(int i=0;i<MAXINV;i++) inv[i].clear(); } //Copy constructor Inventory(const Inventory &i); //Function declarations void add(const Item &i, int slot); void remove(int slot); void display(); int findslot(); int finditem(const Item &i);};#endif
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement