簡體   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

編譯代碼時出現無法解決的外部錯誤,我無法弄清楚問題是什么。 我非常肯定模板和函數是根據分配使用和創建的,但我只是無法將其編譯。 對這個問題的任何幫助將不勝感激。

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

主要:#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;
    }

快好了。 只需添加構造函數(這可能是您想要的那個)

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

對於完成的版本,如下所示:

#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