returning a pointer - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

returning a pointer

dubusdubus Posts: 2Member
i haave a method that is supposed to return a pointer. there are two classes team and player. the method is in team and returns a pointer to a player. i have something writtend but i keep getting definition or declaration errors. any ideas as to how to properly declare such a method?

Comments

  • Griz803Griz803 Posts: 100Member
    : i haave a method that is supposed to return a pointer. there are two classes team and player. the method is in team and returns a pointer to a player. i have something writtend but i keep getting definition or declaration errors. any ideas as to how to properly declare such a method?
    :
    :
    Just at a guess, have you declared the player class after the team class? This will create a declaration problem in team unless you use a forward declaration to remedy the problem. Try to be a bit more specific and/or show some code, it'll help us help you. I hope this helps.
  • dubusdubus Posts: 2Member
    well i have player* findPlayer(int playNum); as my method. i dont want to put in too much code because there is a lot, i have two classes with individual header files for both.

    findPlayer is defined in the header as a public method simply as seen above.
    the code is something like

    team::player* findPlayer(int playNumb){
    //currentPlayer pointer assigned to head
    //while loop to compare currPlayer number to passed number
    //if-else statement to return pointer or continue down linked list
    //final return as NULL if not found
    }
    Im pretty sure it is something about how i declare it, Im just dont know what the right declaration is.
  • Griz803Griz803 Posts: 100Member
    Well, you're still not getting enough code through my thick skull as to what may be your problem. Below is an example of two classes with one that has a method to return a pointer. It is probably a good deal simpler than what you need, but maybe some experimentation with it by you can shine a light for you.
    [CODE]
    #include
    #include

    using namespace std;

    class F_Class{
    private:
    int Stuff;

    public:
    F_Class(){
    Stuff = 0;
    }
    ~F_Class(){// Do Nothing.
    }
    };

    class S_Class{
    private:
    F_Class * Fred;

    public:
    S_Class(){
    Fred = NULL;
    }
    S_Class(F_Class * One){
    Fred = One;
    }
    ~S_Class(){ // Do Nothing.
    }
    F_Class * Get_Data_Ptr(void);
    };

    F_Class * S_Class::Get_Data_Ptr(void)
    {
    return Fred;
    }

    int main(void)
    {
    F_Class First;
    S_Class Second(&First);
    cout << Second.Get_Data_Ptr() << endl;

    system("PAUSE");
    return 0;
    }
    [/CODE]
    I hope this will help you out rather than confuse you more.
  • ash1894ash1894 Posts: 26Member
    are u doing this:

    team::player* findPlayer(int playNumb){
    }

    then I will suggest do this:

    player* team::findPlayer(int playNumb){
    }

    I think u've already got better concept from Griz803's example.

    bye, hope it helps
    --------------->ASH

    : Well, you're still not getting enough code through my thick skull as to what may be your problem. Below is an example of two classes with one that has a method to return a pointer. It is probably a good deal simpler than what you need, but maybe some experimentation with it by you can shine a light for you.
    : [CODE]
    : #include
    : #include
    :
    : using namespace std;
    :
    : class F_Class{
    : private:
    : int Stuff;
    :
    : public:
    : F_Class(){
    : Stuff = 0;
    : }
    : ~F_Class(){// Do Nothing.
    : }
    : };
    :
    : class S_Class{
    : private:
    : F_Class * Fred;
    :
    : public:
    : S_Class(){
    : Fred = NULL;
    : }
    : S_Class(F_Class * One){
    : Fred = One;
    : }
    : ~S_Class(){ // Do Nothing.
    : }
    : F_Class * Get_Data_Ptr(void);
    : };
    :
    : F_Class * S_Class::Get_Data_Ptr(void)
    : {
    : return Fred;
    : }
    :
    : int main(void)
    : {
    : F_Class First;
    : S_Class Second(&First);
    : cout << Second.Get_Data_Ptr() << endl;
    :
    : system("PAUSE");
    : return 0;
    : }
    : [/CODE]
    : I hope this will help you out rather than confuse you more.
    :

Sign In or Register to comment.