I'm new to cpp and want to learn about list. When I run my program it compiles fine but I don't get the result I'm expecting.
This gives me the output
Legs: 0 Name:
It should be
Legs 4 Name: dog
Can any one see the problem?
As in the comment section, I have tried for over an hour and get get this to work, can't see the problem.
Header file
using namespace std;
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal {
public:
Animal();
Animal(const Animal& orig);
virtual ~Animal();
int _Legs;
void SetName(string name);
string GetName();
private:
string _Name;
};
#endif /* ANIMAL_H */
Animal.cpp
Animal::Animal() {
}
Animal::Animal(const Animal& orig) {
}
Animal::~Animal() {
}
string Animal::GetName(){return _Name;}
void Animal::SetName(string name){_Name = name;};
Main file
void List()
{
std::list<Animal> animal_list;
Animal temp;
temp = Animal();
temp._Legs = 4;
temp.SetName("Dog");
animal_list.push_back(temp);
for(std::list<Animal>::iterator list_iter = animal_list.begin();
list_iter != animal_list.end(); list_iter++)
{
std::cout<< "Legs:" << list_iter->_Legs << " Name: " << list_iter->GetName() << endl;
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
List();
return 0;
}