static array as a class member

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    static array as a class member

    what is wrong with the code below,
    when i compile i get the following error:

    ld:
    Unresolved:
    myClass::myArra y

    but when i dont use the keyword static while declaring the array,
    there is no problem. what is the story about static arrays in class

    *************** ********
    Code:
    #include <iostream>
    using namespace std;
    
    class myClass{
    private:
            int a;
            static int myArray[10];
    public:
            myClass();
            void display();
    };
    
    int main()
    {
            myClass A;
            A.display();
    
            return 0;
    }
    
    
    myClass::myClass()
    {
                    for(int i=0; i<10; i++)
                            myArray[i]=i;
    }
    
    void myClass::display()
    {
            for(int i=0; i<10; i++)
                    cout<<myArray[i]<<" ";
            cout<<endl;
    }
    
    *****************************
    Last edited by sicarie; May 4 '07, 01:43 PM. Reason: Please use [code] and [/code] tags around your code.
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by stmfc
    what is wrong with the code below,
    when i compile i get the following error:

    ld:
    Unresolved:
    myClass::myArra y

    but when i dont use the keyword static while declaring the array,
    there is no problem. what is the story about static arrays in class

    *************** ********

    #include <iostream>
    using namespace std;

    class myClass{
    private:
    int a;
    static int myArray[10];
    public:
    myClass();
    void display();
    };

    int main()
    {
    myClass A;
    A.display();

    return 0;
    }


    myClass::myClas s()
    {
    for(int i=0; i<10; i++)
    myArray[i]=i;
    }

    void myClass::displa y()
    {
    for(int i=0; i<10; i++)
    cout<<myArray[i]<<" ";
    cout<<endl;
    }

    *************** **************
    Storage for static members is not allocated by compiler, its only allocated by linker.
    Please correct me if i am wrong.
    You need to initialize static member variables globally, to have space allocated for it.

    class A{
    public:
    static int i;
    };
    //Needs to initialize i
    int A::i=0;

    Comment

    • stmfc
      New Member
      • May 2007
      • 65

      #3
      but when i declare int a, as static int a
      in the class declaration, there is not any problem

      Comment

      • Prasannaa
        New Member
        • May 2007
        • 21

        #4
        Hi ,

        The reason , why you don't get error
        when you declare "int a;" as "static int a;" is because you are not using it
        anywhere in the code, if u use it then you will get the link error.

        Regards
        Prasannaa.

        Comment

        Working...