C ++中的“运算符”错误无匹配

时间:2022-08-09 22:43:40
#include <iostream>
#include <string>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <map>

using namespace std;


struct SBLnode {
    string name;
    SBLnode *next;
    SBLnode * left, * right;
};

struct Queue {
    SBLnode * first, * last;
};

typedef SBLnode* BST;


struct SBL {
    Queue q;
    BST root;
};

void SBL_init (SBL& sbl) {

    sbl = NULL;

}

I keep getting the following error in GCC when compiling...

编译时,我在GCC中一直收到以下错误...

error: no match for ‘operator=’ (operand types are ‘SBL’ and ‘long int’)
  sbl = NULL;
      ^

This error basically is for the line sbl = NULL and it would be great if someone could explain to me exactly what that error actually means.

这个错误基本上是针对行sbl = NULL的,如果有人能够向我解释这个错误究竟意味着什么,那就太好了。

5 个解决方案

#1


4  

It can't find the operator= for SBL &SBL::operator=(const long int &rhs). There is a better practice. One option is to use a pointer and set it to NULL. NULL evaluates to 0. There is no operator which assigns an int intriniscally to your SBL struct object.

它找不到operator = for SBL&SBL :: operator =(const long int&rhs)。有一个更好的做法。一种选择是使用指针并将其设置为NULL。 NULL的计算结果为0.没有运算符在内部为您的SBL结构对象赋予int。

Or define a const static instance of the struct with the initial values and then simply assign this value to your variable whenever you want to reset it.

或者使用初始值定义struct的const静态实例,然后只要您想重置它,就可以将此值分配给变量。

For example:

static const struct SBL EmptyStruct;

This uses static initialization to set the initial values.

这使用静态初始化来设置初始值。

Then, in init you can write:

然后,在init中你可以写:

sbl = EmptyStruct;

Note: Have to compile with -fpermissive in gcc or set EmptyStruct = { }. The reason why you have to set -fpermissive is listed here for GCC 4.6. GCC 4.4 needs EmptyStruct = { }.

注意:必须在gcc中使用-fpermissive进行编译或设置EmptyStruct = {}。这里列出了为GCC 4.6设置-fpermissive的原因。 GCC 4.4需要EmptyStruct = {}。

Here is your program running. Initially it prints "initial" twice and on the third time, it prints empty string. Meaning, it was set to nothing by the assignment in the init function.

这是你的程序运行。最初它打印“初始”两次,第三次打印空字符串。意思是,它在init函数中的赋值设置为空。

int main() 
{   
    struct SBLnode initial;
    initial.name = "initial";
    struct Queue q;
    q.first = &initial;
    cout << q.first->name << endl;
    struct SBL testInit;
    testInit.q = q;
    SBL_init(testInit);
    cout << testInit.q.first->name << endl;

    return 0;
}

http://ideone.com/Ecm6I9

#2


1  

void SBL_init (SBL& sbl) {

    sbl = NULL;

}

Others have already pointed out why that line doesn't compile. Perhaps I can suggest an alternative solution. Instead of providing an init function, why not give all of your structures constructors like so? Is there some reason that you can't provide those? The operator= and copy constructor don't need to be defined if shallow copying of pointers is what you want. Since nodes typically need to be moved around I'm guessing that a shallow copy is fine. You can certainly use the nullptr if using c++ 11 rather than 0. I'm not a big fan of the NULL macro and opinions often vary with regards to NULL.

其他人已经指出了为什么这条线不能编译。也许我可以建议一个替代解决方案。而不是提供init函数,为什么不给所有结构构造函数这样?有什么理由不能提供吗?如果指针的浅层复制是你想要的,则不需要定义operator =和copy构造函数。由于节点通常需要移动我猜测浅拷贝是好的。如果使用c ++ 11而不是0,你当然可以使用nullptr。我不是NULL宏的忠实粉丝,并且意见通常因NULL而异。

struct SBL {
    SBL() : root(0) {}
    Queue q;
    BST root;
};

struct Queue {
    Queue() : first(0), last(0) {}
    SBLnode * first, * last;
};

#3


1  

NULL is a macro which expands to the integer literal 0. There is no intrinsic or user-defined operator which can assign an integer to an object of type SBL.

NULL是一个扩展为整数0的宏。没有内部或用户定义的运算符可以将整数分配给SBL类型的对象。

It looks like you are treating sbl as a pointer; but it is not a pointer, it is a reference.

看起来你正在将sbl视为指针;但它不是指针,它是一个参考。

You probably wanted to write this instead:

你可能想写这个:

void SBL_init (SBL& sbl) {
    sbl.root = NULL;
}

This initializes sbl by nulling out its member pointers.

这通过使其成员指针归零来初始化sbl。

As others have commented, nullptr is preferred in C++11:

正如其他人所评论的那样,nullptr在C ++ 11中是首选:

void SBL_init (SBL& sbl) {
    sbl.root = nullptr;
}

#4


0  

This error means that operator= , which is a function, is not defined in struct SBL. It is required when you write

此错误表示在struct SBL中未定义operator =,这是一个函数。写作时需要它

sbl = NULL;

Solution:

provide SBL& operator=( const long int& i); in struct SBL.

提供SBL&operator =(const long int&i);在struct SBL中。

In fact I think that you would like something alike SBL& operator=( BST b):

