Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compiler differences

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
This program works using Borland 4 but does not work when I compile it with Microsoft Visual C++.


[tt]
#include <iostream.h>

template<class T> T templat(T a,T b)
{
return((a > b) ? a: b);
}

float templat(float a,float b);
int templat(int a,int b);

void main(void)
{
float a = 2.335, b = 4.35;
cout << &quot;FLOAT = &quot; << a << &quot; and &quot; << b <<
templat(a,b) << endl;
int c = 2, d = 4;
cout << &quot;INT = &quot; << c << &quot; and &quot; << d <<
templat(c,d) << endl;
}[/tt]



Here is the Visual C++ error:
Linking...
templat.obj : error LNK2001: unresolved external symbol &quot;int __cdecl templat(int,int)&quot; (?templat@@YAHHH@Z)
templat.obj : error LNK2001: unresolved external symbol &quot;float __cdecl templat(float,float)&quot; (?templat@@YAMMM@Z)
Debug/templat.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

templat.exe - 3 error(s), 0 warning(s)
.


Any ideas why this is happening??
 
Try so:
template <class T> class templat
{
public:
T compare(T a,T b);
};

template<class T> T templat<T>::compare(T a,T b)
{
return((a > b) ? a: b);
}

templat<float> ftempl;
templat<int> itempl;

int main()
{
float a = 2.335, b = 4.35;
cout << &quot;FLOAT = &quot; << a << &quot; and &quot; << b <<
ftempl.compare(a,b) << endl;
int c = 2, d = 4;
cout << &quot;INT = &quot; << c << &quot; and &quot; << d <<
itempl.compare(c,d) << endl;
}

It works allways.

If You needs only functions, make so:
float compare(float a,float b)
{
return((a > b) ? a: b);
}

int compare(int a,int b)
{
return((a > b) ? a: b);
}

it works without any classes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top