'Undefined' error

P

Peter Davis

I'm trying to define a template class ...

namespace abc
{
class Node;
typedef boost::shared_ptr<Node> NodePtr;
template <typename Derived> class Cloneable
{
public:
virtual NodePtr clone() const
{
Derived *d = new Derived(static_cast<const
Derived&>(*this));
return d->makeNodePtr(); }
};
....

and then use that as a superclass of another class ...

#include "abc/dom/Node.hpp" // The file above!
namespace abc
{

class Node : public BaseObject, Cloneable<Node>
{
....

This follows, as I understand it, the 'curiously recurring template'
pattern. However, when I try to compile (Visual Studio 2008), I get the
error:
C2504: 'CloneableImpl' : base class undefined.
on the line defining class Node. I don't understand why, since the cpp
file includes the header file in which the template Cloneable is
defined, and they're in the same namespace.

Any insights?

Thanks!
-pd
 
C

Casey

"Cloneable" and "CloneableImpl" are different identifiers. I think you are missing the forest for the trees.
 
V

Victor Bazarov

I'm trying to define a template class ...

namespace abc
{
class Node;
typedef boost::shared_ptr<Node> NodePtr;
template <typename Derived> class Cloneable
{
public:
virtual NodePtr clone() const
{
Derived *d = new Derived(static_cast<const
Derived&>(*this));
return d->makeNodePtr(); }
};
...

and then use that as a superclass of another class ...

#include "abc/dom/Node.hpp" // The file above!
namespace abc
{

class Node : public BaseObject, Cloneable<Node>
{
...

This follows, as I understand it, the 'curiously recurring template'
pattern. However, when I try to compile (Visual Studio 2008), I get the
error:
C2504: 'CloneableImpl' : base class undefined.
on the line defining class Node. I don't understand why, since the cpp
file includes the header file in which the template Cloneable is
defined, and they're in the same namespace.

Any insights?

Not with your incomplete code. See FAQ 5.8.

This code:
--------------------------------------------------
namespace abc
{
class Node;
typedef Node* NodePtr;
template <typename Derived> class Cloneable
{
public:
virtual NodePtr clone() const
{
Derived *d = new Derived(static_cast<const Derived&>(*this));
return d->makeNodePtr();
}
};

class BaseObject
{

};
}

namespace abc
{
class Node : public BaseObject, Cloneable<Node>
{
public:
Node* makeNodePtr() { return new Node; }
};
}

int main()
{
abc::Node blah;
abc::Node* blahblah = blah.makeNodePtr();
}
 
P

Peter Davis

"Cloneable" and "CloneableImpl" are different identifiers. I think you are missing the forest for the trees.

Sorry. That was a typo in my attempt to simplify the example. In
reality, everything is "CloneableImpl", but the problem occurs nonetheless.

Thanks,

-pd
 
P

Peter Davis

Not with your incomplete code. See FAQ 5.8.

This code:

Thank you. I'm still not sure what was going on, but I found that
forward declaring the cloneable class seems to work in VS2008:

class Node;
class Node : public BaseObject, Cloneable<Node>
....

I have about 30 classes, 2nd- and 3rd-derivatives of BaseObject, and
most built just fine without the forward declaration. When time permits,
I'll do some more investigating and post any findings here.

Thank you!

-pd
 
V

Victor Bazarov

Thank you. I'm still not sure what was going on, but I found that
forward declaring the cloneable class seems to work in VS2008:

class Node;
class Node : public BaseObject, Cloneable<Node>
...

I have about 30 classes, 2nd- and 3rd-derivatives of BaseObject, and
most built just fine without the forward declaration. When time permits,
I'll do some more investigating and post any findings here.

Well, unless you're unduly constrained to 2008, you should consider
switching to 2010 or even to 2012 (which is in RC stage now).

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top