0

I have a class called node defined in a header file ClassDefinition.h

#ifndef CLASS_DEFINITION_H // if-not-defined
#define CLASS_DEFINITION_H // define
#endif                     // end-if


#include<vector>

class node{
public:

std::string name;
std::vector<std::string> child_node_vector;
std::vector<int> child_distance_vector;
bool is_visited;



node& operator=(const node &rhs)
{
    child_distance_vector = rhs.child_distance_vector;
    child_node_vector = rhs.child_node_vector;
    is_visited = rhs.is_visited;
    name = rhs.name;
    return *this;
}

};

In another source code file I have some nodes defined and a vector to hold these nodes Map_Info_Reader.cpp

#include <ClassDefinition.h>
node Eforie;
node Vaslui;
node Iasi;
node Neamt;
std::vector<node> node_list;
node_list[0] = Eforie;
node_list[1] = Vaslui;
node_list[2] = Iasi;
node_list[3] = Neamt;
(other class variables are set further down in file)

Both of these are called from my main source code file DFS_Agent.cpp

#include <Class_Definition.h>
#include <Map_Info_Reader.h>

void Move_To_Node(node){

node current_node = Vaslui;
std::vector<std::string> Search_Queue;
Search_Queue.resize(1);
Search_Queue[0] = "Vaslui"

for (int i = 0; i<node_list.size(); i++)
{
    if (Search_Queue[0] == node_list[i].name)
    {
        current_node = node_list[i];

    }
}

} I get a compiler error: no operator "=" matches these operands. Operand types are node = node from line current_node = node_list[i]

11
  • For which line in your code do you get the error message? Commented Jan 19, 2015 at 13:24
  • 2
    good practice is return referencenode& operator=(const node &rhs) Commented Jan 19, 2015 at 13:26
  • 1
    What you have posted compiles fine for me. Could you post an MCVE Commented Jan 19, 2015 at 13:34
  • 1
    Just to embellish /u/triclosan's answer, you need a return *this; at the end of the assignment operator. are there any other exceptions Commented Jan 19, 2015 at 14:03
  • 1
    void Move_To_Node(node){ looks weird for me Commented Jan 19, 2015 at 20:58

1 Answer 1

2

Why do you need overloaded assignment operator at all in your case. For std::vector, bool, std::string you may omit it.

Anyway if you decide to use your own make it something like

node& operator=(const node &rhs)
{
    if (this != &rhs) // protect against invalid self-assignment
    {
        this->child_distance_vector = rhs.child_distance_vector;
        this->child_node_vector = rhs.child_node_vector;
        this->is_visited = rhs.is_visited;
        this->name = rhs.name;
    }
    return *this;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Whilst this is true, it doesn't explain why OP gets an error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.