简体   繁体   English

我正在学校的一个小班级模板实验室中工作,我的代码无法编译,这给了我一个无法解析的外部符号

[英]I am working on a small class template lab for school and my code will not compile, it gives me an unresolved external symbol

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

    #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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM