所以,我对链接有个问题。答案可能很简单,但我想我是疯了。我定义了一个类,用于计算接受字符串流的事物。
头文件的相关部分:
#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)然后我的司机提交到最后一次呼叫:
#include <sstream>
#include <string>
#include <iostream>
#include "Finder.h"
using namespace std;
int main(int argc, char** argv) {
Finder<int> thing;
string expression;
getline(cin, expression);
while(expression[0] != 'q') {
try {
int result = thing.check(istringstream(expression));错误是: 1>driver.obj : error LNK2019:未解析的外部符号"public:__thiscall Finder::Finder(void)“(?0?$Finder@H@@QAE@XZ)在函数_main中引用。
1>driver.obj : error LNK2019:未解决的外部符号"public:__thiscall Finder::~Finder(void)“(?1?$Finder@H@QAE@XZ)在函数__catch$_main$0中引用
发布于 2014-05-03 01:55:39
首先,不要将输入限制为字符串流。使用泛型std::istream代替,除非您有充分的理由不这样做。您的类将更加健壮,并且能够从多个源流类型(而不仅仅是std::istringstream (例如文件流或输入控制台)获取输入)。
其次,我几乎肯定这就是你想要做的:
#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);
}注意,有 one T,它来自类模板本身。如果成员函数(甚至构造函数)需要辅助类型,也可以使用具有不同类型名称的模板成员函数,例如:
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]注意,与所有函数模板一样,与所有函数模板一样,类型是根据传递的参数导出的,而不是指定的(虽然它们可以强制特定类型,但这里没有)。
不管怎么说,希望能帮上忙。
https://stackoverflow.com/questions/23439205
复制相似问题