| | |
Template class not converting
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 2
Reputation:
Solved Threads: 0
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
Here is the compile results:
Also I got the skeleton of this code from somewhere where they used vectors and iterators with none of the problems of this
c++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; template <typename T> class MyClass { private: T data; public: void Set(T const &); void Print(); }; template <typename T> void MyClass<T> ::Set(T const &d) { data = d; } template <typename T> void MyClass<T>::Print() { cout << data << endl; } int main() { MyClass<int> c; c.Set("boo"); c.Print(); return 0; }
Here is the compile results:
C++ Syntax (Toggle Plain Text)
test.cpp: In function ‘int main()’: test.cpp:31: error: invalid conversion from ‘const char*’ to ‘int’ 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
>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:
>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.
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)
int main() { int foo = "boo"; }
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
![]() |
Similar Threads
- C++ version to Java (Java)
- Class Template used for stack (C++)
- error C2664 (C++)
- File Operations (C++)
- Problem converting Selection Sort to Class (C++)
- template array initialization (C++)
- Help with fstream (C++)
Other Threads in the C++ Forum
- Previous Thread: Rounding to the integer
- Next Thread: Accessing a constructor of a different class
Views: 331 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






