Jump to content
  • Advertisement
Sign in to follow this  
hades5k

Including files in C++

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm learning C++ and I'm using Dev-C++. I'm trying things with classes but some problems occured when I tried to seperate the implentation code from the declaration. When I did, the program was no longer compiled without error. Here's the source and the compiler's error: ------------ main.cpp ------------ #include <cstdlib> #include <iostream> #include <vector> using namespace std; #include "CInventory.h" int main(int argc, char *argv[]) { CInventory myInv; CItem myC; myInv.caca(); myInv.addItem( myC ); myInv.returnFirst().printMe(); string v = "sdkfjslkfjsdkl"; myInv.setName(v); myInv.returnFirst().printMe(); myInv.caca(); system("PAUSE"); return EXIT_SUCCESS; } --------------------- CInventory.h --------------------- #include "CItem.h" class CInventory { private: vector<CItem> m_vecInventory; public: CInventory(); ~CInventory(); void addItem( CItem &cItem ); CItem returnFirst( void ); bool setName(string szName); void caca( void ); }; CInventory::CInventory() {} CInventory::~CInventory() {} void CInventory::addItem( CItem &cItem ) { m_vecInventory.insert( m_vecInventory.begin(), cItem ); } CItem CInventory::returnFirst( void ) { return m_vecInventory.front(); } bool CInventory::setName(string szName) { m_vecInventory.front().setName( szName ); return true; } void CInventory::caca( void ) { cout << m_vecInventory.size() << endl; } ----------- CItem.h ----------- class CItem { public: CItem(); // default constructor ~CItem(); // default destructor void printMe(); // print something bool setName( string szName ); private: string _szName; }; ------------- CItem.cpp ------------- #include <string.h> #include "CItem.h" CItem::CItem() { _szName = "Bouzouk"; } CItem::~CItem() {} void CItem::printMe() { cout << _szName << endl; } bool CItem::setName( string szName ) { _szName = szName; return true; } and finally, the errors : 2 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp In file included from CItem.cpp 8 C:\Dev-Cpp\Projects\Inventory_01\CItem.h expected `;' before '(' token 11 C:\Dev-Cpp\Projects\Inventory_01\CItem.h `string' does not name a type C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp In constructor `CItem::CItem()': 4 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp `_szName' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp In member function `void CItem::printMe()': 9 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp `cout' undeclared (first use this function) 9 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp `_szName' undeclared (first use this function) 9 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp `endl' undeclared (first use this function) 9 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp At global scope: 12 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp `bool CItem::setName' is not a static member of `class CItem' 12 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp `string' was not declared in this scope 13 C:\Dev-Cpp\Projects\Inventory_01\CItem.cpp expected `,' or `;' before '{' token C:\Dev-Cpp\Projects\Inventory_01\Makefile.win [Build Error] [CItem.o] Error 1 Thanks for your help!! Chris

Share this post


Link to post
Share on other sites
Advertisement
Without truly analyzing your source, the errors you're getting appear to stem from not having "using namespace std;" declared before declaring your string variables. Try it in CItem.cpp after including your string header. Also, #include <string.h> is normally associated with the C-style string (char *) functions; you should be using #include <string> instead.

Share this post


Link to post
Share on other sites
TDragon is right, add using namespace std; to CItem.cpp (before the inclusion of CItem.h)


Also, this is not your problem, but when working with header files you will need to use what is called an include guard. It is to make sure that the compiler does not include the same file twice...

#ifndef C_INVENTORY
#define C_INVENTORY

//class definition goes here

#endif

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Include string in your header file not in the implementation file.

Share this post


Link to post
Share on other sites
I'm getting gsomewhere now.
Thanks to all for your replies. I applied everything and most of the errors are gone. But now, I have this only problem (I'll add new sources at EOF):

multiple definition of `CItem::CItem()'
first defined here
multiple definition of `CItem::CItem()'
first defined here
...

-------------
CItem.cpp
-------------

#include <iostream>
#include "CItem.h"

using namespace std;

CItem::CItem() { _szName = "Bouzouk"; }
CItem::~CItem() {}

void CItem::printMe()
{
cout << _szName << endl;
}

bool CItem::setName( string szName )
{
_szName = szName;
return true;
}


-------------
CItem.h
-------------

#ifndef _CITEM_H_
#define _CITEM_H_

using namespace std;

class CItem
{
public:
CItem(); // default constructor
~CItem(); // default destructor

void printMe(); // print something
bool setName( string szName );

private:
string _szName;
};

#endif



Thanks again!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also it is a good idea to define the class in the header file outside of your include guard. To avoid problems with mutual includes.

---- a.h ---

class A;

#ifndef A_H
#define A_H

#include "b.h"

class A
{
B *b;
// .....
};

#endif // A_H

---- b.h ---

class B;

#ifndef B_H
#define B_H

#include "a.h"

class B
{
A *a;
// .....
};

#endif // B_H

Would not be possible without the lines

class A;
class B;

outside of the include guard.

Damn, I must be bored... :-)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also it is a good idea to define the class in the header file outside of your include guard. To avoid problems with mutual includes.

---- a.h ---

class A;

#ifndef A_H
#define A_H

#include "b.h"

class A
{
B *b;
// .....
};

#endif // A_H

---- b.h ---

class B;

#ifndef B_H
#define B_H

#include "a.h"

class B
{
A *a;
// .....class A;

#ifndef A_H
#define A_H

class A
{
// .....
};

#endif // A_H

};

#endif // B_H

Would not be possible without the lines

class A;
class B;

outside of the include guard.

Damn, I must be bored... :-)

Share this post


Link to post
Share on other sites
in the main.cpp... I changed the code to include my CInventory.h BEFORE the "using namespace std;" statement. And hop! no more multiple definition problem!

Does it as something to do?? (i'm really a newb)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!