Error while writing static function? - c++

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.

Related

error: 'noexcept' does not name a type

Seems like it never happened to anyone so I guess I'm doing something wrong...
The error I get is:
error: expected ';' at end of member declaration
error: 'noexcept' does not name a type
the code is this:
void* operator new (size_t size) noexcept
{return foo(size);}
also the code is in macro actually, if it matters.
Thanks!

Can functions accept static function pointers as arguments?

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);

Having problem passing member function pointers to templatized member function on gcc

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).

Explain GCC error after using an object accidentally declared as a function

The following is a common typo with language newcomers, who think that they are defining an object but are actually declaring a function:
struct T
{
void foo() {}
};
int main()
{
T obj();
obj.foo();
}
GCC 4.1.2's error is:
In function 'int main()':
Line 9: error: request for member 'foo' in 'obj', which is of non-class type 'T ()()'
compilation terminated due to -Wfatal-errors.
Why is the reported type in the message T ()()? I'd have expected T ().
IIRC this is just a compiler bug. GCC 4.4 says T() while 4.2 says T()() for me.
The error is best understood when you realize that you usually don't write out function types without naming at least the function, but it's a bit more common for function pointers.
For instance, int (*fooPtr)() names the pointer. If you omit the name, you have int (*)(). Now, going from function pointer to function type would give you int ()().
There's no real standard here, because ISO C++ doesn't define canonical names for all types. For instance, const volatile int is the same type as volatile const int, and neither form is canonical.

ISO C++ forbids declaration Error

I am getting a very strange compile error pointing to where I declare a pointer to my class.
MyClass* myClass; //Line 34
Error:
MyFile.h|34|error: ISO C++ forbids declaration of ‘MyClass’ with no type
MyFilee.h|34|error: expected ‘;’ before ‘*’ token
I could not get a clue what is going wrong. Could anyone throw some light.
Seems to be declaration of MyClass is not visible at that point. Compiler considers MyClass as a new variable's name without type specified.