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

私のユーザークラスは次のとおりです

#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 newuser(int id, string name, int age, int zip);
   object4.push_back(newuser);

User クラスのすべてのデータは有効です。

現在、「'MyList::push_back(User) (&) (int, std:string, int, int) の呼び出しに一致する関数がありません」というエラーが表示されます

「メモ候補は : void MyList::push_back(type) [with type = User]」

4

1 に答える 1

1

関数を宣言します

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

push_backにこの関数を試してみてくださいobject4。しかしobject4、次のように宣言されています

MyList<User> object4;

MyList<User (&) (int, std:string, int, int)>を返す関数の ではありませんUser。それがエラーメッセージの理由です

「MyList::push_back(User (&) (int, std:string, int, int))」の呼び出しに一致する関数はありません

を作成しUserて object4 に追加する場合は、次のようにします。

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

これらのパラメーターを持つコンストラクターがある場合。

于 2013-02-11T01:58:02.807 に答える