Question

I'm working on this project that basically reads information from a file, use that information on an object and then creates a list that contains the objects.

I have a class named Acao which basically contains a few pieces of information, some strings and some floats. Pretty simple;

What I'm trying to do in order to check if my list is being correctly built is to output a float named cMed using the getcMed() member from Acao class.

Ok, first of all:

I'm getting three errors while trying to iterate through my list, being with operators =, != and ++.

All of them being - repectively:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_List_iterator<std::_List_val<std::_List_simple_types<Acao>>>' (or there is no acceptable conversion)

As much as I don't think that really matters in this case, these are my included libs:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <stdlib.h>
#include <sstream>

Now then, my second problem with this piece of code is with the line:

cout << (*it)->getcMed();

Both my list and the iterator it are of Acao type, but my compiler (I'm using VS 2013 for my IDE and compiler) gives me the following error:

error C2039: 'getcMed' : is not a member of 'std::list>'

Here's the chunk of code in question (also note: I'm using namespace std for this):

list<Acao> novaListaAcoes(){
fstream file;
streampos begin;
list<Acao> listaAcoes, it;
Acao A;
string linha, papel, companhia, tipo;
float min, med, max;

file.open("G:\\VS\\ConsoleApplication4\\BDINaux.txt");
file.clear();
file.seekg(0, ios::beg);
listaAcoes.clear();

while (!file.eof()){
    getline(file, linha);
    if (file.eof()){ break; }
    vector<char> vector(linha.begin(), linha.end());

    min = calcMin(vector);
    max = calcMax(vector);
    med = calcMed(vector);
    papel = lePapel(vector);
    companhia = leComapanhia(vector);
    tipo = leTipo(vector);
    vector.clear();
    A.setCompanhia(companhia);
    A.setCotacao(med, min, max);
    A.setNomePapel(papel);
    cout << papel<< endl;
    listaAcoes.push_back(A);
}

cout << "fim loop\n";
for (it = listaAcoes.begin(); it != listaAcoes.end(); ++it){
    cout << (*it)->getcMed();
}
return listaAcoes;
}
Was it helpful?

Solution

Your declaration:

list<Acao> listaAcoes, it;

Doesn't match the type needed for the assignment statement in the for loop initializer:

for (it = listaAcoes.begin(); // <<<

Make a separate declaration for it:

list<Acao>::iterator it;

An iterator is a concept for container classes, but not equivalent to a class instance of those itself!

The idiom I personally prefer, is to declare variables closest to their use, such for a for loop:

for (std::list<Acao>::iterator it = listaAcoes.begin();
     it != listaAcoes.end();
     ++it)
{
    // Access it's underlying Acao instance using -> or * dereference operators
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top