954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Access static member function from outside a class

Hi!
I don't understand why I can't use my functions declared like this:

#include "groundArray.h"

int groundArray::getlistX(int i)
{
 return x;
}
int groundArray::getlistY(int i)
{
    int groundlistY[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
    return groundlistY[i];
}
int groundArray::getlistWidth(int i)
{
    int groundlistWidht[] = {40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40};
    return groundlistWidht[i];
}
int groundArray::getlistHeight(int i)
{
    int groundlistHeight[] = {40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40};
    return groundlistHeight[i];
}


With the header file

#ifndef GROUNDARRAY_H

#define GROUNDARRAY_H





class groundArray

{

    public:

        groundArray();

        virtual ~groundArray();

        static int getlistX(int i);

        static int getlistY(int i);

        static int getlistWidth(int i);

        static int getlistHeight(int i);
        const static int x = 89;

    protected:

    private:

};
#include "masterList.h"

#include "groundArray.h"



int* masterList::list(void)

{

    int* pointer;

    int list[size*2];

    pointer=list;

    for(int i = 0; i<size; i+=2)

    {

       list[i] = getlistX(i);

       list[i+1] = getlistY(i);

    }

    return pointer;

}

int masterList::getSize(void)

{

    return size;

}


I get the error message: masterList.cpp|11|error: ‘getlistX’ was not declared in this scope|. Why?

MasterList is supposed to make the coordinates for the ground available as an array withinmain() later.

Allander
Newbie Poster
2 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Because you are working in the source code of the class "masterList"...

the only way "masterList" would be able to access the functions of "groundArray" is by creating an instance of it (groundArray * ground = new groundArray())

and then calling its methods as follows: ground->getlistX(i)

another way would be to inherit "masterList" from "groundArray"

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 

Since you are calling static functions, you will need to supply your class name. Therefore, you need to make calls like groundArray::getlistX, groundArray::getListY, etc.

MonsieurPointer
Junior Poster
125 posts since Jun 2011
Reputation Points: 31
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: