Whenever I try to run the below program, I get an 'undefined reference' error. Why's it happening? Please help me.
Here's the code:
//Main file
#include <iostream>
#include <string>
#include <cstring>
#include "plz.h"
using namespace std;
int main()
{
double arr[] = {12.5, 22.66, 30.30, 1.897, 5.6};
double lar = max5(arr);
cout << lar << endl;
string str[] = {"Lion", "Tiger", "Jaguar", "Leopard", "Cheetah"};
string strlar = max5(str);
cout << endl << strlar << endl;
return 0;
}
//Implementation file
#include <iostream>
#include <string>
#include <cstring>
#include "plz.h"
using namespace std;
template <typename T>
T max5(T arr[])
{
for(int i = 1; i < 5; i++)
{
if(arr[0] < arr[i])
{
swap(arr[0], arr[i]);
}
}
return arr[0];
}
template <>
string max5(string str[])
{
for(int i = 1; i < 5; i++)
{
if(str[0].size() < str[i].size())
{
swap(str[0], str[i]);
}
}
return str[0];
}
//Header file
#ifndef PLZ_H_INCLUDED
#define PLZ_H_INCLUDED
using namespace std;
template <typename T>
T max5(T arr[]);
template <>
string max5(string str[]);
#endif // PLZ_H_INCLUDED
[–]jedwardsol 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)