classes and functions

This is a discussion on classes and functions within the C++ Programming forums, part of the General Programming Boards category; Code: class zombie { } ; See there ^ Class zombie is missing ; at the bracket at the end, ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Code:
    class zombie {
    };
    See there ^ Class zombie is missing ; at the bracket at the end, I added it in red.

    Code:
    user.takedamage (attackpower);//'user' undeclared (first use this function)
    There's no such thing as "user" in your class - what's it supposed to be? You must declare (or define) something that's used!

    Same thing here with class player - all classes must begin with { and end with }; (notice the ; at the end).

    Code:
    bob.takedamage (attackpower);//'bob' undeclared (first use this function)
    Same thing there - you must define bob.

    Code:
    zombie bob;//no matching function call for 'zombie::zombie()' //candidates are zombie::zombie (const zombie&), and 'zombie::zombie(int)'
    player user;//same as above only 'player::player()'
    For both classes, you have but one constructor - and that constructor takes an int as argument. Here you don't pass any argument and the compiler can't find a constructor that takes no arguments, so it complains.
    Declare a constructor that takes no arguments or simply pass in the appropriate argument to the constructor.

    Code:
    while (bob.life() == 1 and user.life() == 1) {
    and is not a keyword; what you're looking for is probably && (basically and in C++).

    Code:
        int life () {
            if ( hp > 0 ) {
                return 1;
            }
            if (hp <= 0 ) {
                return 0;
            }
        }
    Suggest you do do just
    Code:
        int life () {
            if ( hp > 0 ) return 1;
            return 0;
            }
        }
    The compiler can't know with your IFs that you're actually returning a value at all control paths - this will fix the warning.
    Same goes for player::life.

    Code:
            if (attacktype = 1) {
                gnaw();
            }
            if (attacktype = 2) {
                bite();
            }
            if (attacktype = 3) {
                moan();
            }
    = is assignment, not comparison. What you're looking for is ==.

    And really start using tabs instead of spaces. I really recommend it - it's much better.
    Also remember from before, when they said big names for your classes? Try that. Do Zombie/CZombie or Player/CPlayer. It's better that way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    now I get a new one:

    invalid use of member (did you forget the '&' ?)

    after every use of the function bob.life or user.life

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, class declarations end with a semicolon:
    Code:
    class Zombie
    {
        ...
    };
    And then user and bob are indeed undeclared in the member functions of zombie and player. user and bob only exist in the function battle. If you wish the member functions to know about them, you'll need to pass them into those functions.

    Alternatively each attack method might return the amount of damage, and you call takedamage for user and bob in the function battle, using this return value.

    In battle you fail to construct the objects because the only constructor takes an int (initial hp). It might help if you implemented these constructors and then created your objects, calling the existing constructor:
    Code:
    Zombie bob(500);
    Player user(100);
    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).

  4. #19
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    alright, i'll work on fixing all that.
    Just out of curiosity, is there a way to easily transfer all the variables of a class from one function to another. Because I want the user to be able to have stats such as defence and attack that can be improved and carry over without having to use another variable to transfer it from battle to battle.
    And finally, I just want to see if I get this, each function has its own separately defined variables, classes, and structures, so you have to carry them over in the function arguments. But what if I make it an inline function?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Just out of curiosity, is there a way to easily transfer all the variables of a class from one function to another. Because I want the user to be able to have stats such as defence and attack that can be improved and carry over without having to use another variable to transfer it from battle to battle.
    I think that it is good to have to pass your player object by reference to the functions that need to access the player stats. You could make it a singleton or even use global variables, but I suggest not doing so at this point.

    And finally, I just want to see if I get this, each function has its own separately defined variables, classes, and structures, so you have to carry them over in the function arguments. But what if I make it an inline function?
    We say that a function may have local variables. Declaring a function as inline does not change that (inline cannot change how a function functions), and in fact the compiler is free to ignore the inline keyword as it sees fit.
    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

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    I believe you should slow down a tad and start learning basic syntax since at this point you've done a lot of silly, simple mistakes. Start thinking about how you want it all to work and create a simple class that is correct - where the syntax matches what it should before moving on more advanced things.
    And keep all stats within the class itself.
    As for inline, whether or not it's inline, it's a function. And for functions, there are rules. The compiler will make sure your code properly if it inlines and that's IF. You don't have to worry about it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 10:19 AM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 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

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