【发布时间】: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 example。
file是什么? -
因为
test很可能被当作函数声明而不是变量。 -
file 是文件的名称,构造函数将使用该文件读取数据并将其添加到 Binary_tree 类中定义的列表中。
-
您不清楚“创建新问题”的哪一部分?