template question

bool Compare2( S * s1, S * s2)You need to pass pointers as you can see,

so this should fix it;
a.STest(&d1, &d2);

and also;
1
2
3
4
5
static bool STest( S* s1, S* s2 )
{
return Compare2( s1, s2 );
}
};
Last edited on
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
#include<iostream>
using namespace std;

template <class S>
class TestFunctions
{
    public:
    bool Compare2( S * s1, S * s2) { return *s1 == *s2; }//;

    //not necessary, your compiler will generate this from the above,
    //when it sees TestFunctions< double > a; inside main.
    //bool Compare2( double * t1, double * t2) { return *t1 == *t2; };

    //don't make this a static function
    bool STest( S s1, S s2 )
    {
        //pass S* here, as your Compare2 expects
        //Compare2( s1, s2 );
        return Compare2( &s1, &s2 );
    }
};

int main( int argc, char * argv[])
{
    double d1, d2;
    TestFunctions< double > a;

    //let's get some values first...
    cout << "enter 2 doubles:\n";
    cin >> d1 >> d2;

    //...and then add a way to check the result
    if (a.STest( d1, d2 ))
        cout << "they are equal!" << endl;
    else
        cout << "they are not equal!" << endl;

    cin.get();
    cin.get();
    return 0;
}
Last edited on
First of all ,ur functions :
bool Compare2( S * s1, S * s2) { return *s1 == *s2; };
bool Compare2( double * t1, double * t2) { return *t1 == *t2; };

will casue ambiguity when u pass <S> as <double>,as there would be 2 functions to map to ...
hence the compiler is giving an error that:
cannot find match.

Once you fix this you will again get an error as you are passing object by value,where as function will accept addresses.


What the hell happened to this thread o.O?
I don't know... Maybe the OP deleted his account or something...
Can we delete a particular row from a 3*3 dimensional vector?? if yes how, please answer as soon as possible??
Topic archived. No new replies allowed.