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
G

grscot

@grscot
About
Posts
16
Topics
15
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • operator= understanding problem
    G grscot

    Dear all, I am having a problem in understanding operator overloading in C++. Below is the code I am reading and its explanation. There are some points that I do not really understand. [CODE] class Ratio { public: Ratio(int =0, int =1); //default constructor Ration(const Ratio&); //copy constructor Ration& operator=(const Ratio&); //assignment operator private: int num, den; }; Ratio& Ratio::operator=(const Ratio& r) { num = r.num; den = r.den; return *this; } [/CODE] In the book it is written: [QUOTE] The assignment operator is a function that should return the value it assigns.(This is okay) The assignment operator should return a reference to the same type as the object being assigned. The return type is a reference to an object of the same class. ( I would say okay for both statements) But then this means that the function should return the object that is being assigned in order for the assignment chain to work. So when the assignment operator is being overloaded as a class member function, it should return the object that owns the call. Since there is no other name available for this owner object, C++ defines a special pointer, named this pointer, which points to the owner object. [/QUOTE] I do not really ubderstand the rest of the statements about the object that owns the call. Also I do not understand the implementation code of the operator= function. Why do we use the r object to access the member data and then assign them to the same member data? I do not understand the logic behind that piece of code. Regards, grscot

    C / C++ / MFC c++ json help question learning

  • I can't display book and borrower's details
    G grscot

    Dear all, I'm still struggling to display the book and borrower's details together. Could someone help me? regards, grscot

    C / C++ / MFC database algorithms data-structures help learning

  • I can't display book and borrower's details
    G grscot

    Dear all,
    I can't borrow a book even though the code does not have any error. When I try to display the book details along with the member's name (in case the member has borrowed the book), I can't display the member's name, but only the book details and a message: "no member has borrowed a book".

    Though the code works fine when I have one book and one member, but now that I'm using an array of books and members it doesn't work.

    The code that displays the book and the member's name when I have 1 book and 1 member is:
    void Book::display(Member* borrower) { cout<<"Book title and author are: "<<this->bookDetails<<"."<<endl; if (borrower) cout<<"The member's name is: "<<borrower->get_Data()/*get_name()*/<<"."<<endl; else cout<<"No member has borrowed a book."<<endl; }

    char* get_Data(){return this->name;}

    returns the member's full name, entered in the constructor

    I have an associationlist that holds pointers to associations. Each association has two pointers, one to a book object and the other to the member object.

    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); Member* get_data(Book* book); /* It searches the associationlist, if member is found then returns the book that is connected with.*/ //Book* get_book(Member* member); Book* get_data(Member* member); /* Checks that book/member not already linked creates association if objects are free to link returns whether or not link was valid */ bool link(Book* book,Member* member); /* Checks that book and member are linked deletes association if they are linked returns whether or not unlinking was valid */ bool unlink(Book* book,Member* member); private: Association<Book,Member>* association_list[LIST_SIZE]; };

    The code that performs the linking between 1 book object and 1 member object is the following:

    template bool AssociationList::link(Book* book,Member* member) { bool searching=true; bool success=true; int index=0; int free_slot; while(searching) { if(this->association_list[index]) if((this->association_list[ind

    C / C++ / MFC database algorithms data-structures help learning

  • Can't display items from an array.
    G grscot

    Dear all,
    All I'm trying to do is to enter 3 books from the keyboard to an array and then traverse the array to display the elements and also prompt the user to enter the book names to delete them from the array(if the book exists into the array).

    The code that adds to the array is:

    template<class Object> void List<Object>::addElement(char* type) { if (this->num_elements == MAX_ELEMENTS) { cout<<"No more room in the "<<type<<" array.\n"; cout<<"The maximum number of "<<type<<" is "<<MAX_ELEMENTS<<"."<<endl; } else { this->element_list[num_elements]=new Object; (this->num_elements)++; } }

    The code that displays the elements is:

    template void List::displayElements(char* type) { if (num_elements == 0) cout<<"No "<num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_data(element_list[element])); } } The code for deleting is: `` template void List::removeElement(char* type) { char* item; if (this->num_elements == 0) cout<<"There are no "<num_elements; element++) if (strcmp(item,element_list[element]->get_data()) == 0) //cout<<"Element found\n"; element_list[element]='\0'; else cout<<"Element not found\n"; } But there is a problem after deleting a book from the array. I can't display the elements of the array, after a book has been deleted. For example if I enter a, a as the first book and b, b as the second book and then after deleting b, b, try to display the books into the array, I will only see the details of the first book and after that the program will be terminated due to an error. The error occurs at the file: `Book.cpp Book::Book() { cout<<"Book constructor called called\n"; this->bookDetails=get_string_ver2("input book title and author separated by a comma and a space character: "); } Book::~Book() { if (this->bookDetails) delete [] this->bookDetails; } void Book::display(Member* borrower) { cout<<"Book title and author are: "<bookDetails<<"."<` ``

    C / C++ / MFC help c++ data-structures tutorial learning

  • Can I use 1 template function instead of 2?
    G grscot

    Dear all,
    I have two template functions for displaying books and members respectively from arrays.

    template<class Object> void List<Object>::displayBook(char* type) { if (num_elements == 0) cout<<"No "<<type<<" is found in the "<<type<<" array.\n"; else for(int element=0; element<this->num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_member(element_list[element])); } } template<class Object> void List<Object>::displayMember(char* type) { if (num_elements == 0) cout<<"No "<<type<<" is found in the "<<type<<" array.\n"; else for(int element=0; element<this->num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_book(element_list[element])); } }

    The function prototypes are:

    Member* get_member(Book* book); Book* get_book(Member* member);

    and the functions implementation code is:

    template<class Book,class Member> Member* AssociationList<Book,Member>::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; } template<class Book,class Member> Book* AssociationList<Book,Member>::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; }

    Could someone tell me how can I use One Template Function Instead of Two (if this is possible)?

    Apologies for posting the similar question many times.
    grscot

    C / C++ / MFC question database algorithms data-structures learning

  • Template again
    G grscot

    Dear all, The only reason I posted the whole code was to give one the chance to run the program and see in practice the problem. But I guess that is too much. Anyway, The implementation of the two functions is: [code] //It returns the member. 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] [code] //it returns the book 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] The template file that I use to display the books is: [code] template void List::displayElement(char* type) { if (num_elements == 0) cout<<"No "<num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_member(element_list[element])); } } [/code] But it does not work for Members. I've tried to use the recommendations you've told me but it seems not to work. The program works ok, when I call the functions: book1.addElement("Books"); and book1.displayElement("Books"); to add a book and to display them respectively. But when I use the : member1.addElement("Member"); and member1.displayElement("Member"); it dispalys the problem: The compiler complains at the point: this->element_list[element]->display(association_list.get_member(element_list[element])); saying that : get_member: cannot convert parameter one from Class Member* to class Book*. Can you correct any mistakes or suggest any solutions? Please if you make any suggestion adjust them to my code, if possible. Best Regards, grscot

    C / C++ / MFC database algorithms help question learning

  • Template problem
    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.*/

    C / C++ / MFC help c++ data-structures question learning

  • question on templates
    G grscot

    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 "<

    C / C++ / MFC c++ database wpf algorithms data-structures

  • Template question
    G grscot

    Dear all, I have two functions: a) Member* get_member(Book* book); b)Book* get_book(Member* member); The implementation code for the function prototypes is: a) 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; } b) 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; } All I want to do is use one generic function for the two, so that from the main.cpp: void main() { Listmember1; Listbook1; char menuChoice; do { cout<<'\n'; cout<<'\n'; cout<<"**** Main Menu ****"<>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

    C / C++ / MFC c++ database algorithms question learning

  • More templates problems
    G grscot

    Dear all,I'm really sorry for asking again about the same thing, but I don't really undersatnd what's going on with the templates. I've tried to use the template R* get_member(P* pArg) as adviced, but it seems that my program is more complicated. So, I think I have to submit you a significant part of my code to give me an extra help (and final one I hope ) to overcome this awful template obstacle. AssociationList.h 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.*/ //Book* get_book(Member* member); template R* get_member(P* pArg); /* Checks that book/member not already linked creates association if objects are free to link returns whether or not link was valid */ bool link(Book* book,Member* member); /* Checks that book and member are linked deletes association if they are linked returns whether or not unlinking was valid */ bool unlink(Book* book,Member* member); private: Association* association_list[LIST_SIZE]; }; AssociationList.cpp template AssociationList::AssociationList() { cout<<"AssociationList constructor called\n"; int index; for(index=0; indexassociation_list[index]=0; } template R* AssociationList::get_member(P* pArg) { R* 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; } //template //Member* AssociationList::get_member(Book* book) /*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->asso

    C / C++ / MFC c++ database wpf algorithms data-structures

  • another template question
    G grscot

    Dear all, I 've understood that the template function for the two functions : int AddNumbers(int a, int b); float AddFloats(float a, float b); is: template T Add(const T &a, const T&b) { return a+b; } because the arguments' return type is the same as the functions' return type. But what is the template function when the return type of the arguments is different from the return type of the function? For example: Member* get_member(Book* book); Book* get_book(Member* member); What is now the template function of the two above functions? Regards, grscot

    C / C++ / MFC question tutorial learning

  • Template question problem
    G grscot

    Dear all, I would like to ask if it is possible to have a template function as a general function for two functions that have a different return type. For example: int AddNumbers(int a, int b); float AddFloats(float a, float b); What would it be the template function for the above example? Regards, grscot

    C / C++ / MFC question help tutorial lounge

  • Can't use template function to do 2 similar things(display member,book)
    G grscot

    Dear all, I am using the following function to display the members of a memberlist. I need also to display the books of a booklist. If I use the comments then there is no compilation problem, but I cant display the books, on the other hand if I do not use the comments there is a compilation problem because get_member() function does not accept class Member* argument, but only class Book* argument. How can I generalise the function to accept both arguments, and display both books and members using the same function? template void List::displayElement(char* type) { if (num_elements == 0) cout<<"No "<num_elements; element++) { cout<<'\n'; //this->element_list[element]->display(association_list.get_member(element_list[element])); } } Regards, grscot

    C / C++ / MFC question help learning

  • Linking problem in C++(Unresolved external symbol...)
    G grscot

    Hi everybody, I have a number of .cpp and .h files. Each couple of them(.cpp and the corresponding .h) is compiled correctly. The application compiles successfully, but in the link process an error occurs. The error is: main.obj : error LNK2001: unresolved external symbol "public: __thiscall AssociationList::AssociationList(void)" (??0?$AssociationList@VMember@@VBook@@@@QAE@XZ) Could anyone tell me what is all about? P.S If necessary please tell me to send over the code. Regards, grscot

    C / C++ / MFC c++ help question

  • Problem in deleting a specific record in an array in C++
    G grscot

    Hi everybody, How can I delete a specific record from an array in the Visual C++ environment? I have an array of object pointers. I want to enter a book title for example from the keyboard and then to have an option of deleting it, from the array. Is this possible? Regards, grscot

    C / C++ / MFC c++ question data-structures help tutorial

  • Template question in C++
    G grscot

    Dear all, I need to use an array of Book pointers (BookList) and Member (MemberList). Both of them are compound (container) classes. The component objects are books and members (up to 10)for each list respectively. Is the below implementation of the array.h file correct? #ifndef _ARRAY #define _ARRAY const int MAX_VALUE=10; template class Array { public: Array(int maximum=10); ~Array(); ...... private: Object* array_list[MAX_VALUE]; //To keep track of the elements into the list. int num_of_elements; }; #endif Regards, grscot

    C / C++ / MFC question c++ docker data-structures learning
  • Login

  • Don't have an account? Register

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