473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

static array as a class member

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

ld:
Unresolved:
myClass::myArray

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

***********************
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class myClass{
  5. private:
  6.         int a;
  7.         static int myArray[10];
  8. public:
  9.         myClass();
  10.         void display();
  11. };
  12.  
  13. int main()
  14. {
  15.         myClass A;
  16.         A.display();
  17.  
  18.         return 0;
  19. }
  20.  
  21.  
  22. myClass::myClass()
  23. {
  24.                 for(int i=0; i<10; i++)
  25.                         myArray[i]=i;
  26. }
  27.  
  28. void myClass::display()
  29. {
  30.         for(int i=0; i<10; i++)
  31.                 cout<<myArray[i]<<" ";
  32.         cout<<endl;
  33. }
  34.  
  35. *****************************
May 4 '07 #1
3 1644
svlsr2000
181 Expert 100+
what is wrong with the code below,
when i compile i get the following error:

ld:
Unresolved:
myClass::myArray

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::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;
}

*****************************
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;
May 4 '07 #2
stmfc
65
but when i declare int a, as static int a
in the class declaration, there is not any problem
May 4 '07 #3
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.
May 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rahul Joshi | last post by:
Hi, I want to create a static array whose size is also a const member of the class. Something like: // A.h class A { private: static int size = 0; static int array;
1
by: Dave | last post by:
In Exceptional C++, the following code snippet appears at the bottom of page 197: template<class T> class Array : private ArrayBase, public Container { typedef Array AIType; public: Array(...
7
by: Eric | last post by:
My program will create a few instance of class A. Class A will always need this 256byte array that is used for bitcounting. I have a function that fills the array. How do a right a class or...
2
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
6
by: laniik | last post by:
hi i have a question. i have a class class c { static int vector; c(); void foo(); }; I want to initialize the values in that vector so that in every instance of c, foo can use it. how...
5
by: partha.p.das | last post by:
Here is my class: class MyClass { public: MyClass(); ~MyClass(); private:
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.