Thread: Classes

  1. #16
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    the destructor isn't obligatory, neither is the constructor
    you would only have a destructor if you have memory to free up so if you didn't use pointers, you wouldn't need the destructor and your code will look pretty

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  2. #17
    Me
    Join Date
    Jul 2006
    Posts
    71
    Oh, OK. Works for me.

    But, now when I have something like this:

    Code:
    class unit
       {
             int health, strength;
          public:
             unit (int, int);
             ~unit();
       };
    
    unit::unit(int hlth, int str) : health(hlth), strength(str)
       {
       }
    
    int main()
       {
          srand(time(NULL));
          unit hero(100,10);
    
          int *enemies[5];
    
          cin.get();
       }
    It gives me a compiler and linker output error.

    It says something like "undefined reference to `unit::~unit(void)'"

    And when I comment out the second line in int main ("unit hero(100,10);") it compiles fine. Am I defining the object correct?

  3. #18
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It gives me a compiler and linker output error.

    It says something like "undefined reference to `unit::~unit(void)'"
    This is because you put a destructor in your class description and the compiler is now expecting you to provide one. Take it out and the compiler will do its thing and generate one for you automatically.

    This:
    Code:
    class unit
    {
        int health, strength;
    public:
        unit (int, int);
    };
    Not this:
    Code:
    class unit
    {
        int health, strength;
    public:
        unit (int, int);
        ~unit();
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #19
    Me
    Join Date
    Jul 2006
    Posts
    71
    Ah, of course. Stupid mistake on my part. Thanks.

    Edit:

    I have another question. Is there anyway to have a pointer point to an object?

    I tried something to this effect, and it didn't work:

    Code:
          int *enemies[2];
    
          enemies[0] = &goblin;
    Last edited by relyt_123; 06-19-2007 at 02:05 PM.

  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You mean something like that?

    Code:
    unit* pHero = new unit(100, 10);
    But why use pointers if you absolutely don't need to?

    To make a collection of unit, use a standard container instead of raw arrays. E.g

    Code:
    std::vector<unit> troops;
    troops.push_back(unit(100, 10));
    
    //access troops[0] from here on
    Last edited by anon; 06-19-2007 at 03:21 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #21
    Me
    Join Date
    Jul 2006
    Posts
    71
    Ahh. The thought of using Vectors never even came to mind. Thanks. ^^

    However, I've come to yet another problem.

    Something like this gives me an error:

    Code:
          for(int i=0; i<=goblin.size(); i++)
             {
                (goblin.at(i)).displaystats();
             }
    displaystats() being a class function.

    It gives me an error for the line "(goblin.at(i)).displaystats();"

    Error:
    Code:
    55 c:\docume~1\me\desktop\test.cpp
     no matching function for call to `vector<unit,allocator<unit> >::at (int)'
    Anyone have any input?

    Edit: Nevermind. If I just use goblin[i] it works fine.
    Last edited by relyt_123; 06-19-2007 at 09:15 PM.

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Make sure you use < and not <=.

    Also, I'm surprised goblin.at(i) didn't work. It should. What compiler and version are you using?

  8. #23
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by Daved View Post
    Make sure you use < and not <=.
    Yup. Figured that out already.

    Quote Originally Posted by Daved View Post
    Also, I'm surprised goblin.at(i) didn't work. It should. What compiler and version are you using?
    Dev-C++ Compiler; Version 4

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That uses an old MingW version of gcc I believe. Consider upgrading. If you like Dev-C++, use the version 5 beta, which is like 4.9.9.2 or something. Other good, free options are Code::Blocks and VC++ 2005 Express.

  10. #25
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by Daved View Post
    That uses an old MingW version of gcc I believe. Consider upgrading. If you like Dev-C++, use the version 5 beta, which is like 4.9.9.2 or something. Other good, free options are Code::Blocks and VC++ 2005 Express.
    I tried VC++ 2005 Express but it said it was a 30 day trial. I'll try out Code::Blocks.

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I tried VC++ 2005 Express but it said it was a 30 day trial.
    Really?

    A couple years ago they said they would give it away free for one year. A year passed but people still kept recommending it and downloading it, so I assumed that it was still available.

    Looking at the website it appears that the express edition is still free (http://msdn.microsoft.com/vstudio/express/downloads/ make sure you pick Visual C++, not C#). I prefer Visual C++ but Code::Blocks might be more suitable for a beginner.

  12. #27
    Me
    Join Date
    Jul 2006
    Posts
    71
    Yup. That's the same page I downloaded it from. It said the same thing, that there was a 30 day trial, for both VC++ and VB. Though, Code::Blocks seems to be to my likings anyway so I think I'll stick with it. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM