Menu

Private Static variable problem

2008-09-12
2012-09-26
  • Richard Kopcke

    Richard Kopcke - 2008-09-12

    I am running Dev-C++ 4.9.9.2 on an XP machine.
    In the following C++ code, declaring the static defaultList private in the declaration of class list produces the error shown in the compiler output that appears below the source code.
    Moving this declaration of static defaultList to public in class list allows the program to run. The declaration that appears after the class declaration seems to be the problem. It is accepted when the defaultList is public; it is not, when private.

    What is the recommended fix?

    Thanks!

    Bare bones source code:

    include <cstdlib>

    include <iostream>

    include <cmath>

    using namespace std;

    class list
    {
    public:
    list(const int n=0, const double* const d=NULL);
    ~list() { delete [] data; data = NULL; }

       void writeList() const;
    
       static void setDefault(const int=0, const double* const d=NULL);
    

    private:
    int size;
    double* data;
    static list defaultList;
    };

    list list::defaultList;

    list::list(const int n, const double* const d)
    {
    size = n ? n : list::defaultList.size;
    data = new double [size];
    if (d==NULL)
    {
    for (int i=0; i<size; i++) data[i] = list::defaultList.data[i];
    }
    else
    {
    for (int i=0; i<size; i++) data[i] = d[i];
    }
    }

    void list::setDefault(const int sz, const double* const d)
    {list::defaultList = list(sz, d);}

    void list::writeList() const
    {
    for (int i=0; i<size; i++)
    {
    cout << i << '\t' << data[i] << endl;
    }
    cout << endl;
    }

    int main(int argc, char *argv[])
    {
    int ndefault=2;
    double d[] = {1., 1.};

    list::setDefault(ndefault, d);
    
    cout &lt;&lt; &quot;test the setting of defaultList&quot; &lt;&lt; endl &lt;&lt; endl;
    list::defaultList.writeList();
    
    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
    

    }

    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\test\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\test\Makefile.win" all
    g++.exe -c testStatic.cpp -o testStatic.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    testStatic.cpp: In function int main(int, char**)': testStatic.cpp:23: error:list list::defaultList' is private
    testStatic.cpp:60: error: within this context

    make.exe: *** [testStatic.o] Error 1

    Execution terminated

     
    • cpns

      cpns - 2008-09-13

      The error is exactly what it says it is. In line 60:

      list::defaultList.writeList();

      You are are attempting to access list::defaultList from within main() which is not a member or friend function of list. Making main() an friend of list BTW is not a good idea; you may as well make it public.

      The error messages are intended to be read as one, but are split across lines so that it may reference different source lines. They should be interpreted thus:

      "In the main() function at line 23 `list list::defaultList' is private, so cannot be used as in line 60."

      The better solution is perhaps is to add this member function:

      static void writeDefaultList(){ defaultList.writeList(); }
      

      and then change line 60 to:

      list::writeDefaultList();
      

      Note i have suggested in-lining writeDefaultList. You might prefer:

      static void writeDefaultList() ;

      and then:

      void list::writeDefaultList()
      {
      defaultList.writeList();
      }

      I suggest some caution with this code. The C++ Standard Template Library already defines a symbol std::list, and your global "using namespace std;" declaration may expose it and cause a name clash. Either use more meaningful and application specific names, place the code in your own namespace, or stop declaring "using namespace std;". The first and last of these are good practice in any case.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.