|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 stevenmtroy 于 2020-9-19 13:05 编辑
请问这样写为什么编译器总报错:不可识别的模板定义/声明?
#include <iostream>
template <typename T> Test<T> operator++(Test<T>& t);
template <typename T>
class Test {
T a;
T b;
public:
Test(int a, int b)
{
this->a = a;
this->b = b;
}
void Print()const
{
std::cout << a << " , " << b;
}
template <typename T>
friend Test<T> operator++(Test<T>& t);
};
int main()
{
Test<int> test(0, 0);
(++test).Print();
}
template <typename T> Test<T> operator++(Test<T>& t)
{
return Test<T>(++t.a, ++t.b);
}
是要实现++操作符的重载吗?
- #include <iostream>
- //template <typename T> Test<T> operator++(Test<T>& t);
- template <typename T>
- class Test {
- T a;
- T b;
- public:
- Test(T a, T b)
- {
- this->a = a;
- this->b = b;
- }
- Test<T> & operator ++()
- {
- a++;
- b++;
- return * this;
- }
- void Print()const
- {
- std::cout << a << " , " << b;
- }
-
- // template <typename T>
- // friend Test<T> operator++(Test<T>& t);
- };
- int main()
- {
- Test<int> test(0, 0);
- (++test).Print();
- return 0;
- }
- //template <typename T> Test<T> operator++(Test<T>& t)
- //{
- // return Test<T>(++t.a, ++t.b);
- //}
复制代码
|
|