question about inheritance and virtual methods

So my question has to do with the " virtual int compare " method. im trying to make superclass that would allow subclasses to be sorta based on their values.
the error im getting is "no matching function call to comparable::compare( comparable& other )". when i remove the parameter of " comparable& other " , the problem seems to go away. to get to the point, am i just implementing this wrong? also any explanation of whats going on here would be nice. thank you

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
#include <vector>


using namespace std;

class comparable
{
public:
    comparable();
    virtual int compare( comparable& other ) = 0;
    int getValue();
    virtual ~comparable();


private:
    comparable( const comparable& other );

protected:
    int value;
};

class highScore : public comparable
{
public:
    virtual int compare( comparable& other );
    highScore();
    highScore( int score , string name );
    virtual ~highScore();

private:
    int* Score;
    string* Name;
};

comparable::comparable()
{

}
comparable::~comparable()
{

}

int comparable::getValue()
{
    return value;
}
highScore::highScore()
{
    Score = new int( 0 );
    Name = new string( "blank" );
    value = *Score;
}
highScore::highScore( int score, string name )
{
    Score = new int( score );
    Name = new string( name );
    value = *Score;
}
highScore::~highScore()
{
    delete Score;
    delete Name;
}
int highScore::compare( comparable& other )
{
    int other_val = other.getValue();
    if ( *Score == other_val )
        return 0;
    if ( *Score > other_val )
        return 1;
    else
        return -1;

    return 0;
}

int main()
{
    vector<comparable*> comparables;
    comparables.push_back( new highScore( 1234, "test" ) );
    comparables.push_back( new highScore( 1235, "test" ) );
    vector<comparable*>::iterator itr1 = comparables.begin();
    vector<comparable*>::iterator itr2 = comparables.begin();
    *itr2++;

     cout << (*itr1)->compare( (*itr2) );


    return 0;
}

EDIT: some of the things ive done here might seem strange, thats because ive been trying everything i can think of to make this problem go away
Last edited on
the error im getting is "no matching function call to comparable::compare( comparable& other )".

Your vector 'comparables' contains comparable* pointers, so you're trying to pass a pointer to a function that expects a comparable& reference.

Calling compare like this will work

cout << (*itr1)->compare( *(*itr2) ); // or even just **itr2

But it might make sense to alter the compare method to take a pointer if you plan to work mainly with pointers.

Andy

PS You do say that you've done some strange thing; so I assume you know that neither int nor std::string members are usually allocated using new, and that your code is just an exercise in pointer management.
Last edited on
Great :D it works thank you andy
Topic archived. No new replies allowed.