Class 模板类方法错误“;否';无效对<;T1、T2>;::显示();在类“中声明的成员函数对”;
我不确定我的语法是否不正确。我得到了“no”void Pair T1,T2::display()“类中声明的成员函数对”以及“没有用于调用“Pair std::basic_string char,std::char_traits char”的匹配函数”这是头文件:Class 模板类方法错误“;否';无效对<;T1、T2>;::显示();在类“中声明的成员函数对”;,class,templates,methods,Class,Templates,Methods,我不确定我的语法是否不正确。我得到了“no”void Pair T1,T2::display()“类中声明的成员函数对”以及“没有用于调用“Pair std::basic_string char,std::char_traits char”的匹配函数”这是头文件: #ifndef PAIR_H #define PAIR_H #include <iostream> #include <string> using namespace
#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
\ifndef对
#定义配对
#包括
#包括
使用名称空间std;
模板
类对
{
私人:
T1;
T2;
公众:
配对(常数T1和T1,常数T2和T2):T1(T1),T2(T2){};
T1 getFirst()常量{return T1;};
T2 getSecond()常量{return T2;};
//二传手
void setFirst(常数T1&value){T1=value;};
void setSecond(常数T2&value){T2=value;};
};
模板
void Pair::display()
{
cout是的,您的语法不正确
1) display()
是对的成员,因此必须在类中声明它
您可以在public
部分中添加此声明行
void display ();
或者您可以在类内部定义display()
(删除外部的定义)[我添加了const
修饰符]
void display () const { cout << t1 << " - " << t2 << endl; }
因此,您可以简单地输出这样的一对
cout << fullName << endl;
3) 我不知道您是否知道,但是有一个标准的std::pair
类(#include
)
4) 对于这样的问题,应该使用chec++
标记
cout << fullName << endl;
5) 对不起,我英语不好
cout << fullName << endl;
Pair<string, string> fullName("", "");
Pair<int, int> numbers(0, 0);
Pair<string, int> grade("", 0);
Pair<string, string> fullName(first , last);
Pair<int, int> numbers(num1, num2);
Pair<string, int> grade(name, score);