|
-
July 10th, 2005, 02:29 PM
#1
Class templates
I'm having some trouble getting my new, fancy, templated Interpolator class compiling. The problem is that I'm not as comfortable with the template-syntax as I ought to be. Lots of respect and gratitudes to anyone who can help me identify my mistakes.
Code:
class Interpolator
{
Interpolator()
{}
public:
virtual ~Interpolator()
{
}
virtual void Reset() = 0;
virtual void Interpolate(float timespan) = 0;
};
template<typename T>
class InterpolatorImpl : public Interpolator
{
public:
typedef typename T ValueType;
private:
struct Keyframe
{
InterpolatorImpl<T>::ValueType value_;
float time_;
};
typedef std::list<InterpolatorImpl<T>::Keyframe> KeyframeList;
public:
InterpolatorImpl() : currentTime_(0), pTarget_(0)
{
}
virtual ~InterpolatorImpl()
{
}
void SetTarget(ValueType* pTarget)
{
pTarget_ = pTarget;
}
void AddKeyframe(const ValueType& value_, float time);
virtual void Interpolate(float timespan);
virtual void Reset()
{
currentTime_ = 0;
}
private:
float currentTime_;
KeyfameList keyframes_;
ValueType* pTarget_;
};
//Usage
int main()
{
float myFloat = 0;
InterpolatorImpl<float>* myInterp = new InterpolatorImpl<float>();
myInterp->AddKeyframe(24.5f, 2);
myInterp->AddKeyframe(0, 5);
myInterp->SetTarget(&myFloat);
float timespan = 0.05f;
for(float t=0; t<6; t+=timespan)
{
myInterp->Interpolate(timespan);
cout << myFloat << endl;
}
delete myInterp;
}
The compliler complains about "error C2501: 'InterpolatorImpl<T>::Keyframe::value_' : missing storage-class or type specifiers" amongst other things, but that seems to be the root of all the evil...
any ideas?
/Niklas Andersson
Sweden
-
July 10th, 2005, 02:52 PM
#2
Re: Class templates
Basically, there are a lot of little mistakes in your code. I won't go into the details here (unless you want me to), but I fixed up your code so you can take a look at it youself.
Code:
#include <list>
#include <iostream>
class Interpolator
{
protected:
Interpolator()
{}
public:
virtual ~Interpolator()
{
}
virtual void Reset() = 0;
virtual void Interpolate(float timespan) = 0;
};
template<typename T>
class InterpolatorImpl : public Interpolator
{
public:
typedef T ValueType;
private:
struct Keyframe
{
typename InterpolatorImpl<T>::ValueType value_;
float time_;
};
typedef std::list<Keyframe> KeyframeList;
public:
InterpolatorImpl() : currentTime_(0), pTarget_(0)
{
}
virtual ~InterpolatorImpl()
{
}
void SetTarget(ValueType* pTarget)
{
pTarget_ = pTarget;
}
void AddKeyframe(const ValueType& value_, float time);
virtual void Interpolate(float timespan);
virtual void Reset()
{
currentTime_ = 0;
}
private:
float currentTime_;
KeyframeList keyframes_;
ValueType* pTarget_;
};
//Usage
int main()
{
float myFloat = 0;
InterpolatorImpl<float>* myInterp = new InterpolatorImpl<float>();
myInterp->AddKeyframe(24.5f, 2);
myInterp->AddKeyframe(0, 5);
myInterp->SetTarget(&myFloat);
float timespan = 0.05f;
for(float t=0; t<6; t+=timespan)
{
myInterp->Interpolate(timespan);
std::cout << myFloat << std::endl;
}
delete myInterp;
}
This code compiles fine, but does not link as it's missing a couple of function definitions.
Last edited by wien; July 10th, 2005 at 03:00 PM.
Insert entertaining phrase here
-
July 10th, 2005, 03:07 PM
#3
Re: Class templates
Wohoo! Swell!
No, you don't have to go into details. It was the use of "typename" that was the main problem, and the answer i needed.
You've been very helpful today =) Thanks alot!
/Niklas
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|