Like Tree7Likes

Self referential class || Stroring an address of a class

This is a discussion on Self referential class || Stroring an address of a class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by krishnapollu @ sarath : I I just read thcode. I think ur problem is that u can't ...

  1. #16
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by krishnapollu View Post
    @ sarath :
    I I just read thcode. I think ur problem is that u can't search for a particular book. The mistake is , u have used == operator to compare strings.. Instead u should use strcmp()..

    @others :
    try to understand what is the problem he is facing.. And make him clarify it rather than confusing him with much complex things.
    1. You can compare std::string objects with ==.
    2. If you think using std::string is more complex than character arrays:
    ....either you are deliberately giving out wrong information.
    ....or more likely ...you use a prehistoric compiler and have never used strings.


    _____
    Also I think I agree with kmdv's post.
    @Laserlight : Don't you think using a std::list would be a wrong thing to do ...for a student when he is learning about the implementation of a linked list ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #17
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    I also think that this was an assignment more or less connected with the implementation of a linked list.

    To be more precise to my previous post: I do not encourage learning C++ by assigning C-style programs. BUT if someone has already faced a problem, wrote some code, he should be given an explanation of why it does not work, why it is a bad practise, and what is most important, run this code to see what happens. C/C++ is used for learning basic data structures, and it might have been this "separate assignment".

    It isn't a language-specific approach, it even isn't thing-specific.
    I never put signature, but I decided to make an exception.

  3. #18
    Registered User Sara th's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    10
    Thanks a lot Krishna! Error fixed and thank you all for the suggestions, especially kmdv.

    By the way, this is not at all an implementation of linked list. An assignment, true, to implement a bookshop using class. I wanted to do it with dynamic memory allocation rather than going for an array of objects.

  4. #19
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Sara th View Post
    Thanks a lot Krishna! Error fixed and thank you all for the suggestions, especially kmdv.By the way, this is not at all an implementation of linked list. An assignment, true, to implement a bookshop using class. I wanted to do it with dynamic memory allocation rather than going for an array of objects.
    Why would a bookshop need to know the address of the next bookshop ? Your assignment (probably) means you to implement a bookshop by a linked list of books. If nothing is specified about the way you do it ...it would be much much easier to put the books into a std::list or std::vector...depending on how you want to access them .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #20
    Registered User Sara th's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    10
    Already said I am not an expert programmer. Means I don't know how to use std::list and all

  6. #21
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    That is my point. You DO NOT need to be an expert to use them. It is much simpler than making your own implementation and using it. Also it'd hardly take more than half an hour to learn the basic usage of those containers .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #22
    Registered User Sara th's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    10
    Oh come on! I was not aware of these things when I wrote the program. Next time I will try

  8. #23
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by krishnapollu View Post
    @ sarath :
    I I just read thcode. I think ur problem is that u can't search for a particular book. The mistake is , u have used == operator to compare strings.. Instead u should use strcmp()..
    i thnk ur prblm is us txt spk n frm.


    Quote Originally Posted by krishnapollu View Post
    @others :
    try to understand what is the problem he is facing.. And make him clarify it rather than confusing him with much complex things.
    These "complex things" you are referring to are standard C++ and are in fact easier to implement than a linked list or char arrays. If you do not know how to use the standard features of the language then maybe you should learn before contributing to this board. The answer was in fact provided, if the OP used std::string vice char arrays the use of '==' would have worked.

    Quote Originally Posted by kmdv View Post
    I also think that this was an assignment more or less connected with the implementation of a linked list.
    Well, let's see what the OP has to say about that......
    Quote Originally Posted by Sara th View Post
    By the way, this is not at all an implementation of linked list. An assignment, true, to implement a bookshop using class. I wanted to do it with dynamic memory allocation rather than going for an array of objects.
    So, since we are in fact using C++, then a solution using the STL with std::list and/or std::vector would be the solution. kmdv, we are in the business of teaching people how to program. This means to use the STL when appropriate and to properly implement the C++ language, which is not using char arrays or any other C feature such as the implementation of a user defined linked list.

    Since you seemed to have missed the link Laser posted, here it is again: Learning C++ as a new language. Call me crazy but I think Bjarne Stroustrup might know what he is talking about when it comes to C++.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #24
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    For your information, I am aware of the links other post.

    This means to use the STL when appropriate and to properly implement the C++ language, which is not using char arrays or any other C feature such as the implementation of a user defined linked list.
    I understand, I said:

    I do not encourage learning C++ by assigning C-style programs. BUT [...]
    The difference is that he has already started coding. If he hadn't and had asked "how can I implement a sequence of bookshops?" I would have answered "use std::list or std::vector". But he has shown his code and I attempted to point out his mistakes.
    Last edited by kmdv; 08-09-2011 at 11:27 AM.
    Sara th likes this.
    I never put signature, but I decided to make an exception.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by manasij7479
    Don't you think using a std::list would be a wrong thing to do ...for a student when he is learning about the implementation of a linked list ?
    Yes. However, I also think that cluttering the learning of the implementation of a linked list with so many different member variables, and without any clarity in differentiating between a node and the entire linked list, is a wrong pedagogical approach.

    Quote Originally Posted by Sara th
    I was not aware of these things when I wrote the program. Next time I will try
    That's fine. You don't need to be aware of everything before you start.
    Sara th likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function or code to Determining class of IP address
    By hari_bly in forum C Programming
    Replies: 1
    Last Post: 06-10-2011, 01:59 AM
  2. how to return the address of a member of a class
    By *DEAD* in forum C++ Programming
    Replies: 6
    Last Post: 07-13-2007, 09:15 AM
  3. How do I make a class call it's own address
    By rakan in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 09:57 PM
  4. Address will always return true? (class vectors + member vars)
    By littleweseth in forum C++ Programming
    Replies: 12
    Last Post: 11-27-2003, 12:49 AM
  5. Address of a non-static class member function?
    By darksaidin in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2003, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21