How to make an array of objects?

2

How to do an array of objects? I am trying but I am not succeeding, it returns me an error:

  

line 26 [Error] no match for 'operator []' (operand types are 'Time'   and 'int')

Code:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "Time.h"
using namespace std;


int apaga_espaco(ifstream &tab, Time t)
{
    string nome="gremio";
    int saldo=0;
    int vit=1,i;

    string s;
    char N;
    while (tab.good())
    {
        i++;
        getline(tab, s);
        s.erase(0,29);
        N=s.find(':');
        s.erase(0,N+6);

        return 0;
        t[i].set_name(nome);
    }
}   


int main()
{
    Time *t;
    t=new Time[20]; 
    ifstream tabe;
    char N;
    string s;
    tabe.open("Tabela.txt", ios::in);

    if (!tabe.is_open())
    {
        cout << "Arquivo nao encontrado, erro fatal!";
        exit(1);
    }

    apaga_espaco(tabe,*t);
}
    
asked by anonymous 29.09.2015 / 03:49

1 answer

2

Your code does not make any sense. And he's a bit disorganized, which makes it difficult to understand what he's doing. There's a lot of things loose, things do nothing useful. It is difficult to try to do something minimally coherent. Even hitting on what he is still asking would be fraught with problems. I've fixed a few things, but the code is still meaningless.

The biggest change was to use Vector instead of array , after all you are using C ++ and the use of array should be avoided. Do not use C ++ techniques in C ++:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <memory>
using namespace std;


void apaga_espaco(ifstream& tab, vector<time_t> t) {
    string nome = "gremio";
    int saldo = 0;
    int vit = 1, i = 0;
    string s;
    char N;
    while (tab.good()) {
        i++;
        getline(tab, s);
        s.erase(0, 29);
        N = s.find(':');
        s.erase(0, N + 6);
    }
}

int main() {
    vector<time_t> t(20); 
    ifstream tabe;
    char N;
    string s;
    tabe.open("Tabela.txt", ios::in);

    if (!tabe.is_open()) {
        cout << "Arquivo nao encontrado, erro fatal!";
        exit(1);
    }
    apaga_espaco(tabe, t);
}

See almost running on ideone (by site limitation).

    
29.09.2015 / 04:31