I am trying to use a custom comparator as in the following minimal example:
#include <set>
using namespace std;
struct idComp;
class TestClass
{
public:
int id;
void setId(int i){ id = i; }
int getId(){ return id; }
void test( set<TestClass*, idComp> &s){
//do my stuff
}
void test2(){
set <TestClass*, idComp> s;
}
};
struct idComp
{
bool operator() (TestClass* t1, TestClass* t2) const
{
return t1->getId() < t2->getId();
}
};
int main(int argc, char* argv[])
{
return 0;
}
...but when I try to compile I get the following error relating to the test function:
comp_ref.cpp:12:34: error: ‘idComp’ was not declared in this scope
void test( set<TestClass*, idComp> &s){
^~~~~~
comp_ref.cpp:12:40: error: template argument 2 is invalid
void test( set<TestClass*, idComp> &s){
and this with the addition of test2:
/usr/include/c++/7/bits/stl_tree.h:708:31: error: invalid use of incomplete type ‘struct idComp’
_Rb_tree_impl<_Compare> _M_impl;
Any suggestions of how/where to define idComp so that it is usable by the function test?
Since you have a bit of a circular dependency, you can resolve this by forward-declaring idComp before TestClass:
struct idComp;
class TestClass
{
...
But you can leave the definition of struct idComp where it is.
Related
I am using C++ in native mode with Visual Studio 2017 and I am trying to compile and run the example code found at Debugging a Parallel Application in Visual Studio.
For the record, I program in C not C++. I am clueless when it comes to method declarations (among many other things). I suspect correcting the error is simple but, I simply don't know how.
In other words, I am currently RTFineM. I simply copied and pasted the example given in the url above and ran into 2 problems. First it complained about something being deprecated but a simple define took care of that problem. Second it complained about not being able to convert a type into another as stated in the title.
The RunFunc class causing the problem is declared as follows:
class RunFunc
{
Func& m_Func;
int m_o;
public:
RunFunc(Func func,int o):m_Func(func),m_o(o)
{
};
void operator()()const
{
m_Func(m_o);
};
};
My question/request is: how does the declaration of RunFunc need to be in order for the example to compile and run properly ?
Thank you, much appreciate the help.
In this constructor
RunFunc(Func func,int o):m_Func(func),m_o(o)
{
};
the prameter Func func is adjusted by the compiler to the type Func *func. On the other hand the data member m_Func is declared as a referenced type.
Func& m_Func;
And the error message says about incompatibility of the types.
C2440 cannot convert from 'void (_cdecl*)(int)' to 'void(_cdecl&)(int)
Try to declare the constructor like
RunFunc(Func &func,int o):m_Func(func),m_o(o)
{
};
Or declare the data member like
Func *m_Func;
without changing the constructor.
Here are two demonstrative programs
#include <iostream>
typedef void Func( int );
class RunFunc
{
Func& m_Func;
int m_o;
public:
RunFunc(Func &func,int o):m_Func(func),m_o(o)
{
};
void operator()()const
{
m_Func(m_o);
};
};
int main() {
return 0;
}
and
#include <iostream>
typedef void Func( int );
class RunFunc
{
Func *m_Func;
int m_o;
public:
RunFunc(Func func,int o):m_Func(func),m_o(o)
{
};
void operator()()const
{
m_Func(m_o);
};
};
int main() {
return 0;
}
In your code you are tyring to bound a reference to a temporary, namely to copy of argument passed to the constructor. You can try to run the following code snippet to see the difference:
struct Func {
int _i;
void operator()(int i) { cout << i*_i << endl; }
};
class RunFunc
{
Func& m_Func;
int m_o;
public:
RunFunc(Func &func, int o) :m_Func(func), m_o(o)
// RunFunc(Func func, int o) :m_Func(func), m_o(o)
{
};
void operator()()const
{
m_Func(m_o);
};
};
int main() {
Func f{ 5 };
RunFunc rf(f, 2);
rf();
return 0;
}
This is a legacy approach. You can use standard library functor and binder instead. For example:
#include <functional>
#include <iostream>
static void my_callback(int i) {
std::cout<< i << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::function<void()> functor;
functor = std::bind(my_callback, 1);
functor();
return 0;
}
I have some very simple source code to expose a simple Foo class.
main.cpp:
#include <iostream>
#include <lua.hpp>
#include <LuaBridge.h>
class Foo
{
private:
int number = 0;
public:
void setNumber(const int& newNumber) {number = newNumber;}
int getNumber() {return number;}
};
int main()
{
//Expose the API:
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luabridge::getGlobalNamespace(L)
.beginClass<Foo>("Foo")
.addConstructor<void(*)(void)>()
.addProperty("number", &Foo::getNumber, &Foo::setNumber)
.endClass();
}
Unfortunately, I get this error:
24 error: no matching function for call to ‘luabridge::Namespace::Class<Foo>::addProperty(const char [7], int (Foo::*)(), void (Foo::*)(const int&))’
I don't know what the problem is, but I have to use addProperty otherwise the code doesn't look correct
The template for addProperty:
template <class TG, class TS>
Class <T>& addProperty (char const* name, TG (T::* get) () const, void (T::* set) (TS))
requires that the getter is a const member function.
Changing the getter to:
int getNumber() const { return number; }
removes the error in LuaBridge 2.0
I have following code:
template<class T>
class TemplateA : public virtual std::list<T>
{
protected:
unsigned int iSize;
public:
unsigned int getSize();
};
/////////////
template<class T>
unsigned int TemplateA<T>::getSize()
{
return iSize;
}
/////////////
/////////////
/////////////
template<class T>
class TemplateB : public TemplateA<T>
{
public:
unsigned int calcSize();
};
/////////////
template<class C>
unsigned int TemplateB<C>::calcSize()
{
iSize = C.getSize;
return iSize;
}
/////////////
/////////////
/////////////
// Class C (seperate file) has to contain function getSize()
class CMyClass
{
public:
static const unsigned int getSize = 5;
};
This means, within class TemplateB I want to call the getSize method, which the passed class have defined.
I receive the following error message:
error C2275: 'C' : illegal use of this type as an expression
while compiling class template member function 'unsigned int TemplateB<C>::calcSize()'
1> with
1> [
1> C=CMyClass
1> ]
I'm quiet sure that this function worked under VS 2003... What's wrong with the method? Maybe a compiler setting? I don't know where to set what :(
You should say this->getSize or C::getSize; this will defer lookup to the second phase when the template arguments are known.
Hi you could simplify your code too whilst correcting, all you seem to have done is use C rather than TemplateB so if you do:
template<class C>
unsigned int TemplateB<C>::calcSize()
{
return c::getSize; //based on getSize being static
}
You will save the memory of an extra variable and it should work fine:)
Addendum:
Here is a working code snippet using your code as a basis:
#include <iostream>
#include <list>
using namespace std;
template<class T>
class TemplateA : public virtual std::list<T>
{
protected:
unsigned int iSize;
public:
unsigned int getSize();
};
template<class T>
unsigned int TemplateA<T>::getSize()
{
return iSize;
}
template<class T>
class TemplateB : public TemplateA<T>
{
public:
unsigned int calcSize();
};
template<class C>
unsigned int TemplateB<C>::calcSize()
{
return C::getSize;
}
// Class C (seperate file) has to contain function getSize()
class CMyClass
{
public:
static const unsigned int getSize = 5;
};
int main()
{
CMyClass classme;
TemplateB<CMyClass> test ;
cout <<"Calc size outputs: "<< test.calcSize() << endl;
return 0;
}
Apologies for the unchecked answer previously.
Hope this one helps!
I am trying to create a generic function map using templates.The idea is to inherit from this generic templated class with a specific function pointer type. I can register a function in the global workspace, but I'd rather collect all the functions together in the derived class and register these in the constructor. I think I am almost here but I get a compile error. Here is a stripped down version of my code:
#include <iostream>
#include <string>
#include <map>
#include <cassert>
using namespace std;
int f(int x) { return 2 * x; }
int g(int x) { return -3 * x; }
typedef int (*F)(int);
// function factory
template <typename T>
class FunctionMap {
public:
void registerFunction(string name, T fp) {
FunMap[name] = fp;
}
T getFunction(string name) {
assert(FunMap.find(name) != FunMap.end());
return FunMap[name];
}
private:
map<string, T> FunMap;
};
// specific to integer functions
class IntFunctionMap : public FunctionMap<F> {
public:
int f2(int x) { return 2 * x; }
int g2(int x) { return -3 * x; }
IntFunctionMap() {
registerFunction("f", f); // This works
registerFunction("f2", f2); // This does not
}
};
int main()
{
FunctionMap<F> fmap; // using the base template class directly works
fmap.registerFunction("f", f);
F fun = fmap.getFunction("f");
cout << fun(10) << endl;
return 0;
}
The error I get is:
templatefunctions.cpp: In constructor ‘IntFunctionMap::IntFunctionMap()’:
templatefunctions.cpp:33: error: no matching function for call to ‘IntFunctionMap::registerFunction(const char [3], <unresolved overloaded function type>)’
templatefunctions.cpp:15: note: candidates are: void FunctionMap<T>::registerFunction(std::string, T) [with T = int (*)(int)]
Juan's answer is correct: member functions have an implicit first parameter, which is a pointer to the type of which they are a member. The reason your code fails to compile is that your map supports function pointers with type int (*)(int), but the type of f2 is int (IntFunctionMap::*)(int).
In the specific case that you show here, you can use std::function, which implements types erasure, to present free functions and member functions as the same type. Then you could do what you are trying to do. Note: this requires C++11.
#include <iostream>
#include <string>
#include <map>
#include <cassert>
#include <function>
#include <bind>
using namespace std;
int f(int x) { return 2 * x; }
int g(int x) { return -3 * x; }
typedef std::function<int (int)> F;
// function factory
template <typename T>
class FunctionMap {
public:
void registerFunction(string name, T fp) {
FunMap[name] = fp;
}
T getFunction(string name) {
assert(FunMap.find(name) != FunMap.end());
return FunMap[name];
}
private:
map<string, T> FunMap;
};
// specific to integer functions
class IntFunctionMap : public FunctionMap<F> {
public:
int f2(int x) { return 2 * x; }
int g2(int x) { return -3 * x; }
IntFunctionMap() {
registerFunction("f", f); // This works
registerFunction("f2", std::bind(&f2, this, _1)); // This should work, too!
}
};
int main()
{
FunctionMap<F> fmap; // using the base template class directly works
fmap.registerFunction("f", f);
F fun = fmap.getFunction("f");
cout << fun(10) << endl;
return 0;
}
#include "stdafx.h"
class Person;
typedef void (Person::*PPMF)();
// error C2159: more than one storage class specified
typedef static void (Person::*PPMF2)();
class Person
{
public:
static PPMF verificationFUnction()
{
return &Person::verifyAddress;
}
// error C2440: 'return' : cannot convert from
// 'void (__cdecl *)(void)' to 'PPMF2'
PPMF2 verificationFUnction2()
{
return &Person::verifyAddress2;
}
private:
void verifyAddress() {}
static void verifyAddress2() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
Person scott;
PPMF pmf = scott.verificationFUnction();
(scott.*pmf)();
return 0;
}
Question: I need to define a function pointer PPMF2 to pointing to a static member function verifyAddress2. How can I do it?
#include "stdafx.h"
class Person;
typedef void (Person::*PPMF)();
typedef void (Person::*PPMF2)();
class Person
{
public:
static PPMF verificationFUnction()
{
return &Person::verifyAddress;
}
PPMF2 verificationFUnction2()
{
return &Person::verifyAddress2;
}
private:
void verifyAddress() {}
static void verifyAddress2() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
Person scott;
PPMF pmf = scott.verificationFUnction();
(scott.*pmf)();
return 0;
}
A pointer to a static member function is just a normal function pointer. typedef void (*PPMF2)(). You assign it to a static member function like you assign any function pointer, only that the static member function is inside the class scope:
PPMF2 myfunc = &MyClass::StaticMemberFunc;
About static member function guarantees:
С++ ISO/IEC 14882 2003-10-15 says that
5.2.2 There are two kinds of function call: ordinary function call and member function 57) (9.3) call....
57) A static member function (9.4) is an ordinary function.
Theoretically static-member-functions can have another calling convention.
But standart allow us to leverage on such thing...
Answer:
typedef void (Person::*PPMF2)() => typedef void (*PPMF2)()
If the function is static it does not require a (implicit) this pointer to be invoked. Therefore, a pointer to a static member function is not the same as a member function pointer:
#include "stdafx.h"
class Person;
typedef void (Person::*PPMF)();
typedef /*static*/ void (*PPMF2)();
class Person
{
public:
static PPMF verificationFUnction()
{
return &Person::verifyAddress;
}
PPMF2 verificationFUnction2()
{
return &Person::verifyAddress2;
}
private:
void verifyAddress() {}
static void verifyAddress2() {}
};
int _tmain(int argc, _TCHAR* argv[])
{
Person scott;
PPMF pmf = scott.verificationFUnction();
(*pmf)();
return 0;
}
EDIT:
removed the offending static from the typedef.
#include<iostream>
using namespace std;
class A
{
private:
int x,y;
static int a;
public:
A()
{
x = 10;
y = 11;
}
~A()
{
}
void displayNonStatic()
{
cout<<x<<" "<<y<<endl;
}
void displayStatic()
{
cout<<a<<endl;
}
};
int A::a = 12;
int main()
{
typedef void (A::*NonStatic)(void);
typedef void (A::*Static)(void);
A a1;
NonStatic _nstatic = &A::displayNonStatic ;
Static _static = &A::displayStatic;
// Always make sure that call to the pointer to the member functions is made within the context of the instance.
//Correct way to call the pointer within the context of the instance " a1 " .
(a1.*_nstatic)();
(a1.*_static)();
//Error case given below, the pointer is not called within the context of the instance
// (*_nstatic)(); ->error
// (*_static)(); ->error
getchar();
}
Refer to the link for more information.