How I can access a private static member variable via the class constructor?

user3578367

How can I use and modify the s_member private static variable from the constructor or in general from an other member function?

This is what I tried.

a.h:

#ifndef A_H
#define A_H

#include <set>

class A
{
    public:
        A();
        virtual ~A();

    private:
        static std::set<int> s_member;
};

#endif

a.cpp:

#include "a.h"

A::A()
{
    A::s_member.insert(5); // It causes error.
}

I get this error:

/tmp/ccBmNUGs.o: In function `A::A()': a.cpp:(.text+0x15): undefined
 reference to `A::s_member' collect2: error: ld returned 1 exit status
masoud

You have declared A::s_member but not defined it yet. To define it, put below code, outside of class:

std::set<int> A::s_member;

For example:

std::set<int> A::s_member;

A::A()
{
  // ...
}

The problem is not related to accessing and private/public.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access private static variable in static member function of another class?

From Dev

How do I test this static member in a class with private constructor?

From Dev

How can I access public member of private package class?

From Dev

How can I access public member of private package class?

From Dev

How can I access a member of a class from a function via pointer?

From Dev

How to access private static class member inherited from a template class?

From Dev

How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

From Dev

In C++ how do I access a private base member variable in an inherited class?

From Dev

How a friend class can access a private member of a nested class?

From Dev

can we access static member variable of pure virtual class?

From Dev

How can I access a private class' atributes from a function that the constructor is taking as parameter?

From Dev

access private member variable through private member, same class

From Dev

How do i get a class member function to have access to another class member function's private member?

From Dev

How do I access a private constructor in a separate class?

From Dev

How does static inner class can access all the static data members and static member function of outer class?

From Dev

How to refering a static variable in a class static member?

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

How can I test a final class with private constructor?

From Dev

How can I create object of class whose constructor is defined as private

From Dev

How can I use iQueryable in a class with a private constructor?

From Dev

How can I tell which constructor to use on an inner (member) class?

From Dev

Static nested class has access to private constructor of outer class

From Dev

Static nested class has access to private constructor of outer class

From Dev

How can I use a private member variable in a non-member function, when the variable happens to be a pointer?

From Dev

How do I access a private variable from a class?

From Java

How can I assign variables to my class constructor if they are static?

From Dev

Can Abstract class have constructor and private member in C++11

From Dev

How can I access parent class variable?

From Dev

How can a class method access a private member of another instance of the same class?

Related Related

  1. 1

    How to access private static variable in static member function of another class?

  2. 2

    How do I test this static member in a class with private constructor?

  3. 3

    How can I access public member of private package class?

  4. 4

    How can I access public member of private package class?

  5. 5

    How can I access a member of a class from a function via pointer?

  6. 6

    How to access private static class member inherited from a template class?

  7. 7

    How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

  8. 8

    In C++ how do I access a private base member variable in an inherited class?

  9. 9

    How a friend class can access a private member of a nested class?

  10. 10

    can we access static member variable of pure virtual class?

  11. 11

    How can I access a private class' atributes from a function that the constructor is taking as parameter?

  12. 12

    access private member variable through private member, same class

  13. 13

    How do i get a class member function to have access to another class member function's private member?

  14. 14

    How do I access a private constructor in a separate class?

  15. 15

    How does static inner class can access all the static data members and static member function of outer class?

  16. 16

    How to refering a static variable in a class static member?

  17. 17

    C++ public method inherited from base class can not access private member variable in derived class

  18. 18

    How can I test a final class with private constructor?

  19. 19

    How can I create object of class whose constructor is defined as private

  20. 20

    How can I use iQueryable in a class with a private constructor?

  21. 21

    How can I tell which constructor to use on an inner (member) class?

  22. 22

    Static nested class has access to private constructor of outer class

  23. 23

    Static nested class has access to private constructor of outer class

  24. 24

    How can I use a private member variable in a non-member function, when the variable happens to be a pointer?

  25. 25

    How do I access a private variable from a class?

  26. 26

    How can I assign variables to my class constructor if they are static?

  27. 27

    Can Abstract class have constructor and private member in C++11

  28. 28

    How can I access parent class variable?

  29. 29

    How can a class method access a private member of another instance of the same class?

HotTag

Archive