tags:

views:

87

answers:

3

Dear all, i have two classes which are computer and floppy disk.

When i put

#include "FloppyDisk.h"
#include "Computer.h"

in main, then compiler generates error of computer undeclared

When i

#include "Computer.h"
#include "FloppyDisk.h"

in main, then compiler generates error of floppy disk undeclared.

What is the problem? I have check there is no cyclic dependency between the header file.

These are the implementation file for reference.

#include "EquipmentAttributes.h"
#include "EquipmentVisitor.h"
#include "Computer.h"

#include "BoostHeader.h"

#include <algorithm>

// =============================================
computer::computer()
 : cont()
{
}
// =============================================
void computer::add(equipment* equip)
{
 cont.push_back(equip);
}
// =============================================
void computer::remove(equipment* equip)
{
 vecIte myIte;

 myIte = std::find(cont.begin(), cont.end(), equip);
 cont.erase(myIte); 
}
// =============================================
void computer::accept(equipmentVisitor* visitor)
{
 BOOST_FOREACH(equipment* anEquip, cont)
 {
  anEquip->accept(visitor);
 }

 visitor->visitComputer(this);
}
// =============================================
computer::equipVec computer::getCont() const
{
 return cont;
}


#include "FloppyDisk.h"
#include "EquipmentAttributes.h"
#include "EquipmentVisitor.h"

// =============================================
floppyDisk::floppyDisk(const int userPrice, const std::string& userName)
 : state(new equipmentState(userPrice, userName) )
{
}
// =============================================
void floppyDisk::accept(equipmentVisitor* visitor)
{
 visitor->visitFloppyDisk(this);
}
// =============================================
floppyDisk::equipPtr floppyDisk::getState() const
{
 return state;
}

Please help.

Thanks.

+1  A: 

Did you use include guards in your header files? If so, please provide more information.

Lars
+1  A: 

I have check there is no cyclic dependency between the header file

There may not be direct cyclic dependency between the files. However, if one of the EquipmentAttributes.h or EquipmentVisitor.h includes either computer.h or floppy.h you create a cyclic depenedcy involving three files. In such a case, you get the undefined type compiler error.

Naveen
Thanks. Problem Solved.
peterwkc
+3  A: 

Are you using the same header include guard in each file, e.g.:

#ifndef MY_INCLUDE_GUARD
#define MY_INCLUDE_GUARD

// blah blah

#endif

The MY_INCLUDE_GUARD needs to be a unique name in each header.

jon hanson
Thanks solve it. What a silly mistake.
peterwkc