J
John
Hi everyone I need help with the Standard Template Librarys List class. All
I am trying to do is push a Cat object onto it and the copying it back off
of it into another cat object. It seems like it should be simple but for me
its not. I am using dev C++ for this program. Anyhelp will be greatly
appreciated. Thanks
Here is my source code:
------------------------------------
// main.cpp
#include <iostream>
#include "cat.h"
#include <list>
using namespace std;
int main() {
Cat catObj;
Cat catObjfromList;
list<Cat> catL;
catObj.setAge(4);
cout << "The cats age is --> " << catObj.getAge() << endl;
catL.push_front(catObj);
copy(catL.begin(), catL.begin(), &catObjfromList);
cout << catObjfromList.getAge() << endl; // does not get age.
system("pause");
return 0;
}
------------------------------------
// cat.cpp
#include "cat.h"
Cat::Cat()
{
age_ = 0;
}
void Cat::setAge(const int age)
{
age_ = age;
}
int Cat::getAge() const
{
return age_;
}
------------------------------------
// cat.h
#ifndef CATH
#define CATH
class Cat
{
public:
Cat();
void setAge(const int age);
int getAge() const;
private:
int age_;
};
#endif
I am trying to do is push a Cat object onto it and the copying it back off
of it into another cat object. It seems like it should be simple but for me
its not. I am using dev C++ for this program. Anyhelp will be greatly
appreciated. Thanks
Here is my source code:
------------------------------------
// main.cpp
#include <iostream>
#include "cat.h"
#include <list>
using namespace std;
int main() {
Cat catObj;
Cat catObjfromList;
list<Cat> catL;
catObj.setAge(4);
cout << "The cats age is --> " << catObj.getAge() << endl;
catL.push_front(catObj);
copy(catL.begin(), catL.begin(), &catObjfromList);
cout << catObjfromList.getAge() << endl; // does not get age.
system("pause");
return 0;
}
------------------------------------
// cat.cpp
#include "cat.h"
Cat::Cat()
{
age_ = 0;
}
void Cat::setAge(const int age)
{
age_ = age;
}
int Cat::getAge() const
{
return age_;
}
------------------------------------
// cat.h
#ifndef CATH
#define CATH
class Cat
{
public:
Cat();
void setAge(const int age);
int getAge() const;
private:
int age_;
};
#endif