2014-05-03 34 views
0

所以,我有一个链接问题。答案可能很简单,但我想我很烦。我定义了一个用于计算需要stringstream的东西的类。LNK2019与模板

头文件中的有关章节:

#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:错误LNK2019:无法解析的外部符号“public:__thiscall Finder :: Finder(void)”(?? 0?$ Finder @ H @@ QAE @ XZ)在函数中引用_main

1> driver.obj:error LNK2019:无法解析的外部符号“publi c:__thiscall Finder ::〜Finder(void)“(?? 1?$ Finder @ H @@ QAE @ XZ)在函数__catch中引用$ _main $ 0

+0

什么是*:'〜Finder(T)'析构函数没有参数。它应该是'〜Finder()',没有模板参数,也没有模板参数集。或者,您的流插入器朋友中的“Finder '?请发布*真实*代码。 * *确切*错误消息逐字*总是*一个好主意。 – WhozCraig

+0

首先,错误消息: 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 –

+0

在问题**中发布错误**。另外,'thing.check(istringstream(expression))'不是有效的标准C++。如果你使用非标准的编译器扩展(如VC++),它可能会工作,但它不是标准的。 – WhozCraig

回答

1

首先,不要将输入限制为字符串流。除非您有坚实的理由不这样做,否则请使用通用的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); 
} 

公告有一个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] 

与成员函数模板注意,像所有的函数模板,该类型是推断从过去了,没有指定的参数(尽管他们可能会迫使特定类型的,我们并没有这样做)。

无论如何,希望它有帮助。

+0

谢谢!这不是我想要做的,但它确实间接地清除了我遇到的问题。 –