943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 418
  • C++ RSS
Sep 16th, 2008
0

Template class not converting

Expand Post »
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
c++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirbriggs is offline Offline
3 posts
since Feb 2007
Sep 16th, 2008
1

Re: Template class not converting

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 16th, 2008
0

Re: Template class not converting

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*?
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 16th, 2008
0

Re: Template class not converting

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sirbriggs is offline Offline
3 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Rounding to the integer
Next Thread in C++ Forum Timeline: Accessing a constructor of a different class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC