我一直在为我的模板的一部分收到此错误,因为这工作正常。我很乐意自己处理这个错误,但我甚至都不知道这意味着什么。也许我的运算符重载语法不正确?但即使没有我的操作员加载方法,我仍然会得到相同的错误
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;
}
答案 0 :(得分:3)
这是问题所在:
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
或const *char
。因此'错误:'operator ='消息的模糊重载。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
原帖:
这是operator=
重载的奇怪签名:
Node<T> & Node<T>::operator = (const Node<T>* & nd)
通常只是
Node<T> & Node<T>::operator = (const Node<T> & nd)
没有星号。这可能会消除编译器抱怨的歧义。当然,您必须在该功能中将->
更改为.
。
答案 1 :(得分:0)
您可以将operator =更改为:
Node<T>& operator=(const Node<T>* & nd){
parent = nd->parent;
left = nd->left;
right = nd->right;
data = nd->data;
isBlack = nd->isBlack;
return* this;
}
beacause operator =不在Node类之外。
答案 2 :(得分:0)
您也应该对复制构造函数进行与上面相同的更改。函数的语法意味着您要将节点*分配给一个没有意义的节点对象。
例如:
node1 = &node2; // Assign a pointer to an object. I'm guessing that you don't want to do this. This is not a sensible operation
node1 = node2; // assign one object to another. This is what you want to do right?
顺便说一下,你真的需要向我们展示一个如何使用类的例子。编写一个简单的主函数,显示您实际上是如何尝试进行分配,然后将其添加到您的帖子中,以便其他人可以查看和编译您正在做的事情。
此外,我不清楚你为什么重载operator =和copy构造函数。由于您没有深度复制指针,因此没有理由首先执行此操作。不必要地编写这些功能并不是一个好主意。在下面的代码中,您可以看到节点可以很好地复制。那是因为你的赋值运算符不会被调用。你必须在你的来源做一些奇怪的事情。如果您需要更多帮助,请修改此示例以向我们展示如何导致编译器错误。我们需要具体看到如何复制节点对象/指针。
int _tmain(int argc, _TCHAR* argv[])
{
Node<int> node1;
Node<int> node2;
Node<int> node3;
Node<int> node4;
node1.left = &node2;
node1.right = &node3;
node1.parent = &node4;
node1.isBlack = true;
Node<int> node5;
node5 = node1;
return 0;
}