When I am trying to compile this code
using namespace std;
namespace asf{
inline int operator|(int);
}
asf::operator|(int x){
return (x>1)?x*operator|(x-1):1;
}
int main(){
cout<<5|;
}
I am getting the following errors
[Error] 'int asf::operator|(int)' must have an argument of class or enumerated type
[Error] ISO C++ forbids declaration of 'operator|' with no type [-fpermissive]
[Error] 'int asf::operator|(int)' should have been declared inside 'asf'
[Error] 'int asf::operator|(int)' must have an argument of class or enumerated type
In function 'int main()':
[Error] expected primary-expression before ';' token
What is wrong? Please help.
As the error says, overloaded operators must have at least one argument of class or enumerated type. That's how the language works.
In addition, you cannot change the arity of an operator when overloading. You're tryning to define a unary |, which is also illegal. | must always take two arguments. The declaration of operator | can include one argument only if it's declared inside a class, in which case the left-hand operand is implicitly of the class's type.
Related
I have a class, MyClass, with an overloaded [] operator that takes an anonymous enum of type MyEnum. I can use it fine in code, but cannot inspect the object using lldb. When I try , I get the following error . .
(lldb) p myObject[MyEnum::value]
error: no viable overloaded operator[] for type 'MyClass'
note: candidate function not viable: no known conversion from 'int' to 'MyEnum' (aka '<anonymous enum>') for 1st argument
Can anyone explain why the debugger will not convert the enum properly?
[xcode 5.1.1]
It appears you need to cast the enumeration:
p myObject[(MyEnum)MyEnum::value]
^^^^^^^^
I have the following class which I need to have two constants upon declaration.
template <int PAGE_DIV_SIZE, int BUFFERS_NUM>
class BufferPool {
//...
}
And
here is a test code for its use
void testBufferPool(const int pageDivSize, const int bufferNum){
// other code and declaration
BufferPool <pageDivSize, bufferNum> bufferPool(catalog, devNum, hostCapacityVec, devCapacityVec);
}
I get the following error:
error: ‘pageDivSize’ is not a constant expression
BufferPoolTest.cpp:26:39: note: in template argument for type ‘int’
BufferPoolTest.cpp:26:39: error: ‘bufferNum’ is not a constant expression
BufferPoolTest.cpp:26:39: note: in template argument for type ‘int’
BufferPoolTest.cpp:26:51: error: invalid type in declaration before ‘(’ token
BufferPoolTest.cpp:26:100: error: expression list treated as compound expression in initializer [-fpermissive]
BufferPoolTest.cpp:26:100: error: cannot convert ‘std::vector<long unsigned int>’ to ‘int’ in initialization
In order to instantiate template, compiler must know all template arguments at compile time. There is no way to figure out the values of pageDivSize and bufferNum at compile time. So template argument should not be a constant variable, but a constant expression.
http://en.cppreference.com/w/cpp/language/constant_expression
I've tried this declaration:
using fp_type = void (*)(fp_type);
Unsurprisingly, it does not work.
a.cpp: In function 'int main()':
a.cpp:13:26: error: expected ';' before '(' token
using fp_type = void(*)(fp_type);
^
a.cpp:13:34: error: expected primary-expression before ')' token
using fp_type = void(*)(fp_type);
Is there a workaround? I was thinking about a reinterpret_cast from a dummy function pointer type, but I am not sure how standard-compliant that is. I am looking into this because I don't want to capture a lambda in a ::std::function, and then use it recursively. I'd like to pass it a function pointer to itself.
5.2.10/6 guarantees you can reinterpret_cast a function pointer to a function pointer of different type, and when you cast it back to the original type, you get back the original pointer value.
I would prefer this:
void foo (struct foo_wrapper&);
struct foo_wrapper {
void (*pf) (struct foo_wrapper&);
// add constructor and accessor for convenience
};
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);
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).