May 11, 2013 at 3:58pm UTC
I can't get the bubblesort template function to work.
Header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#ifndef _MYFLOAT_H
#define _MYFLOAT_H
#include <iostream>
using namespace std;
template <class X> void Bubblesort(X &a, X &b);
class myFloat
{
float x;
float y;
public :
myFloat ();
myFloat (float , float );
void setX(float );
void setY(float );
float getX();
float getY();
ostream friend & operator <<(ostream &, myFloat);
bool operator >(myFloat);
};
#endif
CPP
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
#include "myfloat.h"
myFloat::myFloat()
{
x = 0;
y = 0;
}
myFloat::myFloat(float a,float b)
{
x = a;
y = b;
}
void myFloat::setX(float a)
{
x=a;
}
void myFloat::setY(float b)
{
y=b;
}
float myFloat::getX()
{
return x;
}
float myFloat::getY()
{
return y;
}
ostream & operator <<(ostream &out, myFloat i)
{
out << " x = " << i.getX() << " y = " << i.getY() << endl;
return out;
}
bool myFloat::operator >(myFloat)
{
if (x > y)
return true ;
else
return false ;
}
template <class X>
void Bubblesort(X &a, X &b)
{
for (a=1; a<size; a++)
for (b=size-1; b>=a; b--){
if (nums[b-1] > nums[b]){
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
}
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include "myfloat.h"
#include "myname.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
myFloat x;
myFloat y;
x.setX(45);
x.setY(65);
y.setX(67);
y.setY(45);
Bubblesort(x,y);
system("pause" );
return 0;
getting c2668 error and Bubblesort in main says "More than 1 instance of overloaded function matches argument list.
Last edited on May 11, 2013 at 4:15pm UTC
May 11, 2013 at 5:53pm UTC
I believe it has something to do with the fact that you had declared a template for a Bubblesort function as a header. I don't think this works that way with template functions. I don't know why it doesn't work, but I recall trying something like this once and I got the same error. So what you can try is to remove the header and instead declare the function either as a member of myfloat or just have it has above main and try again.
Post here if you are still experiencing problems maybe someone may know something more about it