Zitat von fren
im prinzip sollts immer ein pärchen geben, jedes .cpp hat sein .h mit den prototypen/class deklarationen. im .cpp file includest du das .h, im .h file machst du weitere includes die nötig sind.
forward declarations brauchst du nur wenn ein name zu einem bestimmten zeitpunkt in einem .h file noch nicht bekannt ist weil er weiter unten erst deklariert wird. mit einer forward deklaration sagst du nur das es den namen mal geben wird, du musst ihn dann trotzdem irgendwie zur verfügung stellen.
hth
So weit so gut, also verstehe ich es eh richtig. Nun habe ich das Problem, dass ich versuche, eine Circular Inclusion durch Forward Declarations zu umgehen. Im Konkreten habe ich folgende Abhaengigkeiten:
Node (benutzt Camera, Light, Model)
Camera (abgeleitet von Node)
Light (abgeleitet von Node)
Model (abgeleitet von Node)
Dann habe ich folgendes in den Files stehen:
File: node.h
// node.h
#ifndef _NODE_H_
#define _NODE_H_
//#include "camera.h"
//#include "light.h"
//#include "model.h"
...
using namespace Loader;
using namespace MathLib;
// forward declarations
class Camera;
class Light;
class Model;
namespace Loader
{
//-----------------------------------------------------------------------------
/// Represents a node.
class Node : public ReferenceCounter
{
public:
/// Holds the node type.
enum NodeType
{
NODE_ANONYMOUS = 0x00000000,
NODE_CAMERA = 0x00000001,
NODE_GEOMETRY = 0x00000002,
NODE_LIGHT = 0x00000003
};
/// Holds the type of the node.
static NodeType type_;
/// Holds the node name.
char *name_;
...
public:
//-----------------------------------------------------------------constructor-
/// The default constructor.
Node();
//------------------------------------------------------------------destructor-
/// The destructor.
virtual ~Node();
//------------------------------------------------------------------asCamera()-
/// Convertes this node to a camera.
/// @return Returns a pointer to a 'Camera' object.
Camera *asCamera();
//-------------------------------------------------------------------asLight()-
/// Convertes this node to a light.
/// @return Returns a pointer to a 'Light' object.
Light *asLight();
//-------------------------------------------------------------------asModel()-
/// Convertes this node to a model.
/// @return Returns a pointer to a 'Model' object.
Model *asModel();
//-------------------------------------------------------------------getType()-
/// Returns the node type.
/// @return Returns the type of the node.
virtual NodeType getType() const;
...
}; // class Node
//------------------------------------------------------------------operator>>-
/// This reads a node from an input stream.
BinInStream &operator>>(BinInStream &stream, Node &node);
...
} // namespace Loader
#endif // _NODE_H_
Alles anzeigen
File: camera.h
// camera.h
#ifndef _CAMERA_H_
#define _CAMERA_H_
#include "node.h"
#include "bin_in_stream.h"
#include "bin_out_stream.h"
#include "stream_ops.h"
using namespace Loader;
using namespace MathLib;
namespace Loader
{
/// Holds the different camera types.
enum CameraType
{
/// The anonymous camera.
CAM_ANONYMOUS,
/// The point camera.
CAM_POINT,
/// The directional camera.
CAM_DIRECTIONAL,
/// The spot camera.
CAM_SPOT
};
//-----------------------------------------------------------------------------
/// Represents a camera.
class Camera : public Node
{
public:
/// To be set to Camera.
static NodeType type_;
/// Holds the fiel of view.
float fov_;
/// Holds the focal length.
float focal_length_;
public:
//-----------------------------------------------------------------constructor-
/// The default constructor.
Camera();
//------------------------------------------------------------------destructor-
/// The destructor.
virtual ~Camera();
//-------------------------------------------------------------------getType()-
/// Returns the node's type.
/// @return Returns the type of the node.
virtual Node::NodeType getType() const;
}; // class Camera
//------------------------------------------------------------------operator>>-
/// This reads a camera from an input stream.
BinInStream &operator>>(BinInStream &stream, Camera &camera);
...
} // namespace Loader
#endif // _CAMERA_H_
Alles anzeigen
Und schlieszlich bekomme ich in den Files: camera.cpp, light.cpp, model.cpp an entsprechenden Positionen den Fehler: xxx.cpp(20): error C2027: use of undefined type 'Xxxx' c:\...\node.h(yy) : see declaration of 'Xxxx'. Im naechsten File hab ich die Posititonen markiert.
File: camera.cpp
// camera.cpp
#include "camera.h"
#include "node.h"
...
using namespace Loader;
using namespace MathLib;
Loader::Node::NodeType Loader::Camera::type_ = NODE_CAMERA;
//-----------------------------------------------------------------constructor-
Camera::Camera() : //camera.cpp(20): error C2027: use of undefined type 'Camera' c:\...\node.h(36) : see declaration of 'Camera'
fov_(0.0f),
focal_length_(0.0f)
{
}
//------------------------------------------------------------------destructor-
Camera::~Camera() //camera.cpp(20): error C2027: use of undefined type 'Camera' c:\...\node.h(36) : see declaration of 'Camera'
{
}
//-------------------------------------------------------------------getType()-
Node::NodeType Camera::getType() const //camera.cpp(20): error C2027: use of undefined type 'Camera' c:\...\node.h(36) : see declaration of 'Camera'
{
return(type_);
}
...
Alles anzeigen
Jemand eine Idee, weshalb er die Definition nicht animmt oder warum der Fehler auftritt?
Bin fuer jeden Hint dankbar..
fren Danke fuer die Erklaerung..
ciao..