【问题标题】:Template class method error "no 'void Pair<T1, T2>::display()' member function Pair declared in class"模板类方法错误“没有'void Pair<T1, T2>::display()' 类中声明的成员函数Pair”
【发布时间】:2016-03-16 03:37:58
【问题描述】:

我不确定我的语法是否不正确。我得到“没有在类中声明的'void Pair T1,T2::display()'成员函数Pair”以及“没有匹配的函数来调用'Pair std::basic_string char,std::char_traits char”这是头文件:

    #ifndef PAIR_H
    #define PAIR_H

    #include <iostream>
    #include <string>
    using namespace std;


    template <typename T1, typename T2 >
       class Pair
    {
      private:
       T1 t1;
       T2 t2;

      public:
      Pair(const T1 & t1,const T2 & t2) : t1(t1), t2(t2) {};
       T1 getFirst() const { return t1; };
       T2 getSecond() const { return t2; };

       //setters
       void setFirst(const T1 & value) { t1 = value; };
       void setSecond(const T2 & value) { t2 = value; };

    };

    template <typename T1, typename T2>
       void Pair<T1,T2> :: display()
    {
       cout << t1 << " - " << t2 << endl;
    }


    #endif // PAIR_H

这是驱动文件。

    #include <iostream>
    #include <string>
    using namespace std;

    #include "pair.h"

    int main()
    {
       string first;
       cout << "Please enter a first name: ";
       cin >> first;

       string last;
       cout << "Please enter a last name: ";
       cin >> last;

       Pair<string, string> fullName;
       fullName.setFirst(first);
       fullName.setSecond(last);

       cout << "The first name is: " << fullName.getFirst() << endl;
       cout << "The last name is: " << fullName.getSecond() << endl;
       cout << "The complete pair is: ";
       fullName.display();
       cout << endl << endl;


       int num1;
       cout << "Please enter a number: ";
       cin >> num1;

       int num2;
       cout << "Please enter another number: ";
       cin >> num2;

       Pair<int, int> numbers;
       numbers.setFirst(num1);
       numbers.setSecond(num2);

       cout << "The first number is: " << numbers.getFirst() << endl;
       cout << "The second number is: " << numbers.getSecond() << endl;
       cout << "The complete pair is: ";
       numbers.display();
       cout << endl << endl;



       string name;
       cout << "Please enter a name: ";
       cin >> name;

       int score;
       cout << "Please enter a score: ";
       cin >> score;

       Pair<string, int> grade;
       grade.setFirst(name);
       grade.setSecond(score);

       cout << "The name is: " << grade.getFirst() << endl;
       cout << "The score is: " << grade.getSecond() << endl;
       cout << "The complete pair is: ";
       grade.display();
       cout << endl << endl;

       return 0;
    }

【问题讨论】:

    标签: class templates methods


    【解决方案1】:

    是的,你的语法不正确。

    1) display()Pair 的成员,所以你必须在类中声明它。

    您可以在public 部分添加此声明行

    void display ();
    

    或者你可以在类里面定义display()(把外面的定义删掉)【我加了const修饰符】

    void display () const { cout << t1 << " - " << t2 << endl; }
    

    1 之二)

    我建议你为类Pair定义运算符public,这个

       friend ostream & operator<< (ostream & os, Pair const & p)
        { return os << '(' << p.t1 << ',' << p.t2 << ')'; } 
    

    所以你可以像这样简单地输出一对

    cout << fullName << endl;
    

    2) 你没有定义默认构造函数(没有参数),所以你应该以这种方式定义你的对

       Pair<string, string> fullName("", "");
       Pair<int, int> numbers(0, 0);
       Pair<string, int> grade("", 0);
    

    或者,避免使用以下setFirst()setSecond()

       Pair<string, string> fullName(first , last);
       Pair<int, int> numbers(num1, num2);
       Pair<string, int> grade(name, score);
    

    3) 我不知道你是否知道,但有一个标准的 std::pair 类 (#include &lt;utility&gt;)。

    4) 对于这样的问题,您应该使用 che c++ 标签

    5) 对不起我的英语不好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多