C++ 模板类中没有匹配的函数调用
我正在编写一个程序,它读入一个文本文件并将数据存储到一个名为User的对象类中。然后,我使用push_-back函数将用户对象存储到一个名为MyList的动态数组模板类中 目前我的MyList类如下所示C++ 模板类中没有匹配的函数调用,c++,C++,我正在编写一个程序,它读入一个文本文件并将数据存储到一个名为User的对象类中。然后,我使用push_-back函数将用户对象存储到一个名为MyList的动态数组模板类中 目前我的MyList类如下所示 #ifndef MYLIST_H #define MYLIST_H #include <string> #include <vector> using namespace std; template<class type> class MyList { p
#ifndef MYLIST_H
#define MYLIST_H
#include <string>
#include <vector>
using namespace std;
template<class type>
class MyList
{
public:
MyList();
~MyList();
int size() const;
int at(int) const;
void remove(int);
void push_back(type);
private:
type* List;
int _size;
int _capacity;
const static int CAPACITY = 80;
};
MyList<User> object4;
用户类中的所有数据都有效
当前,我收到一个错误“调用'MyList::push_back(User)(&)(int,std:string,int,int)时没有匹配的函数”
“注意候选项是:void MyList::push_back(type)[with type=User]”您声明了一个函数
User newuser(int id, string name, int age, int zip);
并尝试将此函数向后推到对象4上。但是object4
被声明为
MyList<User> object4;
如果您有一个具有这些参数的构造函数。我有一个用户对象,它定义为一个包含(int,name,int,int)的对象,那么我不应该使用这种格式将用户对象存储到MyList中吗?非常感谢!我真不敢相信我在这样一个小错误上花了一个小时。
User newuser(int id, string name, int age, int zip);
object4.push_back(newuser);
User newuser(int id, string name, int age, int zip);
MyList<User> object4;
User newuser(id, name, age, zip);
object4.push_back(newuser);