Static variable initalization

M

mrsinger

I have a multi-threaded application (non posix) that needs to put a
semaphore around the constructor/destructor of a class. So, I have a
static variable which is the semaphore. Now, I have to make an OS call
to init the variable before any constructor for the class is called.
The code below give me a link error:

unresolved external symbol "private: static int test::sem"

I've been searching through old posts, but can't find anything anything
close. What is the correct way to do this?

---------------------


void OS_CreateSem(int *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;

public:
static void makesem()
{
OS_CreateSem(&sem);
}

test()
{
//take semaphore
//do construction stuff
//release semaphore
}
};



void main()
{
test::makesem();
class test()
}
 
V

Victor Bazarov

I have a multi-threaded application (non posix) that needs to put a
semaphore around the constructor/destructor of a class. So, I have a
static variable which is the semaphore. Now, I have to make an OS
call to init the variable before any constructor for the class is
called. The code below give me a link error:

unresolved external symbol "private: static int test::sem"

I've been searching through old posts, but can't find anything
anything close. What is the correct way to do this?

---------------------


void OS_CreateSem(int *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;

You declared it here. Where is it defined?
public:
static void makesem()
{
OS_CreateSem(&sem);
}

test()
{
//take semaphore
//do construction stuff
//release semaphore
}
};



void main()

There is no 'void main' in C++.
{
test::makesem();
class test()
}

V
 
J

Joseph Paterson

I have a multi-threaded application (non posix) that needs to put a
semaphore around the constructor/destructor of a class. So, I have a
static variable which is the semaphore. Now, I have to make an OS call
to init the variable before any constructor for the class is called.
The code below give me a link error:

unresolved external symbol "private: static int test::sem"

I've been searching through old posts, but can't find anything anything
close. What is the correct way to do this?

---------------------


void OS_CreateSem(int *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;

public:
static void makesem()
{
OS_CreateSem(&sem);
}

test()
{
//take semaphore
//do construction stuff
//release semaphore
}
};



void main()
{
test::makesem();
class test()
}

You need a

int test::sem = 0;

in there somewhere.

When you add the static keyword to a member variable, it doesn't
actually allocate memory for it, it just declares it's existence. By
adding the line above, you allocate memory for it.
Don't include that line in a header file, as it should only appear
once.

Joseph.
 
S

Salt_Peter

I have a multi-threaded application (non posix) that needs to put a
semaphore around the constructor/destructor of a class. So, I have a
static variable which is the semaphore. Now, I have to make an OS call
to init the variable before any constructor for the class is called.
The code below give me a link error:

unresolved external symbol "private: static int test::sem"

Read the pertinent sections about defining static member variables in:
http://www.parashift.com/c++-faq-lite/ctors.html
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top