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. question on templates

question on templates

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasewpfalgorithmsdata-structures
1 Posts 1 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, I have two functions: [code] a) Member* get_member(Book* book); b)Book* get_book(Member* member); [/code] The implementation code for the function prototypes is: a) [code] template Member* AssociationList::get_member(Book* book) { Member* member=0; bool searching=true; int index=0; while(searching) { if (this->association_list[index]) if (this->association_list[index]->linked_book() ==book) { member=this->association_list[index]->linked_member(); searching=false; } else index++; else index++; if (searching && (index == LIST_SIZE)) { searching=false; } } return member; } [/code] b) [code] template Book* AssociationList::get_book(Member* member) { Book* book=0; bool searching=true; int index=0; while(searching) { if (this->association_list[index]) if (this->association_list[index]->linked_member()==member) { book=this->association_list[index]->linked_book(); searching=false; } else index++; else index++; if (searching && (index == LIST_SIZE)) { searching = false; } } return book; } [/code] All I want to do is use one generic function for the two, so that from the main.cpp: [code] void main() { Listmember1; Listbook1; char menuChoice; do { cin>>menuChoice; switch(menuChoice) { case '1': cout<<'\n'; book1.addElement("Books"); break; case '2': cout<<'\n'; break; case '3': cout<<'\n'; book1.displayElement("Books"); break; case '4': cout<<'\n'; /*member1.addElement("Member");*/ break; case '5': cout<<'\n'; break; case '6': cout<<'\n'; /*member1.displayElement("Member");*/ break; case '7': cout<<'\n'; //book1.borrowElement(); break; case '8': cout<<'\n'; //book1.returnElement(); break; default: cout<<'\n'; cout<<"Invalid Selection\n"; } }while(menuChoice != '0' && !cin.eof()); } [/code] ...to call each one and dispaly either the books from a book array or the members from a member array. The'display' implementation code is: [code] template void List::displayElement(char* type) { if (num_elements == 0) cout<<"No "<

    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