Compiling Error in Overloaded Operator =

Dear all,

I have my Node class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <string>
#include <set>

class Node{

private:

	std::string label;
	int ID;


public:
	
	std::set<Node *> AdjacencyList;

	std::string GetNodeLabel()
	{
		return label;
	}

	int GetNodeID()
	{
		return ID;
	}

	void SetNodeLabel(std::string newLabelVal)
	{
		label=newLabelVal;
	}

	void SetNodeID(int newIDVal)
	{
		ID=newIDVal;
	}
	

	Node(std::string l, int id)
	{
		label=" ";
		ID=0;
	}
	~Node();
	Node(){};
	Node & operator = (const Node &NewNode)
	{
		if(this!=&NewNode)
		{
			this->ID=NewNode.GetNodeID();
			this->label=NewNode.GetNodeLabel();
		}

	   return *this;
	}

};


And I get the following errors for the operator= :

error C2662: 'Node::GetNodeLabel' : cannot convert 'this' pointer from 'const Node' to 'Node &'
error C2662: 'Node::GetNodeID' : cannot convert 'this' pointer from 'const Node' to 'Node &'

What am I doing wrong? I tried for too long to fix the code, but I can't.

Thanks a lot!
you're not const correct. Your getters should be const functions:

1
2
3
4
5
6
7
8
9
10
	int GetNodeID() const  // put the const here
	{
		return ID;
	}

//...
	std::string GetNodeLabel() const // and here
	{
		return label;
	}



But of course you don't really need to call the getters from within your own class.

Also this arguably get/set abuse, but that's another matter.
Thanks, this fixed the problem.
I get a run time error for the label now. When I create an array of Nodes and allocate memory for them, it says <Bad Ptr> in the value for the label (when debugging). What can this possibly be?
My guess is you have a bad pointer. =P

Are your pointers actually pointing to something?
Good guess :)

Well this is what I do:
1
2
3
4
5
6
7
8
9
10
Node *words_array; 
words_array=(Node *) malloc(no_lines*sizeof(Node)); 


.
.
.


(*words_array).SetNodeLabel(node_string); //this is where the run time error occurs because of the bad pointer for the label, i.e. the string cant be assigned to the label in the node 

Thanks a lot for helping!
p.s. node_string in not null, it has a value (checked it while debugging)
don't use malloc/free. It's a C construct. It generally should not be used in C++.

new/delete is better. It calls ctors/dtors.


1
2
3
4
5
6
7
8
9
10
// bad
words_array=(Node *) malloc(no_lines*sizeof(Node));
...
free(words_array);


// good
words_array = new Node[no_lines];
...
delete[] words_array;



(edit: just in case it wasn't clear -- this really is the cause of your problem -- it's not just a suggestion)
Last edited on
This code is like Pandora's box! Yes you are right, I used C approach. But after fixing it, I got an unresolved externals compilation error. What am I doing so wrong?


error LNK2019: unresolved external symbol "public: __thiscall Node::~Node(void)" (??1Node@@QAE@XZ) referenced in function "void __cdecl ReadNouns(char * const,struct _iobuf *)" (?ReadNouns@@YAXQADPAU_iobuf@@@Z) SemanticSearch_Functions.obj SemanticSearch

fatal error LNK1120: 1 unresolved externals C:\Users\CS User\Documents\Purdue IPFW\Master Thesis Research\SemanticSearch\Debug\SemanticSearch.exe SemanticSearch

And all I did was adding the new code instead the old one..

Thanks for your help so much!
I forgot to add that I have the class defined in a header file, and in my code I include it like this:

#include "Node.h"

Thanks again for helping me out, this project is very important to me..
99% of the time "unresolved external symbol" means you defined a function prototype, but never gave that function a body.

In this case, the function is your destructor, ~Node().

Give that a body.


EDIT:

It's somewhat strange that you had this linker error after you got a runtime error. How could the program have been running before if you had this problem?

Did you delete the dtor by accident or something?
Last edited on
I added body to the destructor, and voila! Its working! You are a genius! Thanksss so much! My lack of experience in c++ was gonna make me suffer for days :)
Topic archived. No new replies allowed.