2012-01-16 93 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*>