开发者

C++ Undefined Type Error

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 "EquipmentVisi开发者_如何学Gotor.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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