fast simple question

This is a discussion on fast simple question within the C++ Programming forums, part of the General Programming Boards category; I have a class which has a enum in protected, and a function in public, and this function uses the ...

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    fast simple question

    I have a class which has a enum in protected, and a function in public, and this function uses the enum, still I get...
    Code:
    error C2511: 'void Cball::move(Cball::posNeg)' : overloaded member function not found in 'Cball'
    here's my code...
    Code:
    class Cball
    {
    public:
    	Cball();
    	~Cball();
    
    	float x, y;
    	float speed;
    	bool inAir;
    
    	void sendDir();
    	void move(enum posNeg xDir);
    
    protected:
    	int chooseDir();
    	enum posNeg { POSITIVE = 1, NEGATIVE = -1};
    };
    Last edited by Akkernight; 01-26-2009 at 06:20 AM.
    Currently research OpenGL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Provide the smallest and simplest program that demonstrates the error. Your class definition looks fine, though the enum should be declared before the move member function is declared (and the enum keyword is optional when referring to the enum type).
    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

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Code:
    void Cball::move(enum posNeg xDir){
    	while(inAir){
    		x = (x + speed) * xDir;
    	}
    }
    That's what I'm working on...
    And should I then have protected above public?
    Currently research OpenGL

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Akkernight
    That's what I'm working on...
    This compiles without even a warning for me:
    Code:
    class Cball
    {
    protected:
    	enum posNeg { POSITIVE = 1, NEGATIVE = -1};
    public:
    	Cball();
    	~Cball();
    
    	float x, y;
    	float speed;
    	bool inAir;
    
    	void sendDir();
    	void move(enum posNeg xDir);
    
    protected:
    	int chooseDir();
    };
    
    int main() {}
    
    void Cball::move(enum posNeg xDir){
    	while(inAir){
    		x = (x + speed) * xDir;
    	}
    }
    Quote Originally Posted by Akkernight
    And should I then have protected above public?
    Up to you (you can even use it more than once, as in my example), but why are your member variables public?
    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

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Well, I did that too, moved it above, and it works, and I'm not used to classes, so it's not set up the best xP I'll get that along the road... Hopefully
    Currently research OpenGL

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And generally, things should either be public or private (because you want other stuff to use it - enum's are generally harmless when visible outside the class that defines them, so I don't see a reason why not).

    The reason that "protected" is "bad" is that anyone can inherit a class and get access to anything "protected" [unless it's explicitly made uninheritable - in which case protected makes no sense anyways, because it doesn't "add" anything above private for classes that can't be used as a base-class].

    For example:
    Code:
    class MyClass
    {
       protected: 
            int mySecretValue;
    };
    
    ...
    // Other code:
    #include <myclass.h>
    
    class Subvert: public MyClass
    {
    public:
        void DoSubvert(int x)
        {
            mySecretValue = x;
        }
    };
    So, either a value is private, or it is public - there are some very special cases where protected does make sense, but really, it's not particularly meaningful.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Since the class has no virtual functions, it is not designed to be a base class, so instead of protected access you might as well use private access. Generally, you should make your member variables private since you want to encapsulate the implementation of your class such that you can change it without the users of the class needing to change their code.
    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

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ok, I'll get to that But now I'm too frusterated xP
    I got this other problem, which prolly has a simple reason, I just can't figure it out, since I'm frusterated and at work xP
    anyways here's the code...
    and it is called like sendDir() -> chooseDir() -> Move(), now I'm sure one of this is doing it, prolly chooseDir(), 'cause the game crashes when I press Space, which executes this
    Code:
    void Cball::sendDir(){
    	int choice = chooseDir();
    	if(choice == 1) move(POSITIVE);
    	if(choice == 2) move(NEGATIVE);
    
    }
    Code:
    int Cball::chooseDir(){
    	int random_integer;
    	vector<int> randVec;
    	for(int i = 0; i<20; ++i){
    		random_integer = (rand()%2)+1;
    		randVec.push_back(random_integer);
    	}
    	int twos = 0, ones = 0;
    	for(int i = 0; i != randVec.size(); ++i){
    		if(randVec[i] == 1) ++ones;
    		if(randVec[i] == 2) ++twos;
    	}
    	if(ones > twos) return 1;
    	else return 2;
    
    }
    Code:
    void Cball::move(enum posNeg xDir){
    	while(inAir){
    		x = (x + speed) * xDir;
    	}
    }
    Currently research OpenGL

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Akkernight
    I got this other problem, which prolly has a simple reason, I just can't figure it out
    I got the solution, but I just can't figure the problem out!

    Just kidding, what's the problem?
    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

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    It freezes the application xP Like, I have to press Alt + F4, and it all looks like it should work, to me :P
    Currently research OpenGL

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does choosedir really have to be that complicated? Why not count one's & two's in the first loop, rather than store them in a vector? And for all intents and purposes, you've just written a more complex version of
    Code:
    return rand() % 2 + 1;
    - it may look much more complicated, but the result is either 1 or 2, and it will probably vary from one to the other each time you call it.

    Don't worry - when I was a beginner, I also wrote lots of code that was unnecessarily complicated - only I didn't have anyone to show me how I could do it better then.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Oh yeah... I can skip the vector xP But I want it to check 20 times, and then see which one came up the most, makes it a little more random, or it should... xP
    Currently research OpenGL

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Akkernight
    It freezes the application xP Like, I have to press Alt + F4, and it all looks like it should work, to me :P
    Of course, I would have no way of testing that since I would need to write the code that actually uses the class, and if the class implementation is correct, the bug would not surface since it exists in the code that you did not show.

    Really, you need to post the smallest and simplest (compilable) program that demonstrates the problem.

    Quote Originally Posted by Akkernight
    But I want it to check 20 times, and then see which one came up the most, makes it a little more random, or it should.
    I suggest that you try the straightfoward implementation first. Get something working, and then improve it. As I said, if you design your class interface well, you can always change the implementation without needing to change the code that uses the interface of your class.
    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

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Akkernight View Post
    Oh yeah... I can skip the vector xP But I want it to check 20 times, and then see which one came up the most, makes it a little more random, or it should... xP
    My guess is that it's not "more random" - just like tossing a coin 20 times and counting if heads came up more often than tails is no more random than tossing a coin once.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    No, I'm actually gonna disagree with you matsp xP no algorithm calculates randomness right.. It's impossible, so if you toss a coin in real life, you get the real randomness, if you do rand() in code, you get some algorithm of predictable numbers :P
    and laser, it's running on the HGE engine, I could give you the application, but you'd need the engine, and two .png files xP
    Code:
    if(hge->Input_GetKey() == HGEK_SPACE) ball.sendDir();
    might help.. .That's what calls sendDir(), and the only thing that has anything to do with it
    Last edited by Akkernight; 01-26-2009 at 07:18 AM.
    Currently research OpenGL

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

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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