c++ - 如何正确使用模板?我的模板 <> 存在数据类型问题

标签 c++ types prototype compiler-errors

编辑 2011 年 11 月 3 日晚上 9:37:

这是我的错误:

 error: no matching function for call to "charList_join(const char [1], CISP430_A5::linked_list<char>&)"

这是我的原型(prototype):

template <typename Item>
string charList_join(const char* glue, linked_list<char> pieces);

这是我的函数调用:

charList_join("", usedChars)

其中 usedChars 是 static linked_list<char>charList_join() 的相同范围内声明被称为。

编辑 2011 年 11 月 4 日上午 8:45: 好的,下面是我的代码,删除了不必要的功能:

[sehe:从 pastebin 编辑到 github]

  • 浏览 online at github
  • 用git下载:

     git clone git://gist.github.com/1340832.git
    

我在 premute_append.cpp 的第 57 行第 68 列收到错误。我已经包含了 makefile,因此您可以根据需要尝试构建它。此时我只收到一个错误,但我完全不知道这意味着什么。

如果您尝试编译,错误将如下所示:

[cisw320b_stu022@scc-bdiv-cis assn5]$ make
g++ -c main.cpp
g++ -c permute_append.cpp
permute_append.cpp: In member function âCISP430_A5::linked_list<std::basic_string<char> > CISP430_A5::permute_append::permute(CISP430_A5::linked_list<char>)â:
permute_append.cpp:57:68: error: no matching function for call to âcharList_join(const char [1], CISP430_A5::linked_list<char>&)â
make: *** [permute_append.o] Error 1

知道我为什么会收到此错误吗?

最佳答案

你说:

and this is my prototype:

string charList_join(const char* glue, linked_list<char> pieces);

但事实并非如此。根据the code you provided ,你的声明是:

template <typename Item>
string charList_join(const char* glue, linked_list<char> pieces);

这意味着你需要这样调用它:

charList_join<char>("", usedChars)

但是,您实际上并没有在此函数中使用 Item,因此您应该只删除 template 说明符(从声明和定义中)并将其称为你之前是。更好的是,通过 const-reference 传递列表:

string charList_join(const char* glue, const linked_list<char>& pieces);

通话仍然存在:

charList_join("", usedChars)

关于c++ - 如何正确使用模板?我的模板 <> 存在数据类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004824/

相关文章:

c++ - 使用参数和 this 关键字时的评估顺序

c++ - 在 Visual C++ 2010 中构建 Tesseract

Javascript 重写原型(prototype)方法

通过 Object.create() 的 Javascript 原型(prototype)

c++ - 在单个链表中搜索特定值

c++ - 在C++类成员函数上使用#ifdef防护措施是否安全?

c# - 长期在 float ,为什么?

pointers - 为什么在 Go 中不能将整数添加到 "dereferenced"指针变量?

c# - 可空通用声明?

javascript - 如何去抖一个事件? (语法错误?)