0

I am getting an unresolved external error when compiling the code, and i cannot figure out what the issue is. I am pretty positive that the template and functions are being used and created according to the assignment, but i just cannot get it to compile. Any help on the matter would be greatly appreciated .H

    #pragma once
    #include<iostream>
    using namespace std;
    template <class P>

    class Pair
    {
    private:
        P firstLetter;
        P secondLetter;

    public:
        Pair(const P&, const P&);

        P getSecondLetter();
        P getFirstLetter();
    };

    template <class P>
    P Pair<P>::getFirstLetter()
    {
        return firstLetter;
    }

    template <class P>
    P Pair<P>::getSecondLetter()
    {
        return secondLetter;
    }

Main: #include

    #include "Pair.h"

    using namespace std;


    int main()
    {
        Pair<char> letters('a', 'd');
        cout << "\nThe first letter is: " << letters.getFirstLetter();
        cout << "\nThe second letter is: " << letters.getSecondLetter();

        cout << endl;
        system("Pause");
        return 0;
    }
  • 1
    You declared `Pair(const P&, const P&);` but didn't define it. – Algo Jul 30 '17 at 17:36
  • 1
    What is the error message? See also [this](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – Raymond Chen Jul 30 '17 at 17:37
  • 1
    You forgot to define the constructor. Also, please drop the `using namespace std;`. – Quentin Jul 30 '17 at 17:37
  • unresolved external symbol "public: __thiscall Pair::Pair(char const &,char const &)" (??0?$Pair@D@@QAE@ABD0@Z) referenced in function _main – Chance Luker Jul 30 '17 at 17:52

1 Answers1

0

Almost there. Just add the constructor (this is probably the one you wanted)

template <class P>
Pair<P>::Pair(const P &p1, const P &p2) : firstLetter(p1), secondLetter(p2) { }

For a finished version something like this:

#pragma once
#include<iostream>
using namespace std;
template <class P>

class Pair
{
private:
    P firstLetter;
    P secondLetter;

public:
    Pair(const P&, const P&);

    P getSecondLetter();
    P getFirstLetter();
};

template <class P>
Pair<P>::Pair(const P &p1, const P &p2) : firstLetter(p1), secondLetter(p2) { }

template <class P>
P Pair<P>::getFirstLetter()
{
    return firstLetter;
}

template <class P>
P Pair<P>::getSecondLetter()
{
    return secondLetter;
}
N00byEdge
  • 1,106
  • 7
  • 18