static members help

J

Jonathan Bartlett

class temp
{
public:
void init();
void setArray();
void print();
private:
static char array[3][3];
int row , col;
};

Declaring a static class variable is a lot like declaring an "extern" in
C. Not only do you need the extern, you also need the actual definition
in EXACTLY ONE source file. Therefore, somewhere you need to put:

static char temp::array[3][3];

Jon
 
R

reddy

hi I am getting this error

/tmp/ccYZ9ROC.o(.text+0x139): In function `temp::init()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x157): In function `temp::setArray()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x19a): In function `temp::print()':
: undefined reference to `temp::array'
collect2: ld returned 1 exit status


here is my code:

#include <iostream>
using namespace std;

class temp
{
public:
void init();
void setArray();
void print();
private:
static char array[3][3];
int row , col;
};

void temp::init()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

temp::array[row][col] = ' ';
}
}
}

void temp::setArray()
{
temp::array[0][0] = 'P';
}

void temp::print()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

cout << array[row][col]<<endl;
}
}

}


int main()
{
temp t;
t.init();
t.setArray();
t.print();
}

thank in advance.
 
V

Victor Bazarov

reddy said:
hi I am getting this error

/tmp/ccYZ9ROC.o(.text+0x139): In function `temp::init()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x157): In function `temp::setArray()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x19a): In function `temp::print()':
: undefined reference to `temp::array'
collect2: ld returned 1 exit status


here is my code:

#include <iostream>
using namespace std;

class temp
{
public:
void init();
void setArray();
void print();
private:
static char array[3][3];

This is the declaration. Where is the definition?
int row , col;
};
[...]

V
 
V

Victor Bazarov

reddy said:
hi, i have defined it in init right..

but how to make this program run...

I guess you don't understand the terms "declaration", "definition",
and the difference between them. Read your book again, the section
about static data members. BTW, what book are you reading on C++?

V
 
L

Larry I Smith

reddy said:
hi I am getting this error

/tmp/ccYZ9ROC.o(.text+0x139): In function `temp::init()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x157): In function `temp::setArray()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x19a): In function `temp::print()':
: undefined reference to `temp::array'
collect2: ld returned 1 exit status


here is my code:

#include <iostream>
using namespace std;

class temp
{
public:
void init();
void setArray();
void print();
private:

// here you declare 'temp::array'
static char array[3][3];
int row , col;
};

// here you define temp::array
char temp::array[3][3];
void temp::init()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

temp::array[row][col] = ' ';
}
}
}

void temp::setArray()
{
temp::array[0][0] = 'P';
}

void temp::print()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

cout << array[row][col]<<endl;
}
}

}


int main()
{
temp t;
t.init();
t.setArray();
t.print();
}

thank in advance.
 
R

reddy

ok, i dont the definition but i am sure u do.. so why dont u just
explain it or even better run the code...
 
P

piinyouri

reddy said:
hi I am getting this error

/tmp/ccYZ9ROC.o(.text+0x139): In function `temp::init()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x157): In function `temp::setArray()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x19a): In function `temp::print()':
: undefined reference to `temp::array'
collect2: ld returned 1 exit status


here is my code:

#include <iostream>
using namespace std;

class temp
{
public:
void init();
void setArray();
void print();
private:
static char array[3][3];
int row , col;
};

void temp::init()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

temp::array[row][col] = ' ';
}
}
}

void temp::setArray()
{
temp::array[0][0] = 'P';
}

void temp::print()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

cout << array[row][col]<<endl;
}
}

}


int main()
{
temp t;
t.init();
t.setArray();
t.print();
}

thank in advance.


I removed the static array declaration and it compiled just fine for
me. Also, they row and col should be declared withen a member function
because looking at the code they are only used in loops.
 
R

reddy

actually i can declare row and col in the loops itself that is not the
problem the problem is to declare multi-dimensional array as static..
and use it.. but this is not working... any ideas why?

thank you.
 
A

Alvin

reddy said:
actually i can declare row and col in the loops itself that is not the
problem the problem is to declare multi-dimensional array as static..
and use it.. but this is not working... any ideas why?

thank you.

You have been given the answer already (at least twice).
 
V

Victor Bazarov

Jonathan said:
class temp
{
public:
void init();
void setArray();
void print();
private:
static char array[3][3];
int row , col;
};


Declaring a static class variable is a lot like declaring an "extern" in
C. Not only do you need the extern, you also need the actual definition
in EXACTLY ONE source file. Therefore, somewhere you need to put:

static char temp::array[3][3];

What's the keyword "static" doing here, Jon?
 
J

Jeffrey Baker

static data cannot be written as a dimensional array.

JB

reddy said:
hi I am getting this error

/tmp/ccYZ9ROC.o(.text+0x139): In function `temp::init()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x157): In function `temp::setArray()':
: undefined reference to `temp::array'
/tmp/ccYZ9ROC.o(.text+0x19a): In function `temp::print()':
: undefined reference to `temp::array'
collect2: ld returned 1 exit status


here is my code:

#include <iostream>
using namespace std;

class temp
{
public:
void init();
void setArray();
void print();
private:
static char array[3][3];
Must be - static char one = 'P';
static char two = 'o';
or give up the static for char array[3][3];
int row , col;
};

void temp::init()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

temp::array[row][col] = ' ';
}
}
}

void temp::setArray()
{
temp::array[0][0] = 'P';
}

void temp::print()
{
for (row = 0; row < 3; row++) {
for (col = 0; col < 3; col++) {

cout << array[row][col]<<endl;
}
}

}


int main()
{
temp t;
t.init();
t.setArray();
t.print();
}

thank in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top