Pointer in Function problem

This is a discussion on Pointer in Function problem within the C++ Programming forums, part of the General Programming Boards category; im trying to make my badguy pointer work. but i have no idea where the problem lies this is the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    Pointer in Function problem

    im trying to make my badguy pointer work. but i have no idea where the problem lies

    this is the function code
    Code:
    int Monsters::DistanceToPlayerX(int curX,int PlayerX,Monsters& pBadGuy){
         *pBadGuy->GetX() = curX;
         ASCIIMan.GetX() = PlayerX;
         return (X+PlayerX);
         };
    the error is
    character.h: In member function `int Monsters:istanceToPlayerX(int, int, Monsters&)':
    character.h:37: error: base operand of `->' has non-pointer type `Monsters'

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    becase pBadGuy is declared as reference - you are asked not to use dereference operator ->
    Use
    pBadGuy.GetX()
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    oh i always thought that i had to use -> evertime i use a pointer and try to use a method

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by lilhawk2892 View Post
    oh i always thought that i had to use -> evertime i use a pointer and try to use a method
    It is correct. But you do not use a pointer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    On the other hand, the very fact that a "getter function" can be an lvalue probably means you have a very poor design; your getter function should be const, and returning a copy, not a reference to the actual data (if you DO need a reference, at least return a const reference).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Indeed, and since it is not a pointer, you may wish to change the naming of the parameter.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    ya i do have poor design. its not my specialty.. for example. heres my entire character.h file

    Code:
    #ifndef CHARACTER_
    #define CHARACTER_
    #include "headers.h"
    #include <string>
    
    class Character
    {
          public:
          //methods:
                    void Init(int NewX, int NewY, char NewChar);
                    void Draw();
                    int Walk(int newX, int newY);
    
                    void Attack(Character);
                    int  GetHealth();
                    int  GetX();
                    int  GetY();
                    char GetName();
                    
    private:
          //attributes
                    char name;
                    int  Health;
                    int  X;
                    int  Y;
                    char ASCIICharacter;
    };
    class Monsters : public Character {
          public:
                 void MonsterAI();
                 int DistanceToPlayerX(int curX, int PlayerX);
                 int DistanceToPlayerY(int curY, int PlayerY,Monsters *pBadGuy);
                 void MoveTowardsPlayer(int curX, int curY,Monsters *pBadGuy);
                 private:
                 Monsters * pBadGuy;
    };
    int Monsters::DistanceToPlayerX(int curX,int PlayerX){
         *pBadGuy->GetX() = curX;
         ASCIIMan.GetX() = PlayerX;
         return (X+PlayerX);
         };
    
    int Monsters::DistanceToPlayerY(int curY,int PlayerY,Monsters *pBadGuy){
         Y = curY;
         ASCIIMan.GetY() = PlayerY;
         return (Y+PlayerY);
         
    void Monsters::MoveTowardsPlayer(int curX, int curY, Monsters *pBadGuy){\
    //movement code
    };
    
    void Monsters::MonsterAI(){
          if (DistanceToPlayer() < 1){
                               MoveTowardsPlayer();
                               };
          };
    #endif
    //ignore distancetoplayerY, im waiting till i have a working distancetoplayerX and then just changing what i need.

    does anybody have any tips on what i should do with this code to help debug it and properly design it?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    DistanceToPlayerX is missing pBadGuy parameter.
    DistanceToPlayerY is never using pBadGuy parameter.
    MoveTowardsPlayer has a syntax error with that "\" at the end.
    And remember to use const on all functions that does not modify any class data. And add const to any and all references which you are not changing.
    Those are a few.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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