Skip to main content
GameDev.net gamedev.net
🔒 Locked

operator overloading

Started by Kevn Feb 27, 2009 at 2:14 AM 4 replies 800+ views
Original Post
Kevn
Kevn
Can someone please tell me what i'm doing wrong here? code: #include #include "container.h" using namespace std; int main() { int a; Container linkedListTwo; Container linkedList; linkedListTwo.firstItem(4); linkedList.firstItem(4); if ( linkedList == linkedListTwo ) cout << "test" << endl; linkedList.addItem(5); linkedList.addItem(3); linkedList.addItem(8); linkedList.deleteList(); cin >> a; return 0; } bool operator ==(Container a, Container b) { return a.check() == b.check(); } I am trying to compare two different Container objects together. I get the error: no match for 'operator==' in 'linkedList == linkedListTwo' Originally I was trying to do the overloading as a member function but that wasn't working out too well for me. Oh and object.check() sends back an int value from the head node in the object, because each Container object is a linked list.
Aressera
Aressera
Move your operator declaration above your main method. Methods in C++ must be declared before they are used.

Also, I recommend changing your operator declaration to the following because it avoids needless copying of objects:
bool operator == ( const Container& a, const Container& b ){return a.check() == b.check();}
Kevn
Kevn
alright fixed it, thank you!

[Edited by - KBakerSR on February 27, 2009 2:30:51 AM]
Zahlman
Zahlman
Or you could just use standard library containers that already have all this functionality implemented efficiently (and are templates which allow you to make containers of whatever type you like, and don't need to treat the first element of a linked list as a special case) :/

By the way, please don't pause your programs artificially at the end. Learn to run them properly instead, from a batch file or a console window.
Kevn
Kevn
i didnt use already made containers because this was an assignment for school and we werent allowed to. I paused it like that because I wasnt on my computer and didn't feel like spending extra time to do it correctly. I was in a rush.
NotAYakk
NotAYakk
By the way, you can keep formatting and tabs and the like if you post with code in [ source ] or [ code ] tags. (Source is for lots of code, code is for snippets).

You should generally prototype your functions long before you use them. Instead of moving the implementation, declare that the function exists:
#include <iostream>#include "container.h"using namespace std;bool operator ==(Container a, Container b); // prototyped!int main(){  int a;  Container linkedListTwo;  Container linkedList;  ...


Now the main function will know that the operator== exists, and will know to look for it.

This mirrors the .h/.cpp file pattern that you will grow to know and love.

Finally, the 'for beginners' forum often generates better responses than 'general programming'.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.