-1

The question asked is very different from the so called duplicate post as I have specific errors and they are just asking why it needs to be in the .h file... I already have it in the header file and am getting the errors below.

Updated the files and I still get the following errors.

  • 1 Unresolved externals (lab12.exe line 1)
  • unresolved external symbol "public char__thiscall Pair::geetFirst(void)" (?getFirst@?$Pair@D@@QAEDXZ) referenced in function_main (lab12.obj line 1)

Pair.h

#pragma once
template <class T>
class Pair
{

private:
    T theFirst;
    T theSecond;

public:
    /*Pair(const T& dataOne, const T& dataTwo);*/
    Pair(T a, T b) {
        theFirst = a;
        theSecond = b;
    }
    T getFirst();
    T getSecond();

};

Pair.cpp

#include "stdafx.h"
#include "Pair.h"


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

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

Main.cpp

#include "stdafx.h"
#include "Pair.h"
#include <iostream>
using namespace std;

int main()
{
    Pair <char> letters('a', 'd');

    cout << letters.getFirst();


    cout << endl;
    system("Pause");

    return 0;
}
Brandon Turpy
  • 883
  • 1
  • 10
  • 32

1 Answers1

1

You should put all code for template class Pair into the header file.

Also, there is no need to separate method declaration from definition in header.

'Pair T::Pair' is wrong because you do not need T here.