Syntax Help, class interactions and functions..

This is a discussion on Syntax Help, class interactions and functions.. within the C++ Programming forums, part of the General Programming Boards category; Code: class Creature { struct Stats { int Health; int Attack; int Defense; }; Creature Attack(Creature A) { A::Stats::Health - ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Syntax Help, class interactions and functions..

    Code:
    class Creature
    {
    	struct Stats
    	{
    		int Health;
    		int Attack;
    		int Defense;
    	};
    
    	Creature Attack(Creature A)
    	{
    		A::Stats::Health - Creature::Stats::Attack;
    	}
    
    };
    
    class Human : Creature
    {
    };
    Okay, so my attack function throws out multiple errors

    Code:
    c:\documents and settings\home\my documents\visual studio 2005\projects\syntax testing 2\syntax testing 2\testing.cpp(17) : error C2653: 'A' : is not a class or namespace name
    c:\documents and settings\home\my documents\visual studio 2005\projects\syntax testing 2\syntax testing 2\testing.cpp(17) : error C2065: 'Health' : undeclared identifier
    c:\documents and settings\home\my documents\visual studio 2005\projects\syntax testing 2\syntax testing 2\testing.cpp(17) : error C2326: 'Creature Creature::Attack(Creature)' : function cannot access 'Creature::Stats::Attack'
    So I can't really figure out what is wrong with my syntax, I know I need the input creatures health to be decreased by the attackers attack number... But it just isn't working as simple as I'd like..

    Help?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Before you try to use inheritance you really need to read more about classes... And before you attempt a game you need to be quite comfortable with them.
    Your function says it returns a creature, but all that the function contains is a statement that won't compile and if it would, it would do nothing.
    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).

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Alright I did not ask you to preach to me. I know it doesn't return anything, but that's not why I have an error, I know plenty about classes and inheritance.

    I could pull out some old code of mine if I had it, I've been here, done that, I just forget the syntax.

    Why doesn't the line

    Code:
    A::Stats::Health - Creature::Stats::Attack;
    Work?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because syntax like that seems to expect everything to be static.

    Sorry if I sound preaching, but your code gives the impression that you are trying to do something that is beyond your abilities.

    May-be something like this (this creature attacks creature c)?

    Code:
    class Creature
    {
    	struct Stats
    	{
    		int Health;
    		int Attack;
    		int Defense;
    	};
            Stats statistics; //needs the actual statistics
    
            ....
    	void Attack(Creature& c)
    	{
    		c.statistics.Health -= statistics.Attack;
    	}
    
    };
    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).

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Oh thats why, duh, I didn't create an instance of statistics...

    I'll have to make it so the constructor does that...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    And by the way... How does code I wrote about 5 minutes ago suggest I don't know what I'm doing?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    88
    Because your Stats struct isn't static (which, BTW, what is the point of that struct anyway?), you can't use the scope resolution operators like you are. Hence the errors. You pretty much need to rethink the design on this.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    88
    >How does code I wrote about 5 minutes ago suggest I don't know what I'm doing?

    The fact that it's completely wrong in almost every way?

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Shamino View Post
    And by the way... How does code I wrote about 5 minutes ago suggest I don't know what I'm doing?
    Well, if you trim out the classes and nested structs and all the advanced stuff, it seems that you hoped something like that would do something

    Code:
    int foo()
    {
        int a, b;
        a - b; ///???
    }
    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).

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    exactly, but I forgot the syntax sooooooooo.

    Thus the question.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    So you mean to tell me that this bit of code...

    Code:
    class Creature
    {
    	struct Stats
    	{
    		int Health;
    		int Attack;
    		int Defense;
    	};
    
    	void Attack(Creature c)
    	{
    		c.statistics.Health -= statistics.Attack;
    	}
    
    	Stats statistics;
    
    };
    Is completely wrong in every way?

    WTF?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Quote Originally Posted by UMR_Student View Post
    >How does code I wrote about 5 minutes ago suggest I don't know what I'm doing?

    The fact that it's completely wrong in almost every way?
    Right I have one line of code causing me errors and alluva sudden its all wrong in every way...

    No, I'm done with putting it lightly.

    ........ off you silly ..........
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    maybe this will help you out a little?

    Code:
    #ifndef CLASS_HPP
    #define CLASS_HPP
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    struct STATS_
    {
        int Health;
        int Attack;
        int Defense;
    
        long experience;
        long neededexp;
    };
    
    class Creature : public STATS_
    {
        Creature();
        ~Creature();
    
        bool Attack (Creature target);
    
    };
    #endif
    it is far from working but I think that is what you were trying to do.
    Also dont forget to put the code in an implementation file, not in the definition

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Nah stats inheriting creature is illogical, because creatures have stats, but are not stats themselves.

    A nested struc is what I'm going for I'm pretty sure.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Will you ever stop...

    So you mean to tell me that this bit of code...

    Code:
    class Creature
    {
    	struct Stats
    	{
    		int Health;
    		int Attack;
    		int Defense;
    	};
    
    	void Attack(Creature c)
    	{
    		c.statistics.Health -= statistics.Attack;
    	}
    
    	Stats statistics;
    
    };
    Is completely wrong in every way?

    WTF?
    This is not the code you presented at first. This is based on the code I showed. And now it's not wrong in about every way, but in some small way: it's not going to reduce the health of the creature you pass in the argument to Attack, because you pass a copy (Creature c) - and not a reference (Creature& c) - and this copy goes out of scope at the end of the function.

    May-be you are great programmer, but if you don't know or can't remember the general syntax of a language, Internet boards may not be the best place to ask.
    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).

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

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