templated function with vector

D

Daniel L Elliott

Hello,

I am feeling very confused right now. The following code gives me an
error:

X.h:
template <typename T>
ILuint startImage(vector<vector<T> > imageData);

X.cpp:
template<typename T>
void X::startImage(vector<vector<T> > imageData){blah}

test.cpp:
vector<vector<double> > testImage1(10);
X.startImage(testImage1);

gcc 3.3.2 gives the following error:
/home/dane/myLibraries/image/testDriver.cpp:25: undefined reference to`unsigned X::startImage<double>(std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,std::allocator<double> > > >)'

Am I not using templates correctly? It appears to have the correct
function.

Thank you very much for any assistance I might receive!

- dan elliott
 
V

Victor Bazarov

Daniel said:
I am feeling very confused right now. The following code gives me an
error:

X.h:
template <typename T>
ILuint startImage(vector<vector<T> > imageData); ^^^^^^

X.cpp:
template<typename T>
void X::startImage(vector<vector<T> > imageData){blah} ^^^^

test.cpp:
vector<vector<double> > testImage1(10);
X.startImage(testImage1);

gcc 3.3.2 gives the following error:
/home/dane/myLibraries/image/testDriver.cpp:25: undefined reference to`unsigned X::startImage<double>(std::vector<std::vector<double,
std::allocator<double> >, std::allocator<std::vector<double,std::allocator<double> > > >)'

Am I not using templates correctly? It appears to have the correct
function.

It appears you don't.

V
 
D

Daniel L Elliott

It appears you don't.

V

I am not above total stupidity. I tried to simplify the example, and
forgot to change ILuint startImage to void X::startImage in the example.

It should read:

X.h:
template <typename T>
void startImage(vector<vector<T> > imageData);

X.cpp:
template<typename T>
void X::startImage(vector<vector<T> > imageData){blah}

test.cpp:
vector<vector<double> > testImage1(10);
X.startImage(testImage1);

gcc 3.3.2 gives the following error:
/home/dane/myLibraries/image/testDriver.cpp:25: undefined reference to`unsigned X::startImage<double>(std::vector<std::vector<double,
std::allocator<double> >,
std::allocator<std::vector<double,std::allocator<double> > > >)'

Thanks anyway.

- dan
 
J

John Harrison

Daniel L Elliott said:
I am not above total stupidity. I tried to simplify the example, and
forgot to change ILuint startImage to void X::startImage in the example.

It should read:

X.h:
template <typename T>
void startImage(vector<vector<T> > imageData);

X.cpp:
template<typename T>
void X::startImage(vector<vector<T> > imageData){blah}

test.cpp:
vector<vector<double> > testImage1(10);
X.startImage(testImage1);

gcc 3.3.2 gives the following error:
/home/dane/myLibraries/image/testDriver.cpp:25: undefined reference
to`unsigned X::startImage<double>(std::vector<std::vector<double,
std::allocator<double> >,
std::allocator<std::vector<double,std::allocator<double> > > >)'

Thanks anyway.

- dan

I reckon this is the #1 post to this group.

ALL TEMPLATE CODE MUST GO IN HEADER FILES.

Most startimage out of X.cpp, put it in X.h and all will be well.

Don't books on templates teach this? Where did you learn about templates
that didn't mention this basic fact? And what about all the other people who
post similar problems to yours. Please tell, I'm genuinely curious.

john
 
D

Daniel L Elliott

I reckon this is the #1 post to this group.

ALL TEMPLATE CODE MUST GO IN HEADER FILES.

Most startimage out of X.cpp, put it in X.h and all will be well.

Don't books on templates teach this? Where did you learn about templates
that didn't mention this basic fact? And what about all the other people who
post similar problems to yours. Please tell, I'm genuinely curious.

john

Hey, thanks for the information. I am not sure how this fact has eluded
me over the years. I even have books on c++ and they don't explictly say
it (although their examples always put definitions in the header).

You need to create a webpage entitled: function templates only belong in
the header and have a bunch of people link to it so dummies like myself
can find it via Google!

- dan
 
J

John Harrison

Hey, thanks for the information. I am not sure how this fact has eluded
me over the years. I even have books on c++ and they don't explictly say
it (although their examples always put definitions in the header).

You need to create a webpage entitled: function templates only belong in
the header and have a bunch of people link to it so dummies like myself
can find it via Google!

I guess its templates dirty little secret, people don't really like to talk
about it.

But it is in the FAQ,
http://www.parashift.com/c++-faq-lite/containers-and-templates.html

john
 
D

Default User

Daniel L Elliott wrote:

Hey, thanks for the information. I am not sure how this fact has eluded
me over the years. I even have books on c++ and they don't explictly say
it (although their examples always put definitions in the header).

You need to create a webpage entitled: function templates only belong in
the header and have a bunch of people link to it so dummies like myself
can find it via Google!


Maybe even better would be to gather up all kinds of information on
such problems that crop all the time, give it a catchy name like
Frequently Asked Questions or sumpin.

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12
Brian
 
D

Daniel L Elliott

Daniel L Elliott wrote:




Maybe even better would be to gather up all kinds of information on
such problems that crop all the time, give it a catchy name like
Frequently Asked Questions or sumpin.

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12
Brian

Brian, Jon, et al.

Thanks for the link. It is full of excellent information. Stuff that I
have been wondering about. Sadly, this webpage did not come up during my
search. Search engines like Google typically use the size and location
of text as a measure of its importance. If this is really such an
annoyance, somebody should devote an entire page to it (no matter how
ugly it is). I know I would have appreciated it.

I also noticed that this is referred to as "the FAQ." Does this imply that
this is the defacto, community C++ FAQ?

Thanks again!

- dan
 
V

Victor Bazarov

Daniel L Elliott said:

Brian, Jon, et al.

Thanks for the link. It is full of excellent information. Stuff that I
have been wondering about. Sadly, this webpage did not come up during my
search. Search engines like Google typically use the size and location
of text as a measure of its importance. If this is really such an
annoyance, somebody should devote an entire page to it (no matter how
ugly it is). I know I would have appreciated it.

I also noticed that this is referred to as "the FAQ." Does this imply that
this is the defacto, community C++ FAQ?

It is de facto the FAQ for this newsgroup. It definitely is mentioned here
many times, so if you cared to read the newsgroup before posting to it, you
would see it. Also, http://groups.google.com/ is a very good source of
Q that have been A'ed already.

It's too late in comp.lang.c++, but if you ever decide to visit another NG,
try looking for the FAQ right away _before_ posting. It is always a good
idea to read FAQ. Saves you and others tons of time. Saves bandwidth too.

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
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top