Thread: Problem calling class function.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Problem calling class function.

    Okay, so it's me again

    I have declared a vector and function within a class

    Code:
    vector<Book> Books;
    void add_books(vector<Book>&);
    I have defined the function within a .cpp file

    Code:
    void Library::add_books(vector<Book>& b)
    {
    
    	//do something
    	
    }
    However, when I call the function, I think I am passing the argument incorrectly. I have tried as such:

    Code:
    Library::add_books(Library::Books);
    But I get the following error:

    error C2597: illegal reference to non-static member 'Library::Books

    Any wise words of wisdom?

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is Library? Is it a class?
    Also, don't remove the names of the parameters in the prototypes: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    What is Library? Is it a class?
    Also, don't remove the names of the parameters in the prototypes: SourceForge.net: Do not remove parameter names - cpwiki
    Hello,

    Yeah, Library is the class.

    Cheers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you have to create an instance of the Library first and call its member function add_books.
    Also, I don't see the need for Books to be inside the Library class?
    What are you trying to accomplish?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Then you have to create an instance of the Library first and call its member function add_books.
    Also, I don't see the need for Books to be inside the Library class?
    What are you trying to accomplish?
    I am following suggestions from an exercise within one of Stroustrups books.

    I have already designed a book class and the vector within the LIbrary class is to hold all of the book information.

    So I am thinking of creating a Library object that holds all the book objects, within the vector that :
    a)have already been created within the code, and
    b)the user will be able to add via a Library member function.

    So essentially, if I wish to use the add_book function to store books within the Book vector, I will first need to create a Libryary object that is constructed with the Book vector as an argument.

    Is that correct?

    Thanks.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, certainly that is valid approach. However, note that the vector holding the books is an internal member of the class. That is, the class uses this to store information about the books. You shouldn't have access to it from the outside. You don't need to know how the Library stores the book information. You only need to know how to use it. This is the principle we strive to attain.
    So, the add_book function should either add a new book to the library or several new books.
    It could look something like:

    Code:
    Library MyLibrary;
    Book MyBook1("My book 1"), MyBook2("My book 2");
    MyLibrary.add_book(MyBook1);
    std::vector<Book> Books;
    Books.push_back(MyBook1);
    Books.push_back(MyBook2);
    MyLibrary.add_book(Books);
    Last edited by Elysia; 06-18-2010 at 07:50 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks for that. Will put that into code shortly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem calling function
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2008, 03:23 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM