没有对构造函数的匹配函数调用
no matching function call to a constructor
#include <iostream>
using namespace std;
template<class T>
class people{
public:
virtual void insert(T item)=0;
virtual T show()=0;
};
class name
{
private:
string fname;
string lname;
public:
name(string first, string last);
// bool operator== (name & p1, name &p2)
};
name::name(string first, string last){
fname = first;
lname = last;
}
template <class T>
class person : public people<T>
{
private:
T a[1];
int size;
public:
person();
virtual void insert(T info);
virtual T show();
};
template<class T>
person<T>::person(){
size = 0;
}
template<class T>
void person<T>::insert(T info){
a[0] =info;
}
template <class T>
T person<T>::show(){
return a[0];
}
int main(){
string first("Julia"), last("Robert");
name temp(first,last);
people<name>* aPerson = new person<name>();
aPerson-> insert(temp);
cout << aPerson->show() << endl;
return 0;
}
这些是我遇到的错误:
test.cpp: In function 'int main()':
test.cpp:53: error: no match for 'operator<<' in 'std::cout << people<T>::show [with T = name]()'
test.cpp: In constructor 'person<T>::person() [with T = name]':
test.cpp:51: instantiated from here
test.cpp:37: error: no matching function for call to 'name::name()'
test.cpp:21: note: candidates are: name::name(std::string, std::string)
test.cpp:12: note: name::name(const name&)
name
没有默认构造函数,因此无法使用new person<name>
初始化它。解决此问题的最简单方法是为 name
添加一个默认构造函数:
name() { }; //add this to name under public: of course
问题 2:您没有在名义上重载 <<
运算符:下面是一个基本示例:
friend std::ostream& operator<<(std::ostream& stream, const name &nm); //Inside name class
std::ostream& operator<<(std::ostream& stream, const name &nm){ //OUTSIDE of name
stream << nm.fname << " " << nm.lname << std::endl;
return stream;
}
相关文章:
- 在c++中使用向量时,如何调用构造函数和析构函数
- C++:考虑但不调用构造函数的特殊性
- 对象实例化调用构造函数的次数太多
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C ++:通过大括号调用构造函数?
- 不能调用构造函数
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 当我尝试从成员函数调用构造函数时,为什么IDE会发出警告
- 从复制构造函数调用构造函数
- 从同一类的另一个构造函数调用构造函数
- 从另一个成员函数/构造函数调用C++构造函数是否执行初始值设定项列表
- 通过引用从函数调用C++构造函数
- 从不同类的构造函数调用构造函数
- 错误:没有匹配的函数调用'构造函数'注意:候选函数是:
- 从构造函数调用构造函数
- 用数组初始化从另一个构造函数调用c++构造函数
- 从成员函数调用构造函数
- 在c++中从另一个构造函数调用构造函数时出错
- 可以从静态函数调用构造函数