Bubblesort Template

I keep getting this error no matter how much I change sort. This are 3 separate files header, CPP, and main.
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
#ifndef _MYFLOAT_H
#define _MYFLOAT_H
#include  <iostream>
using namespace std;
template <class X> void BS(X nums, int size);
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

#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 BS(X nums, int size)
{
    for(int x=1; x<size; x++)
    for(int b= 0; b< size - x; b++){
    if(nums[b] > nums[b + 1]){
    swap(nums[b], swap[b+1]);
}
}
}
#include "myfloat.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{ 
    myFloat x;
    myFloat y;

    x.setX(5);
    y.setX(9);
	
    cout << x << y;
    BS(x,y);
    cout << x << y;
 system("pause");
	return 0;
}

error LNK2019: unresolved external symbol "void __cdecl BS<class myFloat>(class myFloat,class myFloat)" (??$BS@VmyFloat@@@@YAXVmyFloat@@0@Z) referenced in function _main
Last edited on
Where is myname.h?
you don't need to include myfloat.h because you're working on the same .cpp file.
Post your Errors
I think you must inline the functions in a templated class.
The error you posted is generated becouse, in line 5, you declared only the prototype of "template <class X> void BS" (the function will be defined in line 62, instead).

I suggest to remove line 5 and to move the declaration of template function from line 62 to line 5...

However, after doing so, your code probably will be again open to problems... I am not sure about it, but I don't think that code will work properly

X nums, infact, will be passed as value... so I don't think you can really treat "nums" as an array.

Probably you would prefer to pass X * nums instead (passing nums as a pointer will allow you to treat nums as an array).

However, also after doing this, I see a possible code vulnerability... that
 
 if(nums[b] > nums[b + 1])

can generate a segmentation fault problem (or index out of range, of similar)... BEFORE EVEN TRY TO COMPARE an element with the following one, you must be sure that actually exists another element, else even the "if" instruction alone will return a memory error message (like the ones I mentioned).
Topic archived. No new replies allowed.