Problem with templates

J

Joseph Paterson

Hi all,

I'm having some trouble with templates. I've read up some FAQs, but
still cannot seem to find the source of my problem!

I have three files: Bezier.h, Bezier.cpp and main.cpp.

Here are stripped down versions:

--- Bezier.h---
emplate <typename T = f32>
class Bezier
{
public:
static array< vector3<T> > *
GenerateBezierCurve(const array<vector3<T> >& cpoints, int
numpoints);
};
--- Bezier.h---
---Bezier.cpp---
template <typename T>
array< vector3<T> > *
Bezier<T>::GenerateBezierCurve(const array< vector3<T> >& cpoints,
int numpoints)
{
array< vector3<T> > * points = new array< vector3<T> >(numpoints);
T time = 0.0;
T time_increment = 1.0 / numpoints;
while (time < 0.0)
{
points->add(PointOnCurve(cpoints, time)); //
time += time_increment;
}
return points;
}
---Bezier.cpp---
---main.cpp---
array< vector3<float> > cpoints;
cpoints.push_back(vector3<float>(0.0f, 0.0f, 0.0f));
cpoints.push_back(vector3<float>(1.0f, 0.0f, 0.0f));
cpoints.push_back(vector3<float>(1.0f, 1.0f, 0.0f));
array< vector3<float> > * points =
Bezier<float>::GenerateBezierCurve(cpoints, 100);
---main.cpp---

It all compiles fine, but won't link:
main.cpp:: undefined reference to
`Bezier<float>::GenerateBezierCurve(array<vector3<float> > const&,
int)

Any help would be greatly appreciated!

Thanks,

Joseph Paterson
 
D

Dave Rahardja

Hi all,

I'm having some trouble with templates. I've read up some FAQs, but
still cannot seem to find the source of my problem!

I have three files: Bezier.h, Bezier.cpp and main.cpp.

Here are stripped down versions:

--- Bezier.h---
emplate <typename T = f32>
class Bezier
{
public:
static array< vector3<T> > *
GenerateBezierCurve(const array<vector3<T> >& cpoints, int
numpoints);
};
--- Bezier.h---
---Bezier.cpp---
template <typename T>
array< vector3<T> > *
Bezier<T>::GenerateBezierCurve(const array< vector3<T> >& cpoints,
int numpoints)
{
array< vector3<T> > * points = new array< vector3<T> >(numpoints);
T time = 0.0;
T time_increment = 1.0 / numpoints;
while (time < 0.0)
{
points->add(PointOnCurve(cpoints, time)); //
time += time_increment;
}
return points;
}
---Bezier.cpp---
---main.cpp---
array< vector3<float> > cpoints;
cpoints.push_back(vector3<float>(0.0f, 0.0f, 0.0f));
cpoints.push_back(vector3<float>(1.0f, 0.0f, 0.0f));
cpoints.push_back(vector3<float>(1.0f, 1.0f, 0.0f));
array< vector3<float> > * points =
Bezier<float>::GenerateBezierCurve(cpoints, 100);
---main.cpp---

It all compiles fine, but won't link:
main.cpp:: undefined reference to
`Bezier<float>::GenerateBezierCurve(array<vector3<float> > const&,
int)

Any help would be greatly appreciated!

Thanks,

Joseph Paterson

The definition of Bezier<T>::GenerateBezierCurve() must be visible at the
place it is instantiated, i.e. from the main.cpp compilation unit. Place it in
the Bezier.h file, or have Bezier.h #include Bezier.cpp.

You're also missing all the #include statements in your .cpp files, but I
assume that it's due to a copy-paste omission.

If your compiler supports the export keyword, you can leave the definition in
Bezier.cpp and declare it export.

-dr
 
M

Marcus Kwok

Joseph Paterson said:
I'm having some trouble with templates. I've read up some FAQs, but
still cannot seem to find the source of my problem!

I have three files: Bezier.h, Bezier.cpp and main.cpp.

Here are stripped down versions:

--- Bezier.h---
emplate <typename T = f32>
class Bezier
{
public:
static array< vector3<T> > *
GenerateBezierCurve(const array<vector3<T> >& cpoints, int
numpoints);
};
--- Bezier.h---
---Bezier.cpp---
template <typename T>
array< vector3<T> > *
Bezier<T>::GenerateBezierCurve(const array< vector3<T> >& cpoints,
int numpoints)
{ [snip implementation]
}
---Bezier.cpp---

It all compiles fine, but won't link:
main.cpp:: undefined reference to
`Bezier<float>::GenerateBezierCurve(array<vector3<float> > const&,
int)

See this FAQ (and the ones following it):
[35.12] Why can't I separate the definition of my templates class from
it's declaration and put it inside a .cpp file?
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top