By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Problem with templates

P: n/a
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

Jun 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 13 Jun 2007 05:35:47 -0000, Joseph Paterson
<jo*************@gmail.comwrote:
>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
Jun 13 '07 #2

P: n/a
Joseph Paterson wrote:
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!
http://www.parashift.com/c++-faq-lit...html#faq-35.12
Read this and the next three sections.

--
Robert Bauck Hamar

«Det er ikke mer enn en uke siden sist jeg satt hos en grafisk
designer og prøvde å gråte meg til en bindestrek.»
– Torgrim Eggen
Jun 13 '07 #3

P: n/a
Joseph Paterson <jo*************@gmail.comwrote:
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-lit...html#faq-35.12

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 13 '07 #4

P: n/a
Thanks very much for all your help!

Joseph Paterson.

Jun 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.