error2019

error
//////////////////
Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Complex<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Complex@H@@@Z) referenced in function "void __cdecl PrintMax<class Complex<int> >(class Complex<int> *,int)" (??$PrintMax@V?$Complex@H@@@@YAXPAV?$Complex@H@@H@Z) E:\اولى حاسبات الترم التانى\labs\lab7\prelab\PreLab08 - Templates\TemplatesPreLab\Requirement - Complex\main.obj Requirement - Complex

/////////////////////
file.h
//////////////////////////////
#ifndef COMPLEX_H
#define COMPLEX_H
template<typename T>
class Complex
{
private:
float real, imag;
public:

Complex(float a=0, float b=0)
{
real = a; imag = b;
}
Complex(const Complex <T>& rhs)
{

real = rhs.real;
imag = rhs.imag;
cout << "\n\n *** *** *** COPY Constructor is Called *** *** ***\n";
cout.flush();

}
~Complex()
{
cout << "destructor" << endl;
}

void setreal(float x);
void setimag(float x);
float getreal();
float getimag();
friend std::ostream& operator<<(std::ostream& out,const Complex & C);
bool operator >(Complex & C);
void operator =(Complex & C);

};

template <typename T>
void Complex<T>::setreal(float x)
{
real = x;
}
template <typename T>
void Complex<T>::setimag(float x)
{
imag = x;
}

template <typename T>
float Complex<T>::getreal()
{
return real;
}

template <typename T>
float Complex<T>::getimag()
{
return imag;
}

template <typename T>
bool Complex <T> ::operator >(Complex <T> & C)
{
if ((real>C.real) && (imag>C.imag)) return true;
else return false;
}


template <typename T>
void Complex <T>::operator =(Complex <T> & C)
{
if (&C != this)
{
real = C.real;
imag = C.imag;
}
}






#endif
////////////////////
file.cpp
/////////////////////////
#include <math.h>
#include <iostream>
//#include<complex>
using namespace std;
#include "Complex.h"

template <typename T>
ostream &operator <<(ostream &out, const Complex <T> & C)
{
out << "max complex number (" << C.getreal() << "+" << C.getimag()<<"j" << ")" <<endl;
return out;
};


// Create PrintMax method that takes array of any type and print the max value in it.
template<typename T>
void PrintMax(T*arr,int count)
{

T max;
max=arr[0];
for (int i =1; i <count ;i++)
{
if (arr[i]>max) max=arr[i];
}
cout<< max<<endl;
};

int main()
{
// Input: Double Array
// 1.5, 2.7, 9.1
// Expected Output: 9.1
// Create the array and call PrintMax function
double arr1[3] = { 1.5, 2.7, 9.1 };
PrintMax(arr1, 3);
//----------------------------------------------//

// Input: Integer Array
// 11, 6, 12
// Expected Output: 12
// Create the array and call PrintMax function
int arr2[3] = { 11,6,12 };
PrintMax(arr2, 3);
//----------------------------------------------//

// Input: Complex Array
// 1+3i, 2+4i, 3+3i
// Expected Output: 2+4i
// Create the array and call PrintMax function

Complex<int> arr3[3] = { Complex <int> (1,3), Complex<int >(2,4), Complex<int>(3,3) };
PrintMax (arr3, 3);
system("pause");
return 0;
}





Last edited on
Your << operator is templated and therefore needs to go in your header file -- not in the cpp file.

PrintMax should probably go in the header as well.
I want both function (<< operator and Print max )to be a global functions
Topic archived. No new replies allowed.