0

I'm trying to use a dynamic array in a class but I'm getting "a non static member must be relative to a specific object" error. Here's the code.

class Calendar
{
    private:
        static int holidayCount;
        int * holidayDates = new int[10];

    public:
        static void addHolidayCount() 
        {
            holidayCount++;
        }

        static int getHolidayCount()
        {
            return holidayCount;
        }

        static void addHolidayDate(int day, int month, int year)
        {
            holidayDates[0] = 1;
        }
};

If I use this same type of setup within my main function I don't get any errors. For example:

int * holidayDates = new int[10];
holidayDates[0] = 1;

I'm guessing I have some kind of syntax error here but I'm not sure what.

4
  • 1
    Static member cannot access non-static member Commented Nov 3, 2017 at 5:25
  • You can't use non static fields (I mean holidayDates ) in static method (addHolidayDate) Commented Nov 3, 2017 at 5:26
  • I see. I should have realized that. And in there lies the dangers of copying and pasting. Thanks. Commented Nov 3, 2017 at 5:28
  • Since you usw c++, avoid the usage of 'new'. The size of your array is fixed. This means you can usw a 'std::array'. Commented Nov 3, 2017 at 7:09

4 Answers 4

1

Your holidayDates member variable is non-static, so there is a separate holidayDates pointer (and array) for each Calendar object that you create.

Your addHolidayDate() method, on the other hand, is marked static, so it runs outside of the context of any particular Calendar object.

That means that if you try to access holidayDates from within addHolidayDate(), the compiler has no way to tell which holidayDates pointer you meant to use; hence the error message.

The solution is either to make holidayDates static as well, or make addHolidayDate() non-static.

Sign up to request clarification or add additional context in comments.

Comments

0

holidayDates is not a static member, you cannot directly access it in a static function.

Comments

0

static member variables as well as functions are independent of all instances of the class. They don't need an instance of the class to be used.

Non-static member variables as well as functions can only be used with an instance of the class.

The problem in your code is in the function addHolidayDate. It is defined as a static member function. Hence, it is not associated with any instance. The member variable holidayDates, on the other hand, is a non-static member variable. It can be used only with an instance of the class.

I suggesting making addHolidayDate a non-static member function.

// Don't use static
void addHolidayDate(int day, int month, int year)
{
   holidayDates[0] = 1;
}

Comments

0

You can't access non static fields in static methods. So remove static for addHolidayDate

class Calendar {
private:
    static int holidayCount;
    int * holidayDates = new int[10];

public:
    void addHolidayCount()
    {
        holidayCount++;
    }

    int getHolidayCount()
    {
        return holidayCount;
    }

    void addHolidayDate(int day, int month, int year)
    {
        holidayDates[0] = 1;
    }
};

Here is example how to use this class:

int main()
{
    Calendar* c = new Calendar();
    c->addHolidayDate(1, 2, 3);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.