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()
} 3 3929 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 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. 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()
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ? */
|
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 ..
|
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...
|
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
|
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...
| |
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
|
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
|
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...
|
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.
-
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |