Hey guys,

Here's my code:

#include <iostream>
using namespace std ;

template<class T>
class Pair
{
public:
	Pair ( ) ;
	Pair (T v1 , T v2 ) ;
	void setFirst(T newV ) ;
	void setSecond(T newV ) ;
	T getFirst() const ;
	T getSecond() const ;
private:
	T first ;
	T second ;
} ;

template<class T>
Pair<T>::Pair(T v1 , T v2 ) : first(v1) , second(v2)
{ }

template<class T>
void Pair<T>::setFirst(T newV)
{ first = newV ; }

template<class T>
void Pair<T>::setSecond(T newV)
{ second = newV ; }

template<class T>
T Pair<T>::getFirst() const
{
	return first ;
}

template<class T>
T Pair<T>::getSecond() const
{
	return second ;
}

template<class T>
T addUp(const Pair<T>& thePair) ;
typedef Pair<double> PairOfDbl ;
typedef Pair<int> PairOfInt ;
int main ( )
{
	PairOfDbl pair1 ;
	PairOfInt pair2 ;

	pair1.setFirst ( 10.3 ) ;
	pair1.setSecond ( 20.72 ) ;

	pair2.setFirst ( 15 ) ;
	pair2.setSecond ( 35 ) ;

	cout << addUp ( pair1 ) << endl ;
	cout << addUp ( pair2 ) << endl ;

	return 0 ;
}

template<class T>
T addUp(const Pair<T>& thePair)
{
	return (thePair.getFirst() + thePair.getSecond());
}

I'm getting about 5 LINK errors. What am I missing? =\

Dani AI

Generated

Short answer: the linker errors come from declaring a default constructor for the template but never providing its definition. pointed this out and confirmed the fix.

Why it happens: when the compiler instantiates Pair<T> for concrete types (here double and int) it needs a definition for every used member. A declared-but-undefined constructor leaves a referenced symbol with no body, so the compiler/linker emits unresolved externals at link time. Templates also require their definitions to be visible to the translation unit that instantiates them (hence the common "header-only" pattern).

Fixes and alternatives:

  • Provide a definition for the default ctor (make sure it's defined with the proper template signature).
  • In C++11+, you can default it inside the class:
    Pair() = default;
  • Construct objects with the parameterized constructor instead of relying on a missing default.
  • If you want to keep implementations in a .cpp, provide explicit instantiations for the concrete types you use, for example:
    template class Pair<double>;
    template class Pair<int>;

    That forces the compiler to emit the needed symbols from that translation unit.

Quick checklist when you see similar LINK errors:

  • Read the linker message to identify the missing mangled symbol (it shows which function/type is unresolved).
  • Ensure every declared member used by instantiated types has a definition visible to the linker.
  • Prefer header-only definitions for templates or use explicit instantiation if separating implementation.
  • If using modern C++, prefer = default or in-class initializers to keep intent clear.

Recommended Answers

All 2 Replies

you are missing this because the constructor at line 8 was never coded.

template<class T>
Pair<T>::Pair( )
{ }
commented: solved my problem. +4

ah! excellent thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.