我正在实现一个链表。我收到错误:
P1LinkedList.cpp:145: error: call of overloaded ‘to_string(int&)’ is ambiguous
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:2604: note: candidates are: std::string std::to_string(long long int)
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:2610: note: std::string std::to_string(long long unsigned int)
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:2616: note: std::string std::to_string(long double)
(我正在使用g++ -std=c++0x
编译我的代码
引发此异常的方法:
std::string P1LinkedList::print(){
std::string print = "";
iterator itr = begin();
for(int i = 0; i < theSize; i++){
print += std::to_string(itr.current->data) + " ";
itr++;
}
return print;
}
current
是迭代器指向的当前节点。data
是 Node 类中的int
。
节点.h:
#ifndef Node_h
#define Node_h
struct Node{
int data;
Node* next;
Node* prev;
Node(const int & d = 0, Node *p = 0, Node *n = 0);
};
#endif
我的 const_iterator.h 文件的一部分,其中声明了current
(我的iterator
类从"const_iterator"类扩展而来:
protected:
Node *current;
int& retrieve() const;
const_iterator(Node*p);
friend class P1LinkedList;
};
我正在尝试将data
作为常规int
值传递to_string
,我认为itr.current->data
正在这样做,但是我可能会收到此错误,因为它不是传递数据的 int 值,而是指向它的指针?
@NathanOliver所说的是正确的,应该有一个 'int' 的模板实例化。 错误消息基本上指出,编译器不确定要使用函数的哪个"版本"(=重载(,它对输入类型long long int
unsigned long long int
long long double
具有(可能不同的(实现,但您要求它使用int
进行操作。同样,int 应该有一个重载,所以这有点奇怪。
为了在当前环境中运行程序,您可以做的是通过强制转换为int64_t来使用建议的重载之一,从而使用其中一个可用函数。
print += std::to_string(static_cast<long long int>(itr.current->data)) + " ";
参考:
在这里,您可以找到应该存在的函数列表。确保通过定义以_
在 std 命名空间中操作开头的预处理器宏来正确#include <string>
而不是调用未定义的行为,但我认为这不是这里的问题。