STL List problem

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
 
V

Victor Bazarov

John said:
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);

What do you expect this to do? Copy from begin() to (but not including)
begin() means copy _nothing_. The simplest way to get the very first
element of the container is to call 'front()':

catObjfromList = catL.front();

or dereference the iterator returned by 'begin()':

catObjfromList = *catL.begin();
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

V
 
J

John

Thanks, I had no idea that front() could return anything. Thanks! I really
do appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top