|
-
April 29th, 2004, 06:00 PM
#1
Template Question
why doesnt this work?
Code:
// global
struct s
{
float x;
float y;
};
// set.h
Class CSet
{
std::vector<s> mStructData;
std::vector<int> m_intData;
template<typename T>
void inline setdata( std::vector<T>& TData )
{
if ( m_bool == TRUE )
m_StructData.assign( TData.begin(), TData.end() );
else
m_intData.assign( TData.begin(), TData.end() );
};
};
/***********************************************/
//try.h
CSet m_Set;
// try.cpp
std::vector<s> sData;
std::vector<int> intData;
if ( m_bool )
m_Set.setdata( sData );
else
m_Set.setdata( intData );
I get error C2664 ... Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
... C\...\try.cpp(178) : see reference to function template instantiation ...
Thx.
-
April 29th, 2004, 06:17 PM
#2
What's this m_bool variable? You've used it twice, but havent defined it anywhere.
And why does setdata need to be templated? If it's only to save you the trouble of writing one setdata for each of the member vectors, it's not worth it. It'll only clutter up your code and make it harder to read for other programmers (and yourself 4 months from now). Make one setdata for each of the member vectors.
We'll need to see some real compilable code to be able to answer your question further. This code doesn't make sense to me...
-
April 29th, 2004, 06:32 PM
#3
Originally posted by me
And why does setdata need to be templated? If it's only to save you the trouble of writing one setdata for each of the member vectors, it's not worth it. It'll only clutter up your code and make it harder to read for other programmers (and yourself 4 months from now). Make one setdata for each of the member vectors.
In fact... This won't work at all, and that's probably the error you're recieving.
When the compiler sees a templated function like yours, and you instantiate it for one type, it will compile the entire function with the template parameter you pass to it, and in your case that will lead to either one or the other assign line to not compile. (Wether or not the assign functions will actually be called, doesn't matter!)
If you pass a std::vector<int> to your func, the m_StructData.assign will not compile since it's assigning a std::vector<int> to a std::vector<s>. If you pass a std::vector<s>, it's the other way around. Any other type, and neither will compile.
Last edited by Assmaster; April 29th, 2004 at 06:34 PM.
-
April 30th, 2004, 03:34 AM
#4
I agree with Assmaster: overload the function - don't bother with templates.
One other point in addition to Assmaster's correct reply: "void inline" should be "inline void".
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
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
|