#### Archived

This topic is now archived and is closed to further replies.

# Hi, header files are bugging me.

This topic is 5584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I have a big problem with my project, there are lots of errors like this. sTexture'' : missing storage-class or type specifiers I have included the files needed but it doesn’t work. Can anyone give me an address or tell me how you should design the headers. I use #ifndef cMap_H #define cMap_H code #endif Isn’t this enough? How should I include headers. Should I use one main header that includes all what the project needs, the other cpp files only include the main header.

##### Share on other sites
quote:
Should I use one main header that includes all what the project needs, the other cpp files only include the main header.
That should be enough.

500

##### Share on other sites
try using #pragma once in place of the if statement u have

header files should usually only contain functions, classes and extern variables

ud have to give more info to get any additional help, i cant think of what u could be doing wrong for sTexture

,Matt

-= kill one your a murderer, kill thousands your a conquerer =-

##### Share on other sites

struct dekl
defines.
functions def
classes def
extern var

Maybe the problem is my struct.
i use them like this in my header.

  struct sTexture{		LPDIRECTDRAWSURFACE	Texture;	cNode<sFile_Tilemap> TileMaps;};

There is a syntax typedef. should I use that with struct.?

##### Share on other sites
This question comes up so many times... we really need a FAQ

Headers are something that all c/c++ programmers have to deal with so you''ll just have to get used to it. It takes effort and care. There are a few simple principles which, if you understand them well, will make it simple and obvious what you need to do. It may still take time and effort but you won''t be chasing your tail all the time.

Understand this: When the compiler looks at a text file, either a .h or a .cpp file (or whatever extension you use), when it compiles it it goes through finding keywords, functions, classes, variables etc. and tries to make sense of it all. If a function or class or variable is mentioned that it has never heard of then it can''t know what to do. You have to be the one who lets it know how to interpret those names.

For example if there was a file with a line

Animal* animal = makeDog("Snowy");

then the compiler wouldn''t know what Animal and makeDog were. Are they functions or classes or variables? You have to think only about the file with that line. Don''t think about the rest of your files. How can the compiler know what you are talking about?

For example the line above might mean there is a function named makeDog which returns a pointer to a base class of a hierachy of classes representing animals, of which Animal is the base.

What the compiler needs to know, in this file is that there is a class called Animal and there is a function called makeDog which takes a string (which is the name of the dog) returns a pointer to an Animal.

If the rest of the file doesn''t require any more detail about the Animal class, for example it doesn''t use the std::string speak() function (or anything else for that matter), then merely putting a forward declaration of the class will satisfy the compiler.

It would also be possible to put a forward declaration of the function. Usually though this is what headers are for. So you would include (in this cpp file) the header which lets the compiler know there is an Animal* makeDog(const std::string&) function in existance somewhere. After the compiler has finished the linker will find it. (if it doesn''t then that''s when you get a link error)

  #include "dogmaker.h"#include "animalPrinter.h"class Animal;int main() {    Animal* dog = makeDog("Doggles - the dog who wore goggles");    printAnimal(dog);    return;}

For headers you should follow the same line of thought. What is the minimum amount of information necessary to satisfy the compiler.

Imagine dog.h below. What does it actually need to be understood by the compiler? Can we forward declare Animal and std::string?

  //dog.hclass Dog : public Animal {public:std::string speak();};

Well, no we can''t. Dog needs to know all about Animal so that the compiler can know what size it will be at compile time. This is important for memory allocation. So anywhere the dog.h header is included in a cpp file you''re going to have to make sure the Animal.h header is before it. You might as well put it right there in the dog.h file, it''ll save you typing and whereever you use dog.h you know you can just include and use. The same goes for the fact it uses std::string. You need to include <string>. Even if that wasn''t the case you aren''t allowed to forward declare std:: stuff because it''s a restricted namespace (in short).

So to make it right

  //dog.h#include "animal.h"#include <string>class Dog : public Animal {public:std::string speak();};

Finally imagine the makeDog.h file. You can just put the following and the compiler will look at the whole file and be happy.

//makedog.h
class Dog;
Dog* makeDog();

You should be able to see that it''s satisfactory by looking at it yourself. Any errors will now be left until linking.

• 10
• 9
• 13
• 41
• 15