473,394 Members | 1,806 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,394 software developers and data experts.

static members help

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.

Jul 23 '05 #1
11 2585
reddy wrote:
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
Jul 23 '05 #2
hi, i have defined it in init right..

but how to make this program run...

thank you.

Jul 23 '05 #3
reddy wrote:
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
Jul 23 '05 #4
reddy wrote:
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.

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #5
ok, i dont the definition but i am sure u do.. so why dont u just
explain it or even better run the code...

Jul 23 '05 #6

reddy wrote:
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.

Jul 23 '05 #7
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.

Jul 23 '05 #8
reddy wrote:
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).

--
Alvin
Jul 23 '05 #9
> 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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 23 '05 #10
Jonathan Bartlett wrote:
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?
Jul 23 '05 #11
static data cannot be written as a dimensional array.

JB

"reddy" <re******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
3
by: SteveM | last post by:
This is probably an easy answer but since I am fairly new to C++ for some reason I can't see it :-( Any help would be appreciated :-) I have a class: class AClass { public:
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
8
by: Rajesh | last post by:
Based on my understanding static members do not have access to non- static members. In the below example static method 'name' accessing non-static method creating reference. Seems to me it is not...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.