How to make an array of objects?

Asked

Viewed 2,561 times

2

How to make a array of objects? I am trying but am not succeeding, 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);
}
  • sorry I made a mistake at the time of dialing, already fixed.

  • No problem. Now you can vote for anything you want on the site. Remember that voting is not the same thing as accepting it. Voting is not exclusive. You can give anything you want, even in other questions.

1 answer

2


His code doesn’t make any sense. And he’s a little disorganized, which makes it hard to understand what he’s doing. There is a lot of loose things, things do nothing useful. It is difficult to try to do something minimally coherent. Even if I hit what you’re asking, it would still be full of problems. I fixed some things, but the code is still meaningless.

The biggest change was using Vector in place of array, after all it is using C++ and the use of array should be avoided. Do not use C in C techniques++:

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

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.