2013-02-11 57 views
0

我正在写一个程序读入文本文件并将数据存储到一个名为User的对象类中。然后,我将User对象存储到带有push_back函数的名为MyList的动态数组的模板化类中。没有匹配函数调用模板类

目前我MYLIST类看起来像这样

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

和功能推回看起来像这样

template<class type> 
void MyList<type>::push_back(type newfriend) 
{ 

    if(_size >= _capacity){ 
     _capacity++; 

    List[_size] = newfriend; 
     size++; 
    } 
} 

我的User类是如下

#ifndef USER_H 
#define USER_H 
#include "mylist.h" 
#include <string> 
#include <vector> 

using namespace std; 

class User 
{ 
public: 
    User(); 
    User(int id, string name, int year, int zip); 
    ~User(); 

private: 
    int id; 
    string name; 
    int age; 
    int zip; 
    MyList <int> friends; 
}; 

#endif 

终于,在我主要功能我这样宣布用户MyList

MyList<User> object4; 

我呼吁的push_back是在User类是有效的如下

User newuser(int id, string name, int age, int zip); 
    object4.push_back(newuser); 

所有数据,

目前即时得到一个错误“为号召“MYLIST没有匹配功能: :的push_back(用户)(&)(INT,STD:字符串,整数,INT)”

“音符候选是:无效MYLIST ::的push_back(类型)[类型=使用者]”

回答

1

你声明函数

User newuser(int id, string name, int age, int zip); 

,并尝试push_back这个功能到object4。但object4被宣布为一个

MyList<User> object4; 

不是返回User功能MyList<User (&) (int, std:string, int, int)>。这对错误信息的原因

呼叫没有匹配功能 “MYLIST ::的push_back(用户(&)(INT,STD:字符串,INT,INT))”

如果您要创建一个User并追加它object4,你会做这样

User newuser(id, name, age, zip); 
object4.push_back(newuser); 

只要你有这些参数的构造函数。

+0

嗯,我有一个用户对象被定义为包含一个(int,名称,int,int)的对象,所以我不应该能够使用这种格式来存储一个用户对象到MyList? – 2013-02-11 02:03:20

+0

@DanielHwang请看最新的答案。 – 2013-02-11 02:10:30

+0

非常感谢!我不敢相信我在这样一个小小的错误上花了一个小时。 – 2013-02-11 02:12:57