OOP problem with "set"

This is a discussion on OOP problem with "set" within the C++ Programming forums, part of the General Programming Boards category; Greetings everybody! I searched the board and the google... but couldn't find any help. So, I hope somebody here does ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Question OOP problem with "set"

    Greetings everybody!
    I searched the board and the google... but couldn't find any help.
    So, I hope somebody here does surely have the answer!
    I just started with OOP. The following program works fine if I use list or vector class but doesn't work for set. I would wonder why??? gives 9 long errors and 4 warnings. Was supposed to handle the "set" in special way??? Please help!!
    Here's the code:
    Code:
    // Cat.h
    
    #ifndef ANIMALS_CAT_H
    #define ANIMALS_CAT_H
    
    #include <string>
    
    namespace animals
    {
    	class Cat
    	{
    	public:
    		Cat(std::string _name);
    		void eat(std::string food);
    		float weight;
    		std::string name;
    	};
    }
    #endif
    Code:
    // cat.cpp
    #include <iostream>
    #include "Cat.h"
    
    using namespace std;
    
    namespace animals
    {
    	Cat::Cat(std::string _name)
    	{
    		name = _name;
    	}
    	void Cat::eat(string food)
    	{
    		cout << name << " says: yum yum! " << food << endl;
    	}
    }
    Code:
    // main.cpp
    
    #include "Cat.h"
    #include <set>
    //#include <list>
    
    using namespace animals;
    
    typedef std::set <Cat> collection;
    //typedef std::list <Cat> collection;
    
    int main(int argc, char* argv, char *envp)
    {
    	collection cats;
    	//cats.push_back(Cat("Garfield"));
    	//cats.push_back(Cat("Pussy"));
    	cats.insert(Cat("Garfield"));
    	cats.insert(Cat("Pussy"));
    	for(collection::iterator i=cats.begin(); i!=cats.end(); ++i)
    	{
    		(*i).eat("lasagna");
    	}
    	return 0;
    }
    Thank you in advance!
    will be there one day!

    I usually use VStudio 6!

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The set maintains order for the items inside it. Thus, in order to put Cats into a set, you must be able to order the cats.

    The most simple way to order them is to use give the cat an operator <. For our purposes, we will say a cat is ordered by it's name, alphabetically, which, conventiently enough, std::string already provides. Also, you should use const for any member functions which do not modify any variables for a given class. For example, since eat does not change a Cat's name (it's only internal variable), it should be declared const.

    Note that I nuked the includes and just compiled it in one file, so it might need a little bit of reworking in that area. I killed the tabs too, sorry .

    Code:
    // Cat.h
    
    #ifndef ANIMALS_CAT_H
    #define ANIMALS_CAT_H
    
    #include <string>
    
    namespace animals
    {
    class Cat
    {
    public:
    Cat(std::string _name);
    void eat(std::string food) const;
    float weight;
    std::string name;
      bool operator < (const Cat& other) const;
    };
    }
    #endif
    
    
    // cat.cpp
    #include <iostream>
    
    using namespace std;
    
    namespace animals
    {
    Cat::Cat(std::string _name)
    {
    name = _name;
    }
    void Cat::eat(string food) const
    {
    cout << name << " says: yum yum! " << food << endl;
    }
    
    bool Cat::operator < (const Cat& other) const {
      return name < other.name;
    }
    }
    
    
    
    #include <set>
    //#include <list>
    
    using namespace animals;
    
    typedef std::set <Cat> collection;
    //typedef std::list <Cat> collection;
    
    int main(int argc, char* argv, char *envp)
    {
    collection cats;
    //cats.push_back(Cat("Garfield"));
    //cats.push_back(Cat("Pussy"));
    cats.insert(Cat("Garfield"));
    cats.insert(Cat("Pussy"));
    for(collection::iterator i=cats.begin(); i!=cats.end(); ++i)
    {
    (*i).eat("lasagna");
    }
    return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Lightbulb Thank you!

    thanks a lot silentStrike!!
    It worked just fine. still gives some long warnings... but executes fine!

    Code:
    std::string name;
      bool operator < (const Cat& other) const;
    };
    this part you added was little too high for me to understand
    any explantiona would be highly appreciated!

    thanks again!!
    will be there one day!

    I usually use VStudio 6!

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    if you post the warning we can help to resolve them and/or explain why you are getting them.

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It's operator overloading

    Before understanding that, I think this member function declration would be pretty clear

    Code:
    bool lessThan(Cat other);
    Except that has a few problems. First, it's passing by value, which means we are going to get a copy of the other cat (and cats are harder to clone then sheep, so we don't want that , realistically, it will mean that the std::string name member of the cat will be copied, which is potentially expensive with cats with long names), we don't want that.

    Code:
    bool lessThan(Cat& other);
    Now it is passing by reference, which means no copy is made, BUT, we can still modify the other cat. It's not really fair to modify the other cat in the lessThan function, so we fix that by making it const.

    Code:
    bool lessThan(const Cat& other);
    Now we can't modify the other cat, but we can still modify the 'this' cat... IE, the a object in a.lessThan(b).

    Code:
    bool lessThan(const Cat& other) const;
    Now this is good, we cannot modify any of the cats, and we are passing by reference. But what we really want is to be able to overload operators, this isn't Java afterall, code readability is important. Instead of using a .lessThan, we overload the < operator

    Code:
    bool operator < (const Cat& other) const;
    And there you have it, the function definition I originally posted.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Lightbulb Thanks a lot!

    Thanks a lot silentStrike for your great explanation. That helped me to understand!

    I would post the warining, if i got to that point now .
    First time it worked fine. So I tried to do the whole project slightly differently from the scratch. But I keep getting this errors for both the projects that was working before and the one I just made.
    The errors are:
    Code:
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/Project1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    I serched the web and got this:
    http://<br /> <a href="http://www.c...l#external</a>
    but didnt help!
    Poor me! had that problem once before too.. but somehow i happened to fix it and don't remember what i did...
    does anyone know its permanent solution???
    Thanks in advance!
    will be there one day!

    I usually use VStudio 6!

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    OOpsss I can compile!

    Okay, I forgot that I could still compile... ...
    here are the warnings i get:
    Code:
    main.cpp
    C:\OOP\Project1\main.cpp(21) : warning C4786: 'std::reverse_bidirectional_iterator<std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals
    ::Cat> >::iterator,animals::Cat,animals::Cat &,animals::Cat *,int>' : identifier was truncated to '255' characters in the debug information
    C:\OOP\Project1\main.cpp(21) : warning C4786: 'std::reverse_bidirectional_iterator<std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals
    ::Cat> >::const_iterator,animals::Cat,animals::Cat const &,animals::Cat const *,int>' : identifier was truncated to '255' characters in the debug information
    C:\OOP\Project1\main.cpp(21) : warning C4786: 'std::pair<std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >::iterator,std::_T
    ree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >::iterator>' : identifier was truncated to '255' characters in the debug informati
    on
    C:\OOP\Project1\main.cpp(21) : warning C4786: 'std::pair<std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >::const_iterator,s
    td::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >::const_iterator>' : identifier was truncated to '255' characters in the deb
    ug information
    c:\program files\microsoft visual studio\vc98\include\xtree(183) : warning C4786: 'std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals
    ::Cat> >::~_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >' : identifier was truncated to '255' characters in the debug informa
    tion
    c:\program files\microsoft visual studio\vc98\include\xtree(160) : warning C4786: 'std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals
    ::Cat> >::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >' : identifier was truncated to '255' characters in the debug informat
    ion
    c:\program files\microsoft visual studio\vc98\include\utility(21) : warning C4786: 'std::pair<std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::alloca
    tor<animals::Cat> >::iterator,bool>::pair<std::_Tree<animals::Cat,animals::Cat,std::set<animals::Cat,std::less<animals::Cat>,std::allocator<animals::Cat> >::_Kfn,std::less<animals::Cat>,std::allocator<animals::Cat> >::iterator,bool>' : identifier wa
    s truncated to '255' characters in the debug information
    Those two errors i posted above were while linking!
    Thanks!
    will be there one day!

    I usually use VStudio 6!

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Heh, MSVC6..

    You can disable those warnings with

    Code:
    #pragma warning(disable:4786)  // get rid of long name warnings on MSVC
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Angry warnings no problem! but the errors!

    The warning were just fine! wouldn't cause any problem for the program to run! but the two errors i keep getting!!!!!!
    I posted the errors two posts above!!
    Here it is again, for your convenience!
    Code:
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/Project1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    Please help me with these erros! It is pulling my hair!!!
    Thanks again!!
    will be there one day!

    I usually use VStudio 6!

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you have mistakenly set it for a windows app when you wanted console app. Navigate thru the project options and change the subsystem:windows to subsystem:console
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Thanks Stoned_Coder

    Thanks a lot sir!
    I recreated the whole project actually and carefully selected the console app. It worked without any problem!
    Thanks a lot agian!
    will be there one day!

    I usually use VStudio 6!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 09:32 AM
  5. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM

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