【发布时间】:2019-04-01 00:17:42
【问题描述】:
(为了简单起见,代码被简化了) 我想创建一个带有模板 E A 的测试类,以及一个只有 E 模板的测试类。当我这样做并尝试编译我的代码时,我得到了这些错误:
错误 C2976:“测试”:模板参数太少
注意:参见“测试”的声明
错误 C2244:“Test::Output”:无法将函数定义与 现有声明
错误 C2662: 'void Test::Output(void)': 无法转换 'this' 从“测试”到“测试&”的指针
错误 C2514:“测试”:类没有构造函数
#include <iostream>
#include <string>
template <typename E, typename A>
class Test
{
public:
Test(E *e = nullptr, A *a = nullptr) : a(e), b(a) {}
void Output();
private:
E * a;
A *b;
};
template <typename E, typename A>
void Test<E, A>::Output()
{
std::cout << " " << *a << " " << *b;
}
template <typename E>
class Test
{
public:
Test(E *e = nullptr, std::string *a = nullptr) : a(e), b(a) {}
void Output();
private:
E * a;
std::string *b;
};
template<typename E>
void Test<E>::Output()
{
std::cout << *a << *b;
}
int main()
{
int a = 10;
std::string str = "hi";
Test<int> t(&a, &str);
t.Output();
return 0;
}
【问题讨论】: