【问题标题】:calling template class constructor [duplicate]调用模板类构造函数[重复]
【发布时间】:2015-09-30 12:28:21
【问题描述】:

我很难找出调用使用模板的类的构造函数的解决方案。

--头文件

template <class Item>
class Binary_tree
{
    string file_name;
    list<Item> arr_data;
public:
    Binary_tree(string fname);
    void printArr();
};

--cpp文件

template <typename Item>
Binary_tree<Item>::Binary_tree(string fname)
{
    File_Name = fname;
    total = 0;
    root = nullptr;

    std::ifstream filestream(fname);
    string line;

    while (!filestream.eof())
    {
        filestream >> line;
        arr_data.push_back(line);
    }
}

template <typename Item>
void Binary_tree<Item>::printArr()
{
    int size = arr_data.size();

    for (int i = 0; i < size; i++)
    {
        cout << "arr_data [" << i << "] is: " << arr_data[i] << endl;
    }
} 

--main.cpp

int main(int argc, char** argv)
{
 Binary_tree<string> test(file); 
 test.printArr();

 return 0;
}

现在我遇到了 LNK1120 和 LNK2019 错误。

【问题讨论】:

  • 请提供minimal, complete, verifiable examplefile 是什么?
  • 因为test 很可能被当作函数声明而不是变量。
  • file 是文件的名称,构造函数将使用该文件读取数据并将其添加到 Binary_tree 类中定义的列表中。
  • 您不清楚“创建问题”的哪一部分?

标签: c++ templates c++11


【解决方案1】:

注意:此答案回答了原始问题,该问题已被完全不同的问题所取代!

声明是针对the Most Vexing Parse中描述的函数:

Binary_tree<string> test(Binary_tree<string>(file));

这声明了一个名为 test() 的函数,返回一个 Binary_tree&lt;string&gt; 并接受一个名为 Binary_tree&lt;string&gt; 的参数或类型 file。以下是解决问题的变体:

Binary_tree<string> test0(file);
Binary_tree<string> test1 = Binary_tree<string>(file);
Binary_tree<string> test2{Binary_tree<string>(file)};

【讨论】:

  • 我尝试了所有这些,并且不断收到以下 4 个 LNK 错误。 LNK2019 和 LNK1120
  • @Gaurav:当然。您需要定义函数模板,我猜您在单独的 .cpp 文件中执行此操作(您没有向我显示代码,我需要使用我的千里眼,这并不完全可靠)。如果将函数模板的定义放入 .cpp 文件中,则需要显式实例化它们(关于这个主题有很多问题和答案),请参阅我的 blog。最简单的方法是将函数模板的定义放在头文件中,让编译器隐式实例化它们。
  • 我看了你的页面,我知道这个概念,而且我主要是按照你展示的那样来实现它。如果你想看我的代码,那么这里是:'#include "BinaryTree.h" template Binary_tree::Binary_tree(string fname) { File_Name = fname;总计 = 0;根= nullptr; std::ifstream 文件流(fname);字符串线; while (!filestream.eof()) { 文件流 >> 行; arr_data.push_back(line); } } int main() { 字符串文件 = "HeapQuestions.txt"; Binary_tree test(file);// = (Binary_tree(file)); test.printArr();返回0; }'
  • @Gaurav:所以你的模板定义在 .cpp 中,这可能不是你想要的!将代码放入BinaryTree.h,它应该可以编译。我不知道它是否会起作用。我发现在您的代码中使用了.eof(),这肯定是错误的!您需要检查 after 阅读,例如,使用while (filestream &gt;&gt; line) { ... }(它不会读取一行,而只会读取一个单词;如果您想读取一行,则使用std::getline(filestream, line))。并不是说您也不应该将代码粘贴到注释中!你宁愿更新你的问题......
  • #include "BinaryTree.h" template &lt;typename Item&gt; Binary_tree&lt;Item&gt;::Binary_tree(string fname) { File_Name = fname; total = 0; root = nullptr; std::ifstream filestream(fname); string line; while (!filestream.eof()) { filestream &gt;&gt; line; arr_data.push_back(line); } } int main() { string file = "HeapQuestions.txt"; Binary_tree&lt;string&gt; test(file);// = (Binary_tree&lt;string&gt;(file)); test.printArr(); return 0; }
【解决方案2】:

您遇到了most vexing parse

Binary_tree<string> test(Binary_tree<string>(file));

这是一个函数声明,就像

Binary_tree<string> test(Binary_tree<string> file);

...声明了一个名为test的函数,它接受Binary_tree&lt;string&gt;作为它的参数,并返回一个Binary_tree&lt;string&gt;

【讨论】:

  • 我想创建一个 Binary_tree 对象,以后可以使用它来调用它的方法。现在我无法调用 printArr 函数。我不断收到类似的错误:表达式必须具有类类型。
猜你喜欢
  • 2013-05-30
  • 2016-04-13
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多