2012-01-16 145 views
0

我找不到问题。我试图用char作为参数类模板:C++模板char类

#include <iostream> 

using namespace std; 

template <class Type1, class Type2> class myclass 
{ 
    Type1 i; 
    Type2 j; 
public: 
    myclass(Type1 a, Type2 b) {i=a; j=b;} 
    void show() { cout << i << ' ' << j << '\n'; } 
}; 

void main() 
{ 
    myclass<int, double> ob1(10, 0.23); 
    myclass<char, char *> ob2('X', Just show "); 
    ob1.show(); 
    ob2.show(); 
} 
+1

那么,你的问题是什么?出了什么问题?另外,下次请格式化您的代码。 – 2012-01-16 13:28:28

+0

也许你应该告诉我们你的问题是什么... – Grizzly 2012-01-16 13:28:32

+0

在ideone中运行这似乎工作正常。 http://ideone.com/xF9wC – 2012-01-16 13:31:12

回答

1

Just之前缺少左报价:

myclass<char, char *> ob2('X', Just show "); 
//       ^
// should be: 
myclass<char, char *> ob2('X', "Just show"); 

不过,请注意,你应该使用const char*当你要允许通过字符串和这个具有所有权的问题。改用std::string代替。

1

你缺少在myclass<char, char *> ob2('X', Just show ");",它应该是myclass<char, char*> ob2('X', "Just show ");。此外,该类型应该大概myclass<char, const char*>代替myclass<char, char*>