0

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

4
  • What are you trying to do in the while(pos != temp.end()) loop? It currently immediately returns temp.begin() Commented Nov 16, 2013 at 20:16
  • sorry about the mass of code!I feel its better than trying to communicate everything Commented Nov 16, 2013 at 20:17
  • hmm. and what exactly are you trying to do? i have a feeling that you want something like what 'yield' does in some languages Commented Nov 16, 2013 at 20:19
  • I set the pos equal to the beginning of the list pos = temp.begin(); so that it is incremented to the next item on the list unless it has reached the end pos != temp.end() Im am trying to get the function bookSetUp() to fill the list in the main list<Book> bookList. I should have stated this in my question the main is in main.cpp and bookSetUp() is in another .cpp called functions. Commented Nov 16, 2013 at 20:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.