473,756 Members | 9,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static variable initalization

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(in t *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;

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

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

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

Dec 4 '06 #1
3 3928
mr******@usa.ne t wrote:
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(in t *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;
You declared it here. Where is it defined?
>
public:
static void makesem()
{
OS_CreateSem(&s em);
}

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

void main()
There is no 'void main' in C++.
{
test::makesem() ;
class test()
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '06 #2

mr******@usa.ne t wrote:
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(in t *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;

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

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.

Dec 4 '06 #3

mr******@usa.ne t wrote:
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
>
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(in t *sem)
{
*sem = 1;
}

class test
{
private:
static int sem;

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

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

void main()
{
test::makesem() ;
class test()
}
Dec 5 '06 #4

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

Similar topics

3
3934
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /* in above case where is variable k and j mapped in memory layout ? */
9
2830
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
7
3154
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my question about local variable initialization is still not solved. And some of the replies forked into talking about out parameters. And the thread is becoming way too deep. So I open a new thread here. My question in the previous thead has turned...
28
4641
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can anyone confirm this. Thanks. -- Dennis in Houston
6
1914
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context. Now, every now and then I get this...
55
6243
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
10
2660
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
17
4449
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized to 0. ANSI C guarantees that. Technically speaking, static variables go into the .bss ELF segment, while "static int foo=0" goes into .data. Because .bss is zero filled by the OS, it does not need to be in the actual binary. So it is in fact...
9
3418
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want to put them in main(), it's so far away. Thanks. -
0
9484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10062
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9881
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8733
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6553
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2694
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.