bemused and confused
bemused and confused

Reputation: 113

Class definition in header file and including string

This is my current collection of headers :

 #include "Header1.h"
 #include "BedAndMatress.h"
 #include "Sofa.h"
 #include "Table.h"
 #include iostream
 #include fstream
 #include iomanip 
 #include string
 #include cmath
 #include vector

using namespace std;


int main()

the includes and the namespace std bit are in both my main file and my 'function definitions.cpp' file. However, the compiler is throwing up some errors:

  e:\computing\coursework2\programme.cpp(2) : fatal error C1083: Cannot open include
 file: 'BedAndMatress.h': No such file or directory

Originally I had the definition of all my classes in the Header1.h file but it was moaning about unexpected end of file and class definitions so i decided to separate them. The file is included in the project and all the others seem to be working so I'm not sure whats going on. I also created a new header file that was just called Bed but it had the same error so i changed it thinkin there may already be a standard file with that name (long shot apparently) is there a maximum number of header files?

Also, in the class definitions some of the member objects are meant to be strings...

#ifndef CLASS_Bed
#define CLASS_Bed
//////BED
class Bed:public Item
{
string frame;
string frameColour;
string mattress;

public:

int Bed(int count);
int Bed(int count, int number, string name, string frm, string fclr, string mtres);
void count();
void printDetails();
}
#endif

but its not recognising the type specifier.

error C2501: 'string' : missing storage-class or type specifiers

should I include string? I read somewhere that this can cause issues so if thats not the solution how should i proceed?

Ta muchly Hx

Upvotes: 0

Views: 7579

Answers (4)

Mike Seymour
Mike Seymour

Reputation: 254661

Originally I had the definition of all my classes in the Header1.h file but it was moaning about unexpected end of file and class definitions so i decided to separate them.

That probably means you'd missed out a closing bracket (), ], } or >) somewhere; the compiler will give that error if it reaches the end of a file without all the brackets closes. However, it's a good idea to put classes in separate headers anyway.

fatal error C1083: Cannot open include file: 'BedAndMatress.h': No such file or directory

That says that it can't find the file called "BedAndMatress.h". Do you have a file with that name? Is it in the same directory as the source file that includes it? If not, are you specifying the directory in the include path? Is the filename spelt exactly like that, with that capitalisation?

error C2501: 'string' : missing storage-class or type specifiers

First, you need to include <string> to get the definition. Then you need to use its full name std::string. You might be tempted to put using namespace std; in the header file, but that's a bad idea - dumping names in the global namespace could break other files that include the header. It's not a great idea to do that in a source file either, but at least there the damage only affects your own code.

You're also missing a ; at the end of the class definition.

Upvotes: 0

Marius
Marius

Reputation: 837

Originally I had the definition of all my classes in the Header1.h file but it was moaning about unexpected end of file and class definitions so i decided to separate them.

This is because you forgot to put a semicolon after the class definition

class Bed:public Item
{
 ...
}; //notice the semicolon 

#endif

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726909

The problem is that you are missing a semicolon after the closing brace in your class definition.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

should I include string? I read somewhere that this can cause issues so if thats not the solution how should i proceed?

You should include <string>. You probably read that it's not ok to put using namespace std; in the header, which is true. But there's nothing wrong with including a header if you need it. You'll need to qualify the uses of string though:

#ifndef CLASS_Bed
#define CLASS_Bed
//////BED
#include <string>
class Bed:public Item
{
std::string frame;
std::string frameColour;
std::string mattress;

public:

int Bed(int count);
int Bed(int count, int number, std::string name, std::string frm, std::string fclr, std::string mtres);
void count();
void printDetails();
};     //<-- note semi-colon here
#endif

Upvotes: 2

Related Questions