library problem

T

thegreatgonzo

I have a little problem. Read this please:

//library.h
class myClass
{
public:
myClass();
static myClass *instance();
private:
static myClass *self;
};

//library.cpp
#include "library.h"

myClass::myClass()
{
self = this;
//more initialisation stuff
}

myClass *myClass::instance()
{
return self;
}

i compiled this 'library':
g++ -Wall -fPIC -c library.cpp -o library.o
g++ -Wall -shared -o libLibrary.so library.o
without any errors

then: i tried to use my 'library':

// main.cpp
#include "library.h"
int main()
{
myClass test;
// do something usefull with the test class
return 0;
}

that's it! but it won't compile! it gave the error:
g++ -lLibrary main.cpp -o test
libLibrary.so: undefined reference to myClass::self
collect2: ld returned 1 exit status

can anyone tell me what i am doing wrong? this isn't the real library,
that one is much bigger, but this is the part what is producing the error.

Regards,

Leon
 
R

Ron AF Greve

Hi,


thegreatgonzo said:
I have a little problem. Read this please:

//library.h
class myClass
{
public:
myClass();
static myClass *instance();
private:
static myClass *self;
};

//library.cpp
#include "library.h"

myClass::myClass()
{
self = this;
//more initialisation stuff
}

myClass *myClass::instance()
{
return self;
}

i compiled this 'library':
g++ -Wall -fPIC -c library.cpp -o library.o
g++ -Wall -shared -o libLibrary.so library.o
without any errors

then: i tried to use my 'library':

// main.cpp
#include "library.h"
int main()
{
myClass test;
// do something usefull with the test class
return 0;
}

that's it! but it won't compile! it gave the error:
g++ -lLibrary main.cpp -o test
libLibrary.so: undefined reference to myClass::self
collect2: ld returned 1 exit status

Just as it says you specified static myClass *self;

But nowhere in your program you reserverd any storage for it i.e. include

myClass *myClass::self;

somewhere in your code
can anyone tell me what i am doing wrong? this isn't the real library,
that one is much bigger, but this is the part what is producing the error.

Regards,

Leon

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
S

Salt_Peter

I have a little problem. Read this please:

//library.h
class myClass
{
public:
myClass();
static myClass *instance();
private:
static myClass *self;

};

//library.cpp
#include "library.h"

myClass::myClass()
{
self = this;
//more initialisation stuff

}

myClass *myClass::instance()
{
return self;

}

i compiled this 'library':
g++ -Wall -fPIC -c library.cpp -o library.o
g++ -Wall -shared -o libLibrary.so library.o
without any errors

then: i tried to use my 'library':

// main.cpp
#include "library.h"
int main()
{
myClass test;
// do something usefull with the test class
return 0;

}

that's it! but it won't compile! it gave the error:
g++ -lLibrary main.cpp -o test
libLibrary.so: undefined reference to myClass::self
collect2: ld returned 1 exit status

can anyone tell me what i am doing wrong? this isn't the real library,
that one is much bigger, but this is the part what is producing the error.

Regards,

Leon

Thats fine, you did exactly what you needed to do, write a short
program that shows the problem at hand - well done!

The error 'undefined reference to myClass::self' is perfectly clear.
There was no instance found of myClass::self.

a) A pointer points to nothing at all unless initialized with a valid
object's address
b) A pointer does not invoke construction

So: how does one define a static variable?
 
T

thegreatgonzo

Thats fine, you did exactly what you needed to do, write a short
program that shows the problem at hand - well done!

The error 'undefined reference to myClass::self' is perfectly clear.
There was no instance found of myClass::self.

a) A pointer points to nothing at all unless initialized with a valid
object's address
b) A pointer does not invoke construction

So: how does one define a static variable?

I thought like a normal variable, but with static in front of it, but it
seems not to be like that.
 
I

Ian Collins

thegreatgonzo said:
I thought like a normal variable, but with static in front of it, but it
seems not to be like that.

What do you expect to happen when you declare a member static? All
instances of the class share the same instance of a static member, so it
has to be defined once and only once in you're application.
 
T

thegreatgonzo

What do you expect to happen when you declare a member static? All
instances of the class share the same instance of a static member, so it
has to be defined once and only once in you're application.

oh ok, now i understand. Thank you!
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top