Dennington-bear
Dennington-bear

Reputation: 1782

Linked list returning variables from a function

Hi i am doing a project for college and i am stuck at a part. I am using linked list in c++. I have to set up a class called Book which has variables 'title', 'author', 'ISBN' and 'availability'. I set it up like this in my main using a prototype for the function and the function being called elsewhere.

//the prototype
list<Book> bookSetUp();
int main()
{
//the variable in main that will have the list
list<Book> bookList;
//the list being populated in function elsewhere so as to not mess up the main
bookList = bookSetUp();
// more stuff in main 
}
//sets up the book vector list by populating it
//title, author, ISBN, availability
list<Book> bookSetUp()
{
//creates a temp vector to pass it back to the actual vector to be used in the main
list<Book> temp;
//The items that populate the list
Book a("A Tale of Two Cities", "Charles Dickens", 1203456, true);
Book b("Lord of the rings", "J.R.R Tolkein", 123456, true);
Book c("Le Petit Prince", "Antoine de Saint-Exupéry", 123457, true);
Book d("And Then There Were None", "Agatha Christie", 123458, true);
Book e("Dream of the Red Chamber","Cao Xueqin",123459, true);
Book f("The Hobbit","J.R.R Tolkein",123467, true);
//pushes the items into the vector
temp.push_back(a);
temp.push_back(b);
temp.push_back(c);
temp.push_back(d);
temp.push_back(e);
temp.push_back(f);

//returns the list
list<Book>::iterator pos;
pos = temp.begin();
while(pos != temp.end())
{
return pos;
if(pos != temp.end())
{
pos++;
}
}
}

I know that my links between files are grand i just cant get the 'temp' list to return the values. Any help would be greatly appreciated. Thanks

Upvotes: 0

Views: 90

Answers (1)

Xiangyan Sun
Xiangyan Sun

Reputation: 453

In C++ most containers like std::list can be copy-constructed or assigned just like any other primitive types. In your case a direct return is enough.

Upvotes: 1

Related Questions