C++ LNK2019带模板
所以,我在链接方面遇到了一个问题。答案可能很简单,但我想我疯了。我定义了一个类来计算采用stringstream的事物 头文件的相关部分:C++ LNK2019带模板,c++,templates,constructor,linker,lnk2019,C++,Templates,Constructor,Linker,Lnk2019,所以,我在链接方面遇到了一个问题。答案可能很简单,但我想我疯了。我定义了一个类来计算采用stringstream的事物 头文件的相关部分: #include <sstream> using namespace std; template<class T> class Finder { public: Finder(istringstream& input) {}; ~Finder() {}; template<typename T&
#include <sstream>
using namespace std;
template<class T>
class Finder {
public:
Finder(istringstream& input) {};
~Finder() {};
template<typename T> Finder(T& input) {};
template<typename T> ~Finder() {};
T check(istringstream&);
template<typename T> friend ostream& operator << (ostream&, Finder<t>&);
};
template<class T>
T Finder<T>::check(istringstream& input)
#包括
使用名称空间std;
模板
类查找器{
公众:
查找器(istringstream&input){};
~Finder(){};
模板查找器(T&input){};
模板~Finder(){};
T检查(istringstream&);
模板friend ostream&operator driver.obj:错误LNK2019:未解析的外部符号“public:u thiscall Finder::Finder(void)”(?0$Finder@H@@QAE@XZ)在函数_main中引用
1> driver.obj:错误LNK2019:未解析的外部符号“public:u thiscall Finder::~Finder(void)”(?1$Finder@H@@QAE@XZ)首先,不要将您的输入仅限于字符串流。除非您有充分的理由这样做,否则请改用泛型std::istream
。您的类将更加健壮,能够从多个源流类型中获取输入,而不仅仅是std::istringstream
(例如文件流或输入控制台)
其次,我几乎可以肯定这就是你想要做的:
并这样调用:
Simple<int> simple(3.1415926); // U will be type double
simple.func("test"); // U will be type const (&char)[5]
Simple(3.1415926);//U将是双精度类型
simple.func(“test”);//U将是const(&char)类型[5]
请注意,对于成员函数模板,与所有函数模板一样,类型是从传递的参数中推断出来的,而不是指定的(虽然它们可能强制特定类型,但我们在这里不做)
无论如何,希望它能有所帮助。那是什么:~Finder(T)
析构函数没有参数。它应该是~Finder()
没有模板参数,也没有模板参数设置。或者这是:Finder
在您的流插入器朋友中?请发布真实的代码。准确的错误消息总是一个好主意。首先,错误消息:1>driver.obj:error LNK2019:未解析的外部符号“public:\u thiscall Finder::Finder(void)”(??0?$Finder@H@@QAE@XZ)在函数_main1>driver.obj中引用:错误LNK2019:未解析的外部符号“public:u thiscall Finder::~Finder(void)”(?1$Finder@H@@QAE@XZ)函数uu catch$\u main$0中引用了问题中的错误。另外,thing.check(istringstream(表达式))<代码>不是有效的标准C++。如果使用非标准编译器扩展(如VC++),它可能会起作用。,但它不是标准的。那么,为什么在这段代码中构造函数或析构函数前面有template
?编译器是否至少尝试警告您,内部模板参数t
与外部模板参数有阴影?很抱歉,这是一个新问题。使用VS2012,编译不应该是一个问题。请输入全文这并不是我真正想做的,但它间接地解决了我遇到的问题。
#include <iostream>
// forward declare class
template<class T>
class Finder;
// forward declare ostream inserter
template<class T>
std::ostream& operator <<(std::ostream&, const Finder<T>&);
// full class decl
template<class T>
class Finder
{
// friend only the inserter that matches this type, as opposed to
// all inserters matching all T for Finder
friend std::ostream& operator<< <>(std::ostream&, const Finder<T>&)
{
// TODO: implement inserter code here
}
public:
Finder()
{
// TODO: default initialization here
};
Finder(const T& value)
{
// TODO: initialize directly from T value here.
}
Finder(std::istream& input)
{
// TODO: initialize from input stream here.
}
~Finder()
{
// TODO: either implement this or remove it outright. so far
// there is no evidence it is even needed.
}
T check(std::istream& input)
{
// TODO: implement check code here. currently just returning a
// value-initialized T. you will change it as-needed
return T();
};
};
int main()
{
Finder<int> f;
std::istringstream iss("1 2 3");
f.check(iss);
}
template<class T>
class Simple
{
public:
// a template member constructor
template<typename U>
Simple(const U& val)
{
}
// a regular template member
template<typename U>
void func(U value)
{
}
};
Simple<int> simple(3.1415926); // U will be type double
simple.func("test"); // U will be type const (&char)[5]