I don't think my question is a duplicate of this one.
what I try to do:
template<const char* szFunctionName>
class CReentranceLock
{
public:
CReentranceLock(){}
~CReentranceLock(){}
bool isLocked(){return s_bLock;}
void setLocked(bool b)
{
const bool Result=(bool)InterlockedCompareExchange(
(unsigned long *)&s_bLock, b, !b);
}
private:
static bool s_bLock;
};
template<const char* szFunctionName>
bool CReentranceLock<const char*szFunctionName>::s_bLock=false;
// error C2146: syntax error : missing ',' before identifier 'szFunctionName'
implying that all instances of CReentranceLock would have their own static, relying on the const char address of the function name passed as a parameter of the template.
and that could be used like this:
void CMyObject::InAnyFunction()
{
const char* szFunctionName = __FUNCTION__;
CReentranceLock<szFunctionName> lock; // Edit: <- this doesn't work
if(lock.isLocked()) return;
lock.setLocked(true);
/// business code
lock.setLocked(false);
}
well that's only the theory...
and unfortunately this doesn't compile under visual 2010, at the line where I try to initialize the statics.
error C2146: syntax error : missing ',' before identifier 'szFunctionName'
what am I doing or thinking wrong ?
PS: and I am not dealing with the fact that reentrance is smelling like an awful design (in MFC), I know, I am lost in the middle of it ;-)
Edit: though the answer below is good, and the definition compiles... my use in CMyObject::InAnyFunction() with a const char * as a template-parameter available at compile time seems to be wrong. :-(
bool CReentranceLock<const char*szFunctionName>::s_bLock=false;
This should be:
bool CReentranceLock<szFunctionName>::s_bLock=false;
Just change the line to
bool CReentranceLock<szFunctionName>::s_bLock=false;
Related
I've been trying to fix this error for hours but there is something I'm missing:
I have a structure declared as:
typedef struct {
bool active;
unsigned long bbcount;
char buffer[BUFFSIZE];
std::set<__uint> *bblist;
} per_thread_t;
Later I'm allocating memory for it and setting some variables including the set like this:
per_thread_t *data = (per_thread_t *)malloc(sizeof(per_thread_t));
data->active = false;
data->bblist = new std::set<__uint>();
data->bblist.find(6328);
But I am getting the error error C2228: left of '.find' must have class/struct/union.
What am I doing wrong here?
Thank you
bblist is a pointer type. You need to access it like this:
data->bblist->find(6328);
This question already has answers here:
C/C++ changing the value of a const
(18 answers)
Closed 8 years ago.
I have a struct which is defined in types.h with the following code:
struct data_Variant {
FlightPlanSteeringDataRecord steeringData;
FlightPlanType flightPlan : 8;
MinitoteLegDataType legDataType : 8; // discriminent, either current or amplified
unsigned spare : 16;
union {
// currentLeg =>
CurrentLegDataRecord currentLegData;
// amplifiedLeg =>
AmplifiedLegDataRecord amplifiedLegData;
} u;
};
I am then trying to pass an instance of that struct as a parameter to a function in a C++ source file called dialogue.cpp:
void dialogue::update( const types::data_Variant& perfData){
...
}
I now want to change the value of some of the members of that struct inside this update() function. However, if I try doing this as I usually would, i.e.
perfData.etaValid = true;
I get a compile error which says: "C2166: l-value specifies const object". As I understand, this is because perfData has been declared as a constant variable. Am I correct in thinking this?
Since I didn't write this part of the code, but only want to use it to update the value displayed on the GUI, I don't really want to change the perfData variable by removing the const keyword, in case I break something else. Is there any way to change the value of a variable that has been declared as const?
I have tried declaring the same struct variable in another part of the code, without using the const keyword, to see if I can change the values of some of its members there... i.e. in Interface.cpp, I have added the following code to a function called sendData():
types::data_Variant& perfData;
perfData.steering.etaValid = true;
perfData.steering.ttgValid = true;
However, I now get the following compile errors on these lines:
error C2653: 'types' is not a class or namespace name
error C2065: data_Variant: undeclared identifier
error C2065: 'perfData': undeclared identifier
error C2228: left of '.steering' must have class/ struct/ union
Is there a way of updating the values of this struct? If so, how should I do it, and what am I doing wrong here?
I have added the following function to the dialogue.cpp source file, as suggested in the answer:
void dialogue::setFPTTGandETAValidityTrue(
FlightPlanMinitoteTypes::FlightPlanMinitoteData_Variant& perfData)
{
SESL_FUNCTION_BEGIN(setFPTTGandETAValidityTrue)
perfData.steeringData.fpETAValid = true;
perfData.steeringData.fpTTGValid = true;
SESL_FUNCTION_END()
}
You could add a wrapper for yourself.
void myupdate(dialogue& dia, types::data_Variant& perfData)
{
perfData.etaValid = true;
dia.update(perfData);
}
Then call myupdate() instead of dialogue::update().
You declare
void dialogue::update( const types::data_Variant& perfData){
...
}
that const is a declaration of you saying: "I won't modify the referenced object in this function". If you want to modify it in dialogue::update you have to remove the const keyword. Wrapping is not a solution, in my opinion, makes the code harder to maintain. Also I vote against remove const with const_cast.
The correct solution is to remove const from method declaration if you want to modify the referenced object inside that function.
Folks,
Problem Statement - Does C++ allow a (static) const be limited to a class scope, so I can get rid of #defines that pollute entire namespace?
My observation is NO (in the following DIFFERENT examples), and I'd like to find out why and what's the best alternative. VS generates error C2589: 'const' : illegal token on right side of '::'
EXAMPLE1
// a.h
class A
{
…
..
static const uint_32 myConst = 1234;
};
//b.cpp
include “a.h”
…
B()
{
uint32_t arr[A::myConst]; // C2589! const : illegal token on right side of '::'
}
EXAMPLE 2
// a.h
class A
{
…
..
enum blah
{
...
myConst = 1234,
..
};
};
//b.cpp
include “a.h”
...
B()
{
uint32_t arr[A::myConst]; // C2589! const : illegal token on right side of '::'
}
When you take your macro:
#define CONST 1234
and substitute it for where you use it:
static const int CONST = 1234;
The end result is nonsense:
static const int 1234 = 1234;
In another instance:
Int a1[a::CONST];
This also becomes nonsense:
Int a1[a::1234];
This all begs the question, what are you trying to do?
It looks like you're trying to create a member variable with the same name as your macro, CONST, here:
class A
{
static const int CONST = 1234;
};
However since when this code is compiled the macro has already been defined, the preprocessor changes this by substituting the macro before the compiler itself can get a crack at it. By the time the code is compiled, it looks like this:
class A
{
static const int 1234 = 1234;
};
Best is to just do away with the macro entirely, and then retrofit your code to use proper constants like you're trying to do here. Don't mix and match. At the very least, don't use the same name for the member as you do for the macro.
First of all your class is called A, as in capital A, not a. The class name is used to qualify the constant you are trying to use. So, change your code to use A::CONST. By the way, this is C++ not C# or Java, so there is no such thing as an Int, unless for some bizarre reason you decided to invent your own integer type.
As an aside, using all caps to name constants can collide with macros and is a good way to get into trouble, especially since pre-processing happens first and macros are substituted for all cases of the constants. This can often lead to invalid C++ code with syntax errors that are difficult to understand. That's why you should never use all caps to name constants, since this is a convention most commonly used for macros.
If I may make a guess, it looks like you're trying to use :: the same way you use . in Python.
It looks like you really don't understand what the scope resolution operator does, or how it works.
:: has a very specific, and quite limited usage. Until you understand it better, we're going to have a really hard time helping you.
I get the following base error:
1>c:\program files\microsoft visual studio 10.0\vc\include\utility(163): error C2436: 'second' : member function or nested class in constructor initializer list
As well as a lot of sub-errors there - I have no idea at all where to look or what goes wrong. (I know what functions it is about, but I'm staring myself blind on why it doesn't work)
The header part:
typedef void *DuplicateFn(pTree&, const pTree&);
enum DuplicateTy {
SKIP,
OVERWRITE,
ASK
};
typedef std::map<DuplicateTy, DuplicateFn> DuplicateMapTy;
static const DuplicateMapTy DuplicateFns;
static DuplicateMapTy DuplicateFns_INIT();
detail namespace:
namespace detail {
void OverWriteFn(GMProject::pTree& tOut, const GMProject::pTree& tIn);
void AskFn(GMProject::pTree& tOut, const GMProject::pTree& tIn);
}
The source part:
GMProject::DuplicateMapTy GMProject::DuplicateFns_INIT() {
DuplicateMapTy tmp;
auto p(std::make_pair(GMProject::OVERWRITE, &detail::OverWriteFn));
tmp.insert(p); //offending line
return tmp;
}
const GMProject::DuplicateMapTy GMProject::DuplicateFns(GMProject::DuplicateFns_INIT());
As said I'm staring myself blind on this, why can't I insert that pair into the map? I'm simply inserting a function pointer & an enum?
I might be wrong, but I don't like the line:
auto p(std::make_pair(GMProject::OVERWRITE, &detail::OverWriteFn));
Are you using VS 2010? You can hover the variable name (p) and see which type auto has deduced.
Also, have you tried:
tmp.insert(std::make_pair(GMProject::OVERWRITE, &detail::OverWriteFn));
Or
tmp.insert(std::pair(GMProject::OVERWRITE, &detail::OverWriteFn));
?
I've been looking for the answer to this question but it seems quite difficult to get it, which brings me finally here.
It's a syntax that we have to put the & right before pointer to member function.
For example here.
class Test;
typedef void (Test::*fpop)();
class Test
{
public:
void Op1(){}
};
int main(){
fpop pFunc;
pFunc = &Test::Op1; // we must need the &
return 0;
}
However, when I take a look at ON_COMMAND(or any other messages) in MFC, it seems a bit different from what I think is right.
VS6.0 is okay. It follows the right syntax as you see below.
You can clearly see & before memberFxn.
#define ON_COMMAND(id, memberFxn) \ // VS6.0
{ WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)&memberFxn },
// ON_COMMAND(id, OnFoo) is the same as
// ON_CONTROL(0, id, OnFoo) or ON_BN_CLICKED(0, id, OnFoo)
But in VS2008, it goes a bit weird. There is no & before memberFxn.
#define ON_COMMAND(id, memberFxn) \ // VS2008
{ WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_v, \
static_cast<AFX_PMSG> (memberFxn) },
// ON_COMMAND(id, OnBar) is the same as
// ON_CONTROL(0, id, OnBar) or ON_BN_CLICKED(0, id, OnBar)
Moreover, in spite of the fact that there is no & before memberFxn,
each line below works perfectly.
ON_COMMAND(ID_APP_ABOUT, CSingleApp::OnAppAbout) // &
ON_COMMAND(ID_APP_ABOUT, &CSingleApp::OnAppAbout) // no &
I tried to find why, and I was curious if it could be because of static_cast<> but it turned out that static_cast has nothing to do with it.
So I am wondering why in VS2008 I have 2 choices where I put the & or I don't have to put the &.
The Visual C++ compiler (VS2005 and VS2008) requires an ampersand (&) and the fully qualified name to form a pointer to member as per C++ standard as shown:
class Test
{
public:
void Foo() {}
void Bar()
{
void (Test::*ptr1)() = Foo; // C3867
void (Test::*ptr2)() = &Foo; // C2276
void (Test::*ptr3)() = Test::Foo; // C3867
void (Test::*ptr4)() = &Test::Foo; // OK
}
};
Most likely there are #pragmas in the MFC headers or something that suppresses the errors, for backwards compatibility reasons. Older versions of VC++ were much less conforming than the newer compilers.
The only correct way to form a pointer to member in C++ is with & and the class qualifier (in this case CSingleApp::).
The Visual C++ compiler has always been more relaxed and has allowed things not normally permitted in the language such as leaving of the qualifier when forming the pointer from inside the class' context and not needing to use & when it is strictly required.