Templates and Unresolved Externals
I'm not sure why I am getting unresolved externals. These are the errors I get:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Array<int,10>::~Array<int,10>(void)" (??1?$Array@H$09@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall Array<int,10>::getSize(void)const " (?getSize@?$Array@H$09@@QBEHXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Array<int,10>::Array<int,10>(void)" (??0?$Array@H$09@@QAE@XZ) referenced in function _main
1>F:\Users\yohosuff\Desktop\C++ HTPE10\Debug\C++ HTPE10.exe : fatal error LNK1120: 3 unresolved externals
Here's the code:
Code:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using std::ostream;
using std::istream;
template<typename T, int numberOfElements>
class Array
{
friend ostream &operator<<( ostream &, const Array<T,numberOfElements> & );
friend istream &operator>>( istream &, Array<T,numberOfElements> & );
public:
Array();
~Array();
int getSize() const;
const Array &operator=( const Array & );
bool operator==( const Array & ) const;
bool operator!=( const Array &right ) const
{
return ! ( *this == right );
}
T &operator[]( int );
T operator[]( int ) const;
private:
int size;
T *ptr;
};
#endif
Code:
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib>
using std::exit;
#include "Array.h" // Array class definition
template<typename T, int numberOfElements>
Array<T,numberOfElements>::Array()
{
size = ( numberOfElements > 0 ? numberOfElements : 10 );
ptr = new T[ size ];
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0;
}
template<typename T, int numberOfElements>
Array<T,numberOfElements>::~Array()
{
delete [] ptr;
}
template<typename T, int numberOfElements>
int Array<T,numberOfElements>::getSize() const
{
return size;
}
template<typename T, int numberOfElements>
const Array<T,numberOfElements> &Array<T,numberOfElements>::operator=( const Array<T,numberOfElements> &right )
{
if ( &right != this )
{
if ( size != right.size )
{
delete [] ptr;
size = right.size;
ptr = new T[ size ];
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ];
}
return *this;
}
template<typename T, int numberOfElements>
bool Array<T, numberOfElements>::operator==( const Array<T,numberOfElements> &right ) const
{
if ( size != right.size )
return false;
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false;
return true;
}
template<typename T, int numberOfElements>
T &Array<T, numberOfElements>::operator[]( int subscript )
{
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 );
}
return ptr[ subscript ];
}
template<typename T, int numberOfElements>
T Array<T,numberOfElements>::operator[]( int subscript ) const
{
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 );
}
return ptr[ subscript ];
}
template<typename T, int numberOfElements>
istream &operator>>( istream &input, Array<T,numberOfElements> &a )
{
for ( int i = 0; i < a.size; i++ )
input >> a.ptr[ i ];
return input;
}
template<typename T, int numberOfElements>
ostream &operator<<( ostream &output, const Array<T,numberOfElements> &a )
{
int i;
for ( i = 0; i < a.size; i++ )
{
output << setw( 12 ) << a.ptr[ i ];
if ( ( i + 1 ) % 4 == 0 )
output << endl;
}
if ( i % 4 != 0 )
output << endl;
return output;
}
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <vector>
using namespace std;
#include "Array.h"
int main()
{
//14.7
Array<int,10> a;
a.getSize();
//********************
cout << "\nProgram Execution Complete\n";
system("PAUSE");
return 0;
}
Any help would be appreciated! :)
Re: Templates and Unresolved Externals
You have to move the code from your template implementatation file into the header or #include it from the header.
Reason: at the point of instantiation of a template the compiler has to be able to see the implementation.
Search the forum for a better explaination. This problem comes up rather frequently.
Kurt
Re: Templates and Unresolved Externals
Re: Templates and Unresolved Externals
Thanks for the tips, I'll give it a shot.
Re: Templates and Unresolved Externals
Awesome! Placing the implementation of the class in the header file worked! And, potatoCode, that link you provided was very helpful. Very helpful indeed. Kudos!