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;
}
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.