Template class not converting

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 2
Reputation: sirbriggs is an unknown quantity at this point 
Solved Threads: 0
sirbriggs sirbriggs is offline Offline
Newbie Poster

Template class not converting

 
0
  #1
Sep 16th, 2008
This is a stripped down version of an error I'm getting in a different program, but this is simpler. The problem comes from if I try to set something that isn't an int in MyClass
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class MyClass
  7. {
  8. private:
  9. T data;
  10. public:
  11. void Set(T const &);
  12. void Print();
  13. };
  14.  
  15.  
  16. template <typename T> void MyClass<T> ::Set(T const &d)
  17. {
  18. data = d;
  19. }
  20.  
  21.  
  22. template <typename T> void MyClass<T>::Print()
  23. {
  24. cout << data << endl;
  25. }
  26.  
  27. int main()
  28. {
  29. MyClass<int> c;
  30.  
  31. c.Set("boo");
  32. c.Print();
  33.  
  34. return 0;
  35. }

Here is the compile results:
  1. test.cpp: In function ‘int main()’:
  2. test.cpp:31: error: invalid conversion from ‘const char*’ to ‘int
  3. test.cpp:31: error: initializing argument 1 of ‘void MyClass<T>::Set(T) [with T = int]

Also I got the skeleton of this code from somewhere where they used vectors and iterators with none of the problems of this
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Template class not converting

 
1
  #2
Sep 16th, 2008
>invalid conversion from ‘const char*’ to ‘int’
Yep, that's what should happen. You say that Set should take a parameter of type T, where T is specified by the template class. If you pass a value that doesn't match the type of the template class and isn't compatible, you'll get an error just as readily as if you did this:
  1. int main()
  2. {
  3. int foo = "boo";
  4. }
>where they used vectors and iterators with none of the problems of this
They probably weren't doing the same thing. Why don't you post the code you're trying to emulate and we can tell you where you got it wrong.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Template class not converting

 
0
  #3
Sep 16th, 2008
Haven't you declared c as a MyClass<int> in line 29, so why are you then trying to call Set(int const &d) with a const char*?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2
Reputation: sirbriggs is an unknown quantity at this point 
Solved Threads: 0
sirbriggs sirbriggs is offline Offline
Newbie Poster

Re: Template class not converting

 
0
  #4
Sep 16th, 2008
I see my problem in that file now, and it's mirrored in the other one I was talking about, it seems the file I assumed was correct (since I didn't write it) had that error in it.

Sorry for wasting you're time.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 331 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC