0

I've been searching the internet and asking my friends for an answer for a good hour to help me with this tiny little error in my code. This is what I try to run:

BinStabloMapa<string,string> m; string s1("Sarajevo"), s2("Zagreb"); m[s1] = "BiH"; m[s2] = "Hrvatska"; { BinStabloMapa<string,string> m2(m); BinStabloMapa<string,string> m3; m3=m; m.obrisi(s1); cout << m2.brojElemenata() << " "; cout << m2[s1] << " "; cout << m3.brojElemenata() << " "; cout << m3[s1] << " "; } cout << m.brojElemenata(); cout << " '" << m[s1] << "' "; m.obrisi(); cout << m.brojElemenata(); cout << " '" << m[s2] << "'"; 

And this is the error: error: invalid initialization of non-const reference of type 'BinStabloMapa< >&' from a temporary of type 'BinStabloMapa< >* const'|

It shows the error in my operator =:

template <typename TipKljuca,typename TipVrijednosti> BinStabloMapa<TipKljuca,TipVrijednosti>& BinStabloMapa<TipKljuca,TipVrijednosti>::operator =(const BinStabloMapa<TipKljuca,TipVrijednosti> &m) { if(this==&m) return this; // ERROR APPEARS HERE obrisiPomocna(korijen); korijen=0; kopiraj(korijen, m.korijen, 0); return *this; } 

I'm not quite sure what am I doing wrong here, since I've been taught to make the operator = like this: the protection of self-destrcution + the destructor + the copy constructor. The code that I'm trying to run should be able to compile, since it's the autotest for my whole program. Sorry if I didn't explained it too well, still learning C++ and all the english terms :)

3
  • Should this be tagged windows? It's often good to provide such information, but I don't see how it applies. Commented Dec 22, 2013 at 19:53
  • You've got two return statements in that function, and one's not throwing that error. What's your conclusion? Commented Dec 22, 2013 at 19:55
  • I had a guy in my last question telling me that I should tag the operating system, but I'll untag it if it doesn't fit. Oh my God, I just added a '*' infront of 'this' in my 1st return and now it's good. How did I not see this :O Commented Dec 22, 2013 at 19:55

1 Answer 1

3

Change return this to return *this.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.