事实上,我认为你想要类似SBL&operator =(BST b):

struct SBL {
    Queue q;
    BST root;
    SBL& operator=( BST b) {
      root = b;
      return *this;
    }
};

#5


0  

It is trying to find an assignment operator that has the form

它试图找到具有该表单的赋值运算符

 SBL &SBL::operator=(const long int &rhs):#

and cannot find one.

而且找不到一个。

I guess you were thinking about the pointers.

我猜你在考虑指针。

#1


4  

It can't find the operator= for SBL &SBL::operator=(const long int &rhs). There is a better practice. One option is to use a pointer and set it to NULL. NULL evaluates to 0. There is no operator which assigns an int intriniscally to your SBL struct object.

它找不到operator = for SBL&SBL :: operator =(const long int&rhs)。有一个更好的做法。一种选择是使用指针并将其设置为NULL。 NULL的计算结果为0.没有运算符在内部为您的SBL结构对象赋予int。

Or define a const static instance of the struct with the initial values and then simply assign this value to your variable whenever you want to reset it.

或者使用初始值定义struct的const静态实例,然后只要您想重置它,就可以将此值分配给变量。

For example:

static const struct SBL EmptyStruct;

This uses static initialization to set the initial values.

这使用静态初始化来设置初始值。

Then, in init you can write:

然后,在init中你可以写:

sbl = EmptyStruct;

Note: Have to compile with -fpermissive in gcc or set EmptyStruct = { }. The reason why you have to set -fpermissive is listed here for GCC 4.6. GCC 4.4 needs EmptyStruct = { }.

注意:必须在gcc中使用-fpermissive进行编译或设置EmptyStruct = {}。这里列出了为GCC 4.6设置-fpermissive的原因。 GCC 4.4需要EmptyStruct = {}。

Here is your program running. Initially it prints "initial" twice and on the third time, it prints empty string. Meaning, it was set to nothing by the assignment in the init function.

这是你的程序运行。最初它打印“初始”两次,第三次打印空字符串。意思是,它在init函数中的赋值设置为空。

int main() 
{   
    struct SBLnode initial;
    initial.name = "initial";
    struct Queue q;
    q.first = &initial;
    cout << q.first->name << endl;
    struct SBL testInit;
    testInit.q = q;
    SBL_init(testInit);
    cout << testInit.q.first->name << endl;

    return 0;
}

http://ideone.com/Ecm6I9

#2


1  

void SBL_init (SBL& sbl) {

    sbl = NULL;

}

Others have already pointed out why that line doesn't compile. Perhaps I can suggest an alternative solution. Instead of providing an init function, why not give all of your structures constructors like so? Is there some reason that you can't provide those? The operator= and copy constructor don't need to be defined if shallow copying of pointers is what you want. Since nodes typically need to be moved around I'm guessing that a shallow copy is fine. You can certainly use the nullptr if using c++ 11 rather than 0. I'm not a big fan of the NULL macro and opinions often vary with regards to NULL.

其他人已经指出了为什么这条线不能编译。也许我可以建议一个替代解决方案。而不是提供init函数,为什么不给所有结构构造函数这样?有什么理由不能提供吗?如果指针的浅层复制是你想要的,则不需要定义operator =和copy构造函数。由于节点通常需要移动我猜测浅拷贝是好的。如果使用c ++ 11而不是0,你当然可以使用nullptr。我不是NULL宏的忠实粉丝,并且意见通常因NULL而异。

struct SBL {
    SBL() : root(0) {}
    Queue q;
    BST root;
};

struct Queue {
    Queue() : first(0), last(0) {}
    SBLnode * first, * last;
};

#3


1  

NULL is a macro which expands to the integer literal 0. There is no intrinsic or user-defined operator which can assign an integer to an object of type SBL.

NULL是一个扩展为整数0的宏。没有内部或用户定义的运算符可以将整数分配给SBL类型的对象。

It looks like you are treating sbl as a pointer; but it is not a pointer, it is a reference.

看起来你正在将sbl视为指针;但它不是指针,它是一个参考。

You probably wanted to write this instead:

你可能想写这个:

void SBL_init (SBL& sbl) {
    sbl.root = NULL;
}

This initializes sbl by nulling out its member pointers.

这通过使其成员指针归零来初始化sbl。

As others have commented, nullptr is preferred in C++11:

正如其他人所评论的那样,nullptr在C ++ 11中是首选:

void SBL_init (SBL& sbl) {
    sbl.root = nullptr;
}

#4


0  

This error means that operator= , which is a function, is not defined in struct SBL. It is required when you write

此错误表示在struct SBL中未定义operator =,这是一个函数。写作时需要它

sbl = NULL;

Solution:

provide SBL& operator=( const long int& i); in struct SBL.

提供SBL&operator =(const long int&i);在struct SBL中。

In fact I think that you would like something alike SBL& operator=( BST b):

事实上,我认为你想要类似SBL&operator =(BST b):

struct SBL {
    Queue q;
    BST root;
    SBL& operator=( BST b) {
      root = b;
      return *this;
    }
};

#5


0  

It is trying to find an assignment operator that has the form

它试图找到具有该表单的赋值运算符

 SBL &SBL::operator=(const long int &rhs):#

and cannot find one.

而且找不到一个。

I guess you were thinking about the pointers.

我猜你在考虑指针。