error LNK2019: unresolved external symbol

Hey guys,
I'm trying to work through an exercise dealing with class templates but i'm receiving this error:

error LNK2019: unresolved external symbol "public: class std::valarray<int> & __thiscall Pair<class std::valarray<int>,class std::valarray<int> >::second(void)" (?second@?$Pair@V?$valarray@H@std@@V12@@@QAEAAV?$valarray@H@std@@XZ) referenced in function "public: void __thiscall Wine::GetBottles(void)" (?GetBottles@Wine@@QAEXXZ) C:\Users\CP\Documents\Visual Studio 2010\Projects\CH14 Q1\Wine.obj CH14 Q1

It seems to me it has a problem with one of my calls to a function in my template class called Pair. I am pretty sure I have included all the appropriate header files. I included valarray and Pair.h. I am pretty stumped as to why its not linking. Here is the code:

Pair.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PAIR_H
#define PAIR_H
template <class T1, class T2>
class Pair
{
private:
    T1 a;
    T2 b;
public:
    T1 & first();
    T2 & second();
    T1 first() const { return a; }
    T2 second() const { return b; }
    Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) { }
    Pair() {}
};
#endif 


Pair.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Pair.h"

template<class T1, class T2>
T1 & Pair<T1,T2>::first()
{
    return a;
}
 
template<class T1, class T2>
T2 & Pair<T1,T2>::second()
{
    return b;
}


Here is the function in question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Wine::GetBottles()
{
    if (Years < 1)
        std::cout << "\nNo years to enter data for or invalid number!";
    else
    {
        std::cout << "Enter " << Name << " data for " << Years << " year(s): " << std::endl;
        for (int i = 0; i < Years; i++)
        {
            std::cout << "Enter year: ";
            (std::cin >> YearBot.first()[i]).get();
            std::cout << "Enter bottles for that year: ";
            (std::cin >> YearBot.second()[i]).get();
        }
    }
}


If you would like more information here is the Wine.h file:

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
#ifndef WINE_H
#define WINE_H
#include <iostream>
#include <string>
#include <valarray>
#include "Pair.h"

typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine
{
private:
	std::string Name;
	int Years;
	PairArray YearBot;
public:
	Wine();
	Wine(const char * l, int y, const int yr[], const int bot[]);
	Wine(const char * l, int y);
	void GetBottles();	//Asks for bottle and year input
	void Show() const;	//Displays current object
	const std::string & Label() {return Name;}		//Displays the label for the object	
        int Sum(); //Returns total number of bottles for the object
};
#endif 

Last edited on
Template class/function definitions must go in the header file.
Appreciated, thank you.
Topic archived. No new replies allowed.