I'm having problem passing member function pointers to templatized member function on gcc. Does anyone know how to modify the code below to get gcc to accept what I am trying to do?
class Foo
{
public:
template <class C, class R>
void Execute(R(typename C::*memFn)())
{
}
};
I get the following errors when trying to compile the code:
test.cpp:40: error: 'memFn' was not declared in this scope
test.cpp:40: error: expected primary-expression before '(' token
test.cpp:40: error: expected identifier before '*' token
test.cpp:40: error: expected '(' before '*' token
test.cpp:40: error: 'memFn' was not declared in this scope
test.cpp:40: error: variable or field 'Execute' declared void
The version of gcc that I am using is 4.4.2.
Thank you very much for your help!
You don't need typename. Remove that and it should work. (I tested it on gcc 4.3.2).
Related
I searched this question, but am unable to figure out how to resolve this:
class DtEffect;
template <typename VertexFormat>
class DtEffectRenderer : public DtFormatRenderer<VertexFormat>
{
public:
template <typename MemberType>
static DtEffect::VertexAttribPtrInfo VertexAttrib(const MemberType VertexFormat::* member)
{
return DtEffect::VertexAttribPtrInfo(
reinterpret_cast<const GLvoid*>(offsetof(VertexFormat, *member))
, DtAttributeType<MemberType>::value
, DtAttributeType<MemberType>::size);
}
protected:
DtEffect* myEffect;
};
Error messages:
../../include/vrvGraphics/DtEffectRenderer.h: In static member function ‘static makVrv::DtEffect::VertexAttribPtrInfo makVrv::DtEffectRenderer<VertexFormat>::VertexAttrib(const MemberType VertexFormat::*)’:
../../include/vrvGraphics/DtEffectRenderer.h:115: error: expected primary-expression before ‘(’ token
../../include/vrvGraphics/DtEffectRenderer.h:116: error: expected unqualified-id before ‘*’ token
../../include/vrvGraphics/DtEffectRenderer.h:116: error: expected ‘)’ before ‘*’ token
Any ideas?
It appears that you are trying to use offsetof macro to get an offset to a member identified through a pointer-to-member:
offsetof(VertexFormat, *member)
This is not going to work, because the second parameter of the offsetof macro must be member's name, not any kind of an expression that could be used to access the member. Compile error is decidedly cryptic, but there is little the compiler could do, because offsetof is a macro.
See 0xbadf00d's answer to this Q&A for information on finding member offset using pointer-to-member. His approach closely replicates the inner workings of the offsetof macro, but he uses a pointer to member instead of member's name.
Looks like you have a missing '(' after VertexAttribPtrInfo. I added it back in below, try that to see if it works.
template <typename MemberType>
static DtEffect::VertexAttribPtrInfo VertexAttrib(const MemberType VertexFormat::* member)
{
return DtEffect::VertexAttribPtrInfo((
reinterpret_cast<const GLvoid*>(offsetof(VertexFormat, *member))
, DtAttributeType<MemberType>::value
, DtAttributeType<MemberType>::size);
}
While upgrading OpenSSL from .97e to 1.0.2d I'm facing these errors,
BasicAuthCertificateLoader.cpp: At global scope:
BasicAuthCertificateLoader.cpp:493: error:
X509*CBasicAuthCertificateLoader::GetCert' is not a static member of
class CBasicAuthCertificateLoader'
BasicAuthCertificateLoader.cpp:493: error: LHASH' was not declared in
this scope BasicAuthCertificateLoader.cpp:493: error:conf' was not
declared in this scope BasicAuthCertificateLoader.cpp:493: error:
expected primary-expression before '' token
BasicAuthCertificateLoader.cpp:493: error: req' was not declared in
this scope BasicAuthCertificateLoader.cpp:493: error: expected
primary-expression before '*' token
BasicAuthCertificateLoader.cpp:493: error:issuer' was not declared
in this scope BasicAuthCertificateLoader.cpp:493: error: expected
primary-expression before '' token
BasicAuthCertificateLoader.cpp:493: error: priv_key' was not declared
in this scope BasicAuthCertificateLoader.cpp:493: error: expected
primary-expression before "days" BasicAuthCertificateLoader.cpp:494:
error: initializer expression list treated as compound expression
BasicAuthCertificateLoader.cpp:494: error: expected,' or ;' before
'{' token BasicAuthCertificateLoader.cpp: In member functionHRESULT
CBasicAuthCertificateLoader::GenerateCertificateData(char*, X509**,
EVP_PKEY**, PKCS12**)': BasicAuthCertificateLoader.cpp:651: error:
LHASH' was not declared in this scope
BasicAuthCertificateLoader.cpp:651: error:conf' was not declared in
this scope BasicAuthCertificateLoader.cpp:674: warning: cast from
const char*' tovoid*' discards qualifiers from pointer target type
BasicAuthCertificateLoader.cpp:691: error: GetCert' was not declared
in this scope BasicAuthCertificateLoader.cpp:705: error:GetCert' was
not declared in this scope
Has any one faced similar errors?
error: `LHASH' was not declared in this scope...
Ensure the source file in question includes lhash.h:
#include <openssl/lhash.h>
Also see the lhash(3) man page.
If you are already including lhash.h, then your project is probably not set up correctly.
You should clear this error first since its the first one listed. These errors have a way of chaining themselves:
At global scope: BasicAuthCertificateLoader.cpp:493:
error: X509* CBasicAuthCertificateLoader::GetCert' is not a static member
of class CBasicAuthCertificateLoader'
Here's a good example: I'm trying to overload OpenGL's glutMouseFunc so it may accept the namespace, and class function of my choosing. The one in particular is Init::DisplayInit::mouse, which is static. The question is, is this possible? If so, how is this achieved?
My Implementation
void glutMouseFunc(void (Init::DisplayInit::*mouse)(int, int, int, int)) {
(*mouse);
}
Errors from Implementation
..\OpenGL_03\/displayinit.h:27: error: variable or field 'glutMouseFunc' declared void
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: expected primary-expression before 'int'
..\OpenGL_03\/displayinit.h:27: error: void value not ignored as it ought to be
Note, I put the declaration of the function in the same file's header file. I also made sure both the declaration and the definition of the function resided outside of the namespace declaration (which wraps most of both files, each). As shown, one of the first errors reads the function as a variable or field (???).
That's not a reasonable way to define glutMouseFunc. It isn't supposed to call the callback immediately, it's supposed to save a pointer for later (when mouse activity occurs).
Call the GLUT-provided version, and pass the address of your function:
#include <GL/glut.h>
glutMouseFunc(&Init::DisplayInit::mouse);
Static member functions are compatible with ordinary function pointers.
The answer to the headline question is "Yes; functions can accept static function pointers as arguments".
You don't specify the namespace or class in the pointer to function argument specification in the function using it:
void glutMouseFunc(void (*mouse)(int, int, int, int)) {
(*mouse)(1, 2, 3, 4);
}
You do specify the namespace or class in the invocation of the function:
glutMouseFunc(Init::DisplayInit::mouse);
Error while writing static function.
static int function_name ( const Reference< data_type>* ptr)
{
}
when i wrote a static functin which takes Reference pointer then ir gives me following error
error: ISO C++ forbids declaration of 'Reference' with no type
error: expected ',' or '...' before '<' token
The compiler is complaining because it doesn't know what a Reference<T> is. Either you've forgotten to #include the header file that it is defined in, or you've forgotten to forward-declare it.
You presumably mean Reference to be a template but the compiler doesn't see it as one.
I have the following member data
vector<State<T>*> activeChildren;
I want to clean-up these pointers in my destructor
StateContainer<T>::~StateContainer() {
vector<State<T>*>::iterator it =
activeChildren.begin();
while(it!=activeChildren.end()) {
State<T>* ptr = *it;
it = activeChildren.erase(it);
delete ptr;
}
}
I get the following error from g++ 4.3.2 on Ubuntu:
./fsm2/StateContainer.cpp: In destructor ‘virtual ervan::StateContainer<T>::~StateContainer()’:
../fsm2/StateContainer.cpp:24: error: expected `;' before ‘it’
../fsm2/StateContainer.cpp:25: error: ‘it’ was not declared in this scope
Can anyone tell me what I've done wrong? I get this error in two more places where I use iterator loops, but not when I use for_each(...)
Looks like typename time again - I think you need:
typename vector<State<T>*>::iterator it = ...
A heuristic for g++ users - when you see this message in template code:
expected `;' before ‘it’
it is a pretty good bet that the thing in front of the 'it' is not being seen by the compiler as a type and so needs a 'typename' added.
It's a parsing issue. In this code, vector<State<T>*>::iterator is a nested dependent type.
Until you know what T is (and T isn't known at parse time), the parser/compiler doesn't realize that iterator is a type (could be a static member variable, for all it knows).
Therefore, you must prefix the definition with typename as a hint to tell the compiler that for all vector<State<T>*>, vector<State<T>*>::iterator is a type name.