gerarddp
gerarddp

Reputation: 667

Struct in a list doesn't work

I have this code:

hpp:

 #include <list>
 using namespace std;

 class funcionario
 {
      public:
          struct Dia {
          int d;
          int h;
          int id;
          int tipo;
     };
     funcionario ();
     void eliminar(int dia, int hora);

     private:
         list<Dia> agenda;
};

cpp:

#include "myClass.hpp"

funcionario::funcionario(){
    agenda = list<Dia> ();
}

void funcionario::eliminar(int dia, int hora) {
    list<funcionario::Dia>::iterator it;
    it = agenda.begin();
    while(it != agenda.end() && (*it).d <= dia) {
        if((*it).d == dia && (*it).h == hora) {
            agenda.erase(it);
            return;
        }
        ++it;
    }
}

I get this compiling error:

  Funcionario.cpp: In constructor ‘funcionario::funcionario()’:
  Funcionario.cpp:5: error: cannot convert ‘std::list<funcionario::Dia, std::allocator<funcionario::Dia> >’ to ‘int’ in assignment
  Funcionario.cpp: In member function ‘void funcionario::eliminar(int, int)’:
  Funcionario.cpp:9: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
  Funcionario.cpp:10: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
  Funcionario.cpp:11: error: request for member ‘end’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’

I don't know what I'm doing wrong.

Upvotes: 1

Views: 243

Answers (4)

Ivan
Ivan

Reputation: 1851

This seems to be working on my computer, so may it be a compiler problem? Try with another compiler and tell us if it worked

Upvotes: 0

Casey
Casey

Reputation: 10936

--Edited to reflect your posting of the original code--

H is not declared anywhere.

and is not a valid C++ keyword or token. Use &&.

Use the local header include form of : #include "myClass.hpp"

Upvotes: 0

Scott Jones
Scott Jones

Reputation: 2908

Not sure what you're trying to achieve, but the code just needs to be fleshed out a bit with complete function definitions. I got this to compile:

#include <list>
class myClass
{
   public:
    myClass();
      struct myStruct {
          int myInfo;
      };
    void something();
    void doSomething(myStruct & ms);

   private:
       std::list<myStruct> myList;
};


myClass::myClass(){
    myList = list<myStruct> ();
}

void myClass::something() {
    std::list<myStruct>::iterator it;
    it = myList.begin();
    while(it != myList.end()) {
       doSomething(*it);
       ++it;
    }
}

Incidentally (or maybe directly relevant, not sure) - the copy-initialization of myList in myClass() is unnecessary, as others have stated. The list<> default constructor will do the right thing, and more efficiently.

Upvotes: 1

esskov
esskov

Reputation: 367

The initialization you're looking for is analogous to Initializing map and set class member variables to empty in C++? - but actually you'll get an empty list automatically (i.e. by the std::list default constructor).

Upvotes: 0

Related Questions