为什么我不能使用模板将对象推回? [重复]

问题描述 投票:0回答:1

我对模板和STL矢量有问题。我想push_back一个对象,但出现错误:

没有用于匹配‘std :: vector :: push_back(Perso(&)())的匹配函数”37 | vec.push_back(auxiliar);

这是代码:

#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

class Perso{
private:
    int edad;
    long dni;
public:
    Perso(){this->edad = 0; this->dni = 0;}
    ~Perso(){}
    void setEdad(int edad){
        this->edad=edad;
    }
    void setDNI(int dni){
        this->dni=dni;
    }
    int getEdad(){
        return this->edad;
    }
    int getDNI(){
        return this->dni;
    }
};

template <typename T>
void cargarPersonas(vector<T>&vec){
    ifstream nomArch;
    T auxiliar();
    nomArch.open("ArchivoBin.dat", ios::in | ios::binary);
    if(nomArch.is_open()){
        int i = 0;
        do{
            nomArch.seekg(i*sizeof(T),ios::beg);
            nomArch.read((char*)&auxiliar,sizeof(T));
            vec.push_back(auxiliar);
            ++i;
        }while (!(nomArch.eof()));
        nomArch.close();
    }
}
template <typename G>
void guardarPersonas(vector<G>vec){
    ofstream nomArch;
    nomArch.open("ArchivoBin.dat", ios::app | ios::binary);
    if(nomArch.is_open()){
        for(int i =0; i<vec.size();++i)
        {
            nomArch.write((char*)&vec.at(i),sizeof(G));
        }
        nomArch.close();
    }
}
int main() {
    vector<Perso>personas;
    cargarPersonas<Perso>(personas);
}
c++ stl
1个回答
0
投票

T auxiliar();是称为auxiliar的函数,该函数返回T。删除括号:

© www.soinside.com 2019 - 2024. All rights reserved.