| by Arround The Web | No comments

C++ Error: No Viable Overloaded ‘=’:

Errors are the operations that may be the result of the abnormal working of any code. The error cannot be detected at the time of writing until we have executed the code or it is compiled. Some errors prohibit the code from executing until they are removed. Today, we will also discuss an error that also occurs when the program is compiled, that is “error: no match for ‘operator=’”. This error may be caused because of reasons like: if we have passed the pointer where the string is to be passed, modifying the constant variable, etc. It is not difficult to remove the “no viable overloaded” error but the main task is to understand the error because it just shows the error without any description of the error.

Syntax

There is no such predefined syntax for this error because it is not a part of the code or the output it’s just an ambiguity that may be caused due to the wrong code. As we can see, below is an example of what the error may look like.

error: no match for ‘operator=

Example # 01:

Let us have an idea about this error and the method to resolve this. To understand it better, we will perform an example in which we are going to pass the names using objects to the function and it will then display them. We will first include the header file iostream. After that, we will declare a class named “my_object”. Inside of which, we have declared two string variables named “F_name and “L_name”, the “F_name” that indicates the first name of the person where the “L_name” indicates the last name of the person.

Next, we have declared a public constructor named “my_object()” in which we assigned null values to both the variables “F_name” and “L_name”. After that, we declared another function to which we have passed the string type variables “fn” and “ln”. Inside that, we called the name_set() method. Then, we declared two member functions “show()” and “name_set()”. When the “show() function is called, it will display the first names and last names together. Whereas, in the “set_name()” member function, we passed two string variables of string type “fn” and “ln” which we have also passed to the second constructor.

Now, using the copy assignment operator of class my_object, we take one parameter of type “my_object”. The compiler always declares it as an inline public member of any class. Inside this member, we have assigned the src.F_name to the “F_name” and src.L_name to the “L_name” that we have retained the copy of the “F_name” and “L_name”. This is passed to the members of the class my_object. Now, we have declared an object of the class my_cobject named “name1” to which we passed two strings “Anna” and “smith” as an argument. This will call the constructor and display the first name along with the last name.

After that, we created another object “name2” and then assigned the name to that object separately. After passing values to the constructor for both objects, we called the show() method which will then display the names for both objects “nam1” and “name2”. At the end of the code, we returned the null value and executed our code.

include
class my_object{
private:
    std::string F_name, L_name;
public:
    my_object(){F_name = " "; L_name = " ";}
    my_object(std::string fn, std::string ln){
        name_set(fn, ln);
    }
    void show(){std::cout << "The name is " << F_name << " " << L_name << ".\n";}
    void name_set(std::string fn, std::string ln){F_name = fn; L_name= ln;}
    my_object &operator=(const my_object &src){
    F_name = src.F_name;
    L_name = src.L_name;
    return *this;
}
 
};
int main(int argc, char** argv) {
    my_object name1("Anna", "smith");
    my_object name2;
    name2 = ("Anna" , "smith");
    name1.show();
    name2.show();
    return 0;
}

After the execution of our code, we have this error which displays that we have written the wrong code on line 24 indicating the type of error that is encountered “error: no match for ‘operator=’”. Now, we will try to resolve this error.

For resolving this error, we have multiple ways to pass the values to the constructor of any class. In the first method, we will simply assign the “name1” object to the “name2” because we have passed the same values to both objects so there is no need to pass them separately. Now we execute the code.

int main(int argc, char** argv) {

    my_object name1("Anna", "smith");
    my_object name2;
    name2 = name1;
    name1.show();
    name2.show();

After making changes to the code as shown above, we have the result given in the snippet below. We have displayed the name passed to the constructor is displayed successfully without any error.

The second method to resolve this error is when we have to pass the different values to both objects. We will simply use the class name along with the values that are to be passed to the constructor as an argument. We passed the first name “jhone” and the second name “smith”. Then, we executed the code.

int main(int argc, char** argv) {

    my_object name1("Anna", "smith");
    my_object name2;
    name2 = my_object(“jhone”, “smith”);

    name1.show();
    name2.show();

After executing the above-added code, we have the output as shown below. For the object “name1”, it displayed the name “Anna smith” and for the second object “name2” it displayed “Jhone Smith”. But this time our code worked properly without any errors in it.

Now, we will try another method for executing our code successfully. As in the above cases, we tried to assign the values to the objects using the assignment operator. But this time, we will pass the values at the time of declaration of the object. As we can see in the snippet below, at the time of the declaration of an object “name1”, we passed the values as an argument to the object repeating the same step for the “name2”. Now, we execute the code once again.

int main(int argc, char** argv) {
    my_object name1("Anna", "smith");
    my_object name2(“jhone”, “smith”);
    name1.show();
    name2.show();

After the code is executed for this time also, we haven’t encountered any error which means this will also prevent us from having any errors.

Conclusion

We have briefly discussed an error that we may face while working on functions that are accessed from classes. We have also studied the causes and methods to resolve the error “no viable overloaded”. Mostly this error is difficult to understand for new programmers so we tried for them to make it easy to get rid of this by implementing examples and also with its explanation.

Share Button

Source: linuxhint.com

Leave a Reply