Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template problem

Template problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresquestionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grscot
    wrote on last edited by
    #1

    Dear all,
    The concept behind the code is:
    There is an associationlist that holds association pointers. Each association pointer has two pointers, that point to a book and to a member object respectively.
    Now what I'm trying to do is:
    To have the association pointer to use its two pointers to point to an array of books(booklist, up to 10 books) and to an array of members (memberlist, up to 10) respectively.
    If you copy and paste the code the program works ok only for books(adding a new book to the booklist(up to 3 for test purposes) and displaying the books).
    If one wants to add a member and display the members, they have to use the function: get_book(); in the file:
    template
    void List::displayElement(char* type) of the List.cpp, and also to put in comments the //book1.displayElement("Books"); of the main.cpp file.

    If I don't use the comments in the main.cpp file and use the get_book() function then an error will occur: " 'get_book':cannot convert parameter 1 from 'class Book*' to 'class Member*' "

    Could someone suggest a solution using a single template function, for displaying members and books?
    The code of each file is:

    //Association.h #ifndef _ASSOCIATION #define _ASSOCIATION //#include "Book.h" //#include "Member.h" template class Association { public: //Sets up book and member with parameters Association(Book* book, Member* member); //Returns Book Book* linked_book(){return this->book;} //Returns Member Member* linked_member(){return this->member;} private: Book* book; Member* member; }; #endif

    //Association.cpp #ifndef _ASSOCIATIONCPP #define _ASSOCIATIONCPP #include "Association.h" template Association::Association(Book* book, Member* member) { this->book=book; this->member=member; } #endif

    //AssociationList.h #ifndef _ASSOCIATIONLIST #define _ASSOCIATIONLIST #include "Association.h" //#include "Book.h" //#include "Member.h" const int LIST_SIZE=100; template class AssociationList { public: //It initialises all slots in the associationlist array to zero. AssociationList(); /* It searches the associationlist, if book is found then returns the member that is connected with.*/ Member* get_member(Book* book); /* It searches the associationlist, if member is found then returns the book that is connected with.*/

    D 1 Reply Last reply
    0
    • G grscot

      Dear all,
      The concept behind the code is:
      There is an associationlist that holds association pointers. Each association pointer has two pointers, that point to a book and to a member object respectively.
      Now what I'm trying to do is:
      To have the association pointer to use its two pointers to point to an array of books(booklist, up to 10 books) and to an array of members (memberlist, up to 10) respectively.
      If you copy and paste the code the program works ok only for books(adding a new book to the booklist(up to 3 for test purposes) and displaying the books).
      If one wants to add a member and display the members, they have to use the function: get_book(); in the file:
      template
      void List::displayElement(char* type) of the List.cpp, and also to put in comments the //book1.displayElement("Books"); of the main.cpp file.

      If I don't use the comments in the main.cpp file and use the get_book() function then an error will occur: " 'get_book':cannot convert parameter 1 from 'class Book*' to 'class Member*' "

      Could someone suggest a solution using a single template function, for displaying members and books?
      The code of each file is:

      //Association.h #ifndef _ASSOCIATION #define _ASSOCIATION //#include "Book.h" //#include "Member.h" template class Association { public: //Sets up book and member with parameters Association(Book* book, Member* member); //Returns Book Book* linked_book(){return this->book;} //Returns Member Member* linked_member(){return this->member;} private: Book* book; Member* member; }; #endif

      //Association.cpp #ifndef _ASSOCIATIONCPP #define _ASSOCIATIONCPP #include "Association.h" template Association::Association(Book* book, Member* member) { this->book=book; this->member=member; } #endif

      //AssociationList.h #ifndef _ASSOCIATIONLIST #define _ASSOCIATIONLIST #include "Association.h" //#include "Book.h" //#include "Member.h" const int LIST_SIZE=100; template class AssociationList { public: //It initialises all slots in the associationlist array to zero. AssociationList(); /* It searches the associationlist, if book is found then returns the member that is connected with.*/ Member* get_member(Book* book); /* It searches the associationlist, if member is found then returns the book that is connected with.*/

      D Offline
      D Offline
      dog_spawn
      wrote on last edited by
      #2

      Could you cut down your code example and format it better? Do you expect someone to read all that code?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups