0

i have a problem with templates and STL Vector. I want to push_back an object but i have the error:

no matching function for call to ‘std::vector<Perso>::push_back(Perso (&)())’
  37 |             vec.push_back(auxiliar);

This is the code:

#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);
}
O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • John is right. See https://en.wikipedia.org/wiki/Most_vexing_parse – DeducibleSteak Apr 12 '20 at 23:39
  • @DeducibleSteak This parse is merely annoying. Most vexing has a few extra requirements. – user4581301 Apr 12 '20 at 23:40
  • 1
    Note: The `do`/`while` loop is odd. It caught my attention because of [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons), but looking it over more to make sure that link fit, and it does, there doesn't seem to be any need for the `seekg`. After reading `sizeof(T)` bytes from the stream, the read indicator will be moved ahead by `sizeof(T)` bytes placing the read indicator exactly where the `seekg` will. – user4581301 Apr 12 '20 at 23:47

1 Answers1

2

T auxiliar(); is a function called auxiliar that returns a T. Remove the parentheses:

T auxiliar;
JohnFilleau
  • 4,045
  • 1
  • 15
  • 22