Thread: Problem accessing class method

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Problem accessing class method

    This has been driving me insane, and I'm not sure what's wrong:

    I have a header file (KeyValueComparator.h):
    Code:
     #ifndef KEY_COMP
      2 #define KEY_COMP
      3 
      4 #include <string>
      5 #include <iostream>
      6 #include "KeyValuePair.h"
      7 
      8 using namespace std;
      9 
     10 class KeyValueComparator {
     11         public:
     12                 bool operator() (const KeyValuePair a, const KeyValuePair b) const;
     13 };
     14 
     15 #endif
    and a .cpp file (KeyValueComparator.cpp):

    Code:
     1 #include <iostream>
      2 #include <string>
      3 #include "KeyValueComparator.h"
      4 
      5 using namespace std;
      6 
      7 bool KeyValueComparator::operator() (const KeyValuePair a, const KeyValuePair b) const {
      8         if(a.count==b.count) {
      9                 if(a.str.compare(b.str)<=0)
     10                         return true;
     11                 else {
     12                         return false;
     13                 }
     14         }
     15         else {
     16                 if(a.count>b.count) {
     17                         return true;
     18                 }
     19                 else {
     20                         return false;
     21                 }
     22         }
     23 }

    and finally a running file (Main.cpp):

    Code:
      1 #include <algorithm>
      2 #include <map>
      3 #include <vector>
      4 #include <iostream>
      5 #include <fstream>
      6 #include <string>
      7 #include "KeyValuePair.h"
      8 #include "KeyValueComparator.h"
      9 using namespace std;
     10 
     11 int main () {
     12 
     13         KeyValuePair a;
     14         KeyValuePair b;
     15 
     16         KeyValueComparator cmp;
     17         bool check = cmp.operator(a,b);
     18 }
    And I get the error:

    Main.cpp: In function ‘int main()’:
    Main.cpp:17: error: expected type-specifier before ‘(’ token


    What's going on here?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    bool check = cmp.operator(a,b);
    You don't need "operator" it should be
    Code:
    cmp(a,b)
    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Having made the aforementioned change I get the following error:

    Undefined symbols:
    "KeyValueComparator:perator()(KeyValuePair, KeyValuePair) const", referenced from:
    _main in ccciczWX.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This is a linker error saying it can't find your class implementation. Is you .cpp file being compiled/linked?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class method overloading
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 03-25-2008, 04:09 AM
  2. Looking for criticism on my GameState class
    By Raigne in forum Game Programming
    Replies: 1
    Last Post: 03-21-2008, 09:45 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM