tags:

views:

378

answers:

3

Hi i'm new here and learning C++

ive just created 2 very simple files , one hpp, and one cpp.. They are located in the same directory and I've added the directory to the C++ includes list through the 'Tools > Compiler Options' window.

using the command

#include "Cat.hpp"

i get the following error

5 K:- C++ Excercises\Cat.cpp In file included from Cat.cpp

And the source does not compile.

Can anyone tell me what I need to do to get this to work?


Nevermind, Looks like I'm using Code::Blocks instead.... (Thanks jalf) ;-)

A: 

Remove the

include "Cat.cpp";

from Cat.hpp. Your error will go away.

Jacob

TheJacobTaylor
A: 

Think of #include as "copy/paste". When you #include a file you are taking its entire contents from one file and pasting it where the #include directive exists. Therefore if your cpp file #includes a hpp file that #includes the cpp file, you're asking the compiler to copy/paste the cpp file back onto itself, which is why the compiler is yelling at you.

In general you should only #include header files (both within cpp files and within other hpp files).

fbrereto
Hi I've not added any : include "Cat.cpp"; statement in the hpp file. therre is only a #include of the hpp file in the cpp file.Might be like what jalf says, could be buggy... does anyone else here have specific experience of Dev C++? what's the next best simple to use alternative?
A: 

Any file that will be #included should only ever be included once. THe common way to accomplish this is to add:

#ifndef _MYHEADER_H
#define _MYHEADER_H

at the beginning of the include file, and to add

#endif // _MYHEADER_H

at the end.

hb2pencil