trouble with template class
Nov 16, 2011 at 4:01am UTC
Im building a program that uses a template class called my data and utilizes overloaded operators for my c++ class
but for some reason when i implement the default constructor i get errors
Error 2 error LNK2001: unresolved external symbol "class MyData<int> __cdecl EqualsData(void)" (?EqualsData@@YA?AV?$MyData@H@@XZ) E:\C++\Brad Final Project\Brad Final Project\TestMyData.obj
Error 3 error LNK1120: 1 unresolved externals E:\C++\Brad Final Project\Debug\Brad Final Project.exe
can you help me find whats wrong
Here's the code:
MyData.h
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
#include<iostream>
using namespace std;
template <class T> class MyData
{
T value;
public :
//Constructors
MyData(T t)
{
value = t;
}
MyData()
{
value = 0
}
//SetData
void setData(T t)
{
value = t
}
//GetData
T getData()
{
return value;
}
//Overload +
MyData operator +(MyData op2)
{
return value+op2.value;
}
//Overload -
MyData operator -(MyData op2)
{
return value-op2.value;
}
//Overload =
MyData operator =(MyData op2)
{
value = op2.value;
}
//Overload >
bool operator >(MyData op2)
{
if (value>op2.value)
return true ;
else
return false ;
}
//Overload ++
MyData operator ++()
{
return value++;
}
//Overload cin
template <class T> friend istream &operator >>(istream &stream, MyData<T> &obj)
{
cout << "Enter data: " ;
stream >> obj.value;
return stream;
}
//Overload cout
template <class T> friend ostream &operator <<(ostream &stream, MyData<T> &obj)
{
stream << obj.value;
return stream;
}
};
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
#include<iostream>
using namespace std;
#include "MyData.h"
int main()
{
int intValue = 12;
MyData<int > intData(intValue);
intData++;
cout <<"integer incremented: " << intData<<endl;
MyData<int > secondInt(3);
cout<<"Sum of two integers: " << intData+secondInt<<endl;
cout<<"Difference of two integers: " << intData-secondInt<<endl;
MyData<int > EqualsData();
cout<<"Test default constructor: " << EqualsData();
//EqualsData = secondInt;
//cout<<"Test == function: "
if (intData > secondInt)
cout<<intData<<" is larger than " <<secondInt<<endl;
}
Nov 16, 2011 at 6:25am UTC
hello,
I've changed some errros which are commented out.
plese read them cerfull!
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
template <class T> class MyData
{
T value;
public :
//Constructors
MyData(T t)
{
value = t;
}
MyData()
{
value = 0; //MISSING SEMICOLON ADED!!
}
//SetData
void setData(T t)
{
value = t
}
//GetData
T getData()
{
return value;
}
//Overload +
MyData operator +(MyData op2)
{
return value+op2.value;
}
//Overload -
MyData operator -(MyData op2)
{
return value-op2.value;
}
//Overload =
MyData operator =(MyData op2)
{
return value = op2.value; //MISSIING RETURN ADDED!!!
}
//Overload >
bool operator >(MyData op2)
{
if (value>op2.value)
return true ;
else
return false ;
}
//Overload ++
MyData operator ++(int ) //MISSING int ADDED FOR POSTFIX OPERTOR!
{
return value++;
}
//Overload cin
template <class T> friend istream &operator >>(istream &stream, MyData<T> &obj)
{
cout << "Enter data: " ;
stream >> obj.value;
return stream;
}
//Overload cout
template <class T> friend ostream &operator <<(ostream &stream, MyData<T> &obj)
{
stream << obj.value;
return stream;
}
};
int main( int argc, char * argv[] )
{
int intValue = 12;
MyData<int > intData(intValue);
intData++;
cout <<"integer incremented: " << intData<<endl;
MyData<int > secondInt(3);
cout<<"Sum of two integers: " << intData+secondInt<<endl;
cout<<"Difference of two integers: " << intData-secondInt<<endl;
MyData<int > EqualsData; //CHANGED FROM EqualsData() which is constructor!
//CHANGED FROM "EqualsData(); which is constructor!
cout<<"Test default constructor: " << EqualsData.getData() << endl;
EqualsData = secondInt;
cout<<"Test == function: " ; //MISSIG ';' PLACED!
if (intData > secondInt)
cout<<intData<<" is larger than " <<secondInt<<endl;
cout << endl << "finish..." ;
cin.ignore();
return 0;
}
there is more thisgs which may be changed but are not nesecary errors!
cheers!
Last edited on Nov 16, 2011 at 6:50am UTC
Nov 18, 2011 at 1:16am UTC
thanks for your help it's running smoothly now
Topic archived. No new replies allowed.