简体   繁体   中英

No Matching Function Call in a template class

I'm writing a program that reads in a text file and stores data into a class of objects called User. I then store the User objects into a templated class of dynamic arrays called MyList with a push_back function.

currently my MyList class looks like this

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

and the function to pushback looks like this

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

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

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

My User class is as follows

#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

finally, in my main function I declare the user MyList like this

MyList<User> object4;

and my call to push_back is as follows

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

All the data in the User class is valid,

currently im getting an error "no matching function for call to 'MyList::push_back(User) (&) (int, std:string, int, int)"

"note candidates are : void MyList::push_back(type) [with type = User]"

You declare a function

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

and try to push_back this function onto object4 . But object4 is declared to be a

MyList<User> object4;

not a MyList<User (&) (int, std:string, int, int)> of functions returning a User . That's the reason for the error message

no matching function for call to "MyList::push_back(User (&) (int, std:string, int, int))"

If you want to create a User and append it to object4, you would do it like

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

provided you have a constructor with these parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM