作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的部分模板不断收到此错误,因为它工作正常。我很想自己处理这个错误,但我什至不知道这意味着什么。也许我的运算符重载语法不正确?但即使没有我的运算符加载方法,我仍然会遇到同样的错误
node.h(12): error C2593: 'operator =' is ambiguous
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(992): could be 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(987): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(987): or 'std::basic_string<_Elem,_Traits,_Alloc> &std::basic_string<_Elem,_Traits,_Alloc>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::string, int)'
1> c:\users\aaron\documents\visual studio 2012\projects\redblacktreefinal\redblacktreefinal\node.h(10) : while compiling class template member function 'Node<T>::Node(void)'
1> with
1> [
1> T=std::string
1> ]
1> c:\users\aaron\documents\visual studio 2012\projects\redblacktreefinal\redblacktreefinal\redblacktree.cpp(145) : see reference to function template instantiation 'Node<T>::Node(void)' being compiled
1> with
1> [
1> T=std::string
1> ]
1> c:\users\aaron\documents\visual studio 2012\projects\redblacktreefinal\redblacktreefinal\redblacktree.cpp(203) : see reference to class template instantiation 'Node<T>' being compiled
1> with
1> [
1> T=std::string
1> ]
1> c:\users\aaron\documents\visual studio 2012\projects\redblacktreefinal\redblacktreefinal\redblacktree.cpp(197) : while compiling class template member function 'bool RedBlackTree<T>::remove(T)'
1> with
1> [
1> T=std::string
1> ]
1> c:\users\aaron\documents\visual studio 2012\projects\redblacktreefinal\redblacktreefinal\redblacktreefinal.cpp(240) : see reference to function template instantiation 'bool RedBlackTree<T>::remove(T)' being compiled
1> with
1> [
1> T=std::string
1> ]
1> c:\users\aaron\documents\visual studio 2012\projects\redblacktreefinal\redblacktreefinal\redblacktreefinal.cpp(213) : see reference to class template instantiation 'RedBlackTree<T>' being compiled
1> with
1> [
1> T=std::string
1> ]
我的node.h文件
#include <cstdlib>
#include <cstring>
template <class T>
class Node {
public :
Node<T>(void) {
parent = NULL;
left = NULL;
right = NULL;
data = NULL;
isBlack = false;
}
Node<T>(T x){
parent = NULL;
left = NULL;
right = NULL;
data = x;
isBlack = false;
}
Node<T>(const Node<T>* & nd){
parent = nd->parent;
left = nd->left;
right = nd->right;
data = nd->data;
isBlack = nd->isBlack;
}
Node<T> & Node<T>::operator = (const Node<T>* & nd){
parent = nd->parent;
left = nd->left;
right = nd->right;
data = nd->data;
isBlack = nd->isBlack;
return* this;
}
Node<T>* parent;
Node<T>* left;
Node<T>* right;
T data;
bool isBlack;
private :
};
如何使用
void part2()
{
cout << endl << endl << "REDBLACKTREE<STRING>";
cout << endl << "insert file and print contents (load, dump)" << endl;
RedBlackTree<string> rb;
string fname = "part2.txt"; //should contain strings
int n = 0;
// read file and load contents into tree
string* arr = readFile<string>(fname, n);
rb.load(arr, n);
// read contents from tree into array
int out_n = 0;
string* out = rb.dump(out_n);
// print dumped contents
int count = 0;
for(int i=0; i < out_n; i++){
if(count % 5 == 0){
cout << endl;
}
cout << left << setw(13) << out[i];
count ++;
}
cout << endl;
statsPrint(rb, 174, 5, 9, true);
// remove all items from tree
cout << endl << endl << "empty tree one item at a time";
for(int i=0; i < n; i++){
rb.remove(arr[i]);
}
statsPrint(rb, 0, 0, 0, true);
delete[] arr;
delete[] out;
}
最佳答案
问题是:
Node<T>(void) {
parent = NULL;
left = NULL;
right = NULL;
data = NULL; // <----- Remove this line - this is causing the errors
isBlack = false;
}
编辑:这是一个隔离此错误的小 C++ 程序:
#include <string>
int main()
{
std::string data;
data = NULL; // error: ambiguous overload for ‘operator=’ in ‘data = 0’
return 0;
}
根据错误消息,NULL
(即 0
)似乎可以解释为 char
或 常量 *字符
。因此出现“错误:‘operator=’消息的不明确重载。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
原帖:
这是 operator=
重载的奇怪签名:
Node<T> & Node<T>::operator = (const Node<T>* & nd)
通常只是
Node<T> & Node<T>::operator = (const Node<T> & nd)
没有星号。这可能会消除编译器提示的歧义。当然,您必须在该函数中将 ->
更改为 .
。
关于c++ - 错误 C2593 : 'operator =' is ambiguous For string template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256044/
我是一名优秀的程序员,十分优秀!