I've searched through Google and stackoverflow and was unable to find something that addresses this situation:
I have a template with a method that takes a function pointer.
The function pointer itself is NOT a template, it's a normal function pointer.
The function pointer, however, has the template args used in its parameters
File name: otherclasses.h
// This class is used as the template parameter
class B
{
public:
B() {}
~B() {}
int getVal() const { return val; }
void setVal(int v) { val = v; }
private:
int val;
};
// This is just a static function
class A
{
public:
static bool someStaticFunction(const B* left, const B* right);
};
inline
bool
A::someStaticFunction(
const B* left,
const B* right)
{
return left->getVal() < right->getVal();
}
File name: templateheader.h
#include "otherclasses.h"
template<typename T>
class theTemplate
{
public:
void insert(T val1, T val2)
{
stuff[0] = val1;
stuff[1] = val2;
}
bool usesSomeStaticFunction(bool (*funcp)(const T, const T))
{
// will return true
return funcp(stuff[0],stuff[1]);
}
private:
T stuff[2];
};
File name: main.cpp
#include "otherclasses.h"
#include "templateheader.h"
#include <stdio.h>
int main()
{
theTemplate<B*> foo;
printf("%d\n", foo.usesSomeStaticFunction(A::someStaticFunction));
return 0;
}
The error from Visual Studio:
error C2664: 'theTemplate<T>::usesSomeStaticFunction' : cannot convert
parameter 1 from 'bool (__cdecl *)(const B *,const B *)' to
'bool (__cdecl *)(const T,const T)'
with
[
T=B *
]
None of the functions with this name in scope match the target type
Two ways around this problem:
Use const void* instead of const T*
Remove const from the parameters of the function pointer and anything that uses it.
Thanks for your help
UPDATE
Turns out there's a better solution - just move the const to the right of B* in the static function:
File name: otherclasses.h EDITED
// This class is used as the template parameter
// This is unchanged.
class B
{
public:
B() {}
~B() {}
int getVal() const { return val; }
void setVal(int v) { val = v; }
private:
int val;
};
// This is just a static function
// This is changed
class A
{
public:
// The "const" is moved to the right side of B*
static bool someStaticFunction(B* const left, B* const right);
};
// This is changed
inline
bool
A::someStaticFunction(
// The function definition must match the function prototype...
B* const left,
B* const right)
{
return left->getVal() < right->getVal();
}
This is because const T becomes T* const, not T const*. One solution is to rework your code to not include the pointer in T (T = B) and include it in the template class instead:
template<typename T>
class theTemplate
{
public:
void insert(T* val1, T* val2)
{
stuff[0] = val1;
stuff[1] = val2;
}
bool usesSomeStaticFunction(bool (*funcp)(const T*, const T*))
{
// will return true
return funcp(stuff[0],stuff[1]);
}
private:
T* stuff[2];
};
Related
I have some problem compiling my code.
I have the following structure:
#include <cstdlib>
using namespace std;
typedef double (*FuncType)(int );
class AnotherClass {
public:
AnotherClass() {};
double funcAnother(int i) {return i*1.0;}
};
class MyClass {
public:
MyClass(AnotherClass & obj) { obj_ = &obj;};
void compute(FuncType foo);
void run();
protected:
AnotherClass * obj_; /*pointer to obj. of another class */
};
void MyClass::compute(FuncType foo)
{
int a=1;
double b;
b= foo(a);
}
void MyClass::run()
{
compute(obj_->funcAnother);
}
/*
*
*/
int main(int argc, char** argv) {
AnotherClass a;
MyClass b(a);
b.run();
return 0;
}
When I try to compile it, it gives:
main.cpp:39:31: error: no matching function for call to ‘MyClass::compute(<unresolved overloaded function type>)’
main.cpp:30:6: note: candidate is: void MyClass::compute(double (*)(int))
What's wrong here?
p/s/ AnotherClass * obj_; should stay like that because I write some function to the big library and can't change it.
-------------- working version by Benjamin -------
#include <cstdlib>
using namespace std;
class AnotherClass {
public:
AnotherClass() {};
double funcAnother(int i) {return i*1.0;}
};
struct Foo
{
/*constructor*/
Foo(AnotherClass & a) : a_(a) {};
double operator()(int i) const
{
return a_.funcAnother(i);
}
AnotherClass & a_;
};
class MyClass {
public:
MyClass(AnotherClass & obj) { obj_ = &obj;};
template<typename FuncType>
void compute(FuncType foo);
void run();
protected:
AnotherClass * obj_; /*pointer to obj. of another class */
};
template<typename FuncType>
void MyClass::compute(FuncType foo)
{
int a=1;
double b;
b= foo(a);
}
void MyClass::run()
{
Foo f(*obj_);
compute(f);
}
/*
*
*/
int main(int argc, char** argv) {
AnotherClass a;
MyClass b(a);
b.run();
return 0;
}
Thank you everybody very much for the help!
Since,
funcAnother(int i);
is a member function it passes an implicit this and then the prototype does not match the type of your function pointer.
The typedef for pointer to member function should be:
typedef double (AnotherClass::*funcPtr)(int);
Here is a modified compilable version of your code. Please check the comments inline to understand the changes, Also I left out the other details, you can add that up.
The following function class will match the signature of your FuncType:
struct Foo
{
AnotherClass & a_;
Foo(AnotherClass & a) a_(a) {}
double operator()(int i) const
{
return a_.funcAnother(i);
}
};
Change MyClass::compute to a template, thusly:
template<typename FuncType>
void MyClass::compute(FuncType foo)
{
int a=1;
foo(a);
}
Then you can call run like this:
void MyClass::run()
{
compute(Foo(*obj_));
}
If your compiler supports lambdas (and there's a good chance it does), then you can forgo the function class and simply define run like this:
void MyClass::run()
{
auto f = [this](int i) {
return obj_->funcAnother(i);
};
compute(f);
}
I have a hash table template that I have written for a class. I have a project due that relies on utilizing this hash table. It accepts an unsigned integer value to initialize the number of buckets it has, as well as a hash function to point to. I have not written that hash function yet, but I have a declaration for it. When I try to use the member initializer in my Game class for the hash table data member, it gives me an error that I don't understand.
Error 1 error C3867: 'Game::xorHash': function call missing argument list; use '&Game::xorHash' to create a pointer to member
2 IntelliSense: no instance of constructor "HTable<Type>::HTable [with Type=std::string]" matches the argument list
argument types are: (int, unsigned int (const std::string &s))
my Hash Table class is as follows:
#pragma once
#include "SLList.h"
template<typename Type> class HTable
{
public:
HTable(unsigned int numOfBuckets, unsigned int (*hFunction) (const Type &v));
~HTable();
HTable<Type>& operator=(const HTable<Type>& that);
HTable(const HTable<Type>& that);
void insert(const Type& v);
bool findAndRemove(const Type& v);
void clear();
int find(const Type& v) const;
private:
SLList<Type>* ht;
unsigned int (*hFunct) (const Type &v);
unsigned int numOfBuck;
};
template<typename Type>
HTable<Type>::HTable(unsigned int numOfBuckets, unsigned int (*hFunction) (const Type &v))
{
ht = new SLList<Type>[numOfBuckets];
this->numOfBuck = numOfBuckets;
this->hFunct = hFunction;
}
template<typename Type>
HTable<Type>::~HTable()
{
delete [] ht;
ht = nullptr;
}
template<typename Type>
HTable<Type>& HTable<Type>::operator=(const HTable<Type>& that)
{
if(this != &that)
{
delete [] this->ht;
this->hFunct = that.hFunct;
this->numOfBuck = that.numOfBuck;
this->ht = new SLList<Type>[numOfBuck];
for(unsigned int i = 0; i < this->numOfBuck; i++)
this->ht[i] = that.ht[i];
}
return *this;
}
template<typename Type>
HTable<Type>::HTable(const HTable<Type>& that)
{
this = *that;
}
template<typename Type>
void HTable<Type>::insert(const Type& v)
{
ht[hFunct(v)].addHead(v);
}
template<typename Type>
bool HTable<Type>::findAndRemove(const Type& v)
{
SLLIter<Type> iter(ht[hFunct(v)]);
for(iter.begin(); !iter.end(); ++iter)
{
if(v == iter.current())
{
ht[hFunct(v)].remove(iter);
return true;
}
}
return false;
}
template<typename Type>
void HTable<Type>::clear()
{
for(unsigned int i = 0; i < this->numOfBuck; ++i)
ht[i].clear();
}
template<typename Type>
int HTable<Type>::find(const Type& v) const
{
SLLIter<Type> iter(ht[hFunct(v)]);
for(iter.begin(); !iter.end(); ++iter)
{
if(v == iter.current())
return hFunct(v);
}
return -1;
}
My Game.h:
#pragma once
#include "stdafx.h"
#include "HTable.h"
#include "BST.h"
#include "DTSTimer.h"
using namespace std;
class Game
{
public:
Game(void);
virtual ~Game(void);
void refresh();
void input();
unsigned int xorHash(const string &s);
private:
string userInput;
DTSTimer timer;
BST<string> answers;
HTable<string> dictionary;
};
My Game.cpp (this is obviously just a skeleton, since I can't get the member init to work)
#include "Game.h"
Game::Game(void) : dictionary(2048, xorHash)
{
}
Game::~Game(void)
{
}
void Game::refresh()
{
}
void Game::input()
{
}
unsigned int Game::xorHash(const string &s)
{
return 0;
}
I've been working on this for a good while, and have been hitting a wall. I would really appreciate some help on how to get this thing up and running. Let me know if there is another snippet that needs to be seen (I've tried to be thorough in that regard).
You have two problems. The first is that you don't pass the member function pointer properly (the error message tells you exactly what do do). The other problem is that a function pointer is not the same as a member function pointer.
A member function pointer needs an instance object object to call the member function on. And this instance is passed as a hidden first argument, something that normal functions don't have.
For this you might instead turn to std::function and std::bind:
class HTable
{
public:
HTable(unsigned int numOfBuckets, std::function<unsigned int(const Type&)> hFunction);
...
private:
std::function<unsigned int(const Type&)> hFunct;
...
};
Then
Game::Game(void) : dictionary(2048, std::bind(&Game::xorHash, this))
{
}
I have a template class, called Cell, here the definition:
template <class T>
class OneCell
{
.....
}
I have a cast operator from Cell to T, here
virtual operator const T() const
{
.....
}
Now i have derived class, called DCell, here
template <class T>
class DCell : public Cell<T>
{
.....
}
I need to override the Cell's cast operator (insert a little if), but after I need to call the Cell's cast operator. In other methods it's should be something like
virtual operator const T() const
{
if (...)
{
return Cell<T>::operator const T;
}
else throw ...
}
but i got a compiler error
error: argument of type 'const int (Cell::)()const' does not match 'const int'
What can I do?
Thank you, and sorry about my poor English.
You are missing parentheses, so the compiler thought you were trying to return the member function, not call it.
return Cell<T>::operator const T();
You're not actually calling the operator:
return Cell<T>::operator const T();
Full code:
template <class T>
class OneCell
{
public:
virtual operator const T() const
{
return T();
}
};
template <class T>
class DCell : public OneCell<T>
{
public:
virtual operator const T() const
{
cout << "operator called";
return OneCell<T>::operator const T();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
DCell<int> x;
int y = (int)x;
}
Consider this code with the implementations of Cell and DCell:
#include <iostream>
#include <exception>
template<class T>
class Cell
{
protected:
T cnt;
public:
Cell(const T& cnt = T()) : cnt(cnt){}
virtual operator const T() const { return cnt; }
};
bool test_bool = true;
template<class T>
class DCell : public Cell<T>
{
public:
DCell(const T& cnt = T()) : Cell<T>(cnt){}
virtual operator const T() const
{
if(test_bool)
{
return Cell<T>::operator const T(); // Here you had Cell<T>::operator const T;
} else {
throw std::exception();
}
}
};
int main()
{
DCell<int> cell(5);
std::cout << static_cast<int>(cell) << "\n"; // prints 5 (and a new line)
return 0;
}
Don't make the operator virtual. Instead, delegate to a protected virtual helper function.
template <class T>
class Cell
{
public:
operator const T() const { return cvt_T(); }
protected:
virtual const T cvt_T() const;
};
template <class T>
class DCell : public Cell<T>
{
const T cvt_T() const
{
if (...)
{
return Cell<T>::cvt_T();
}
else throw ...
}
};
This and other good practices can be learned from GotW, here is the section on virtual architecture.
I have some problem compiling my code.
I have the following structure:
#include <cstdlib>
using namespace std;
typedef double (*FuncType)(int );
class AnotherClass {
public:
AnotherClass() {};
double funcAnother(int i) {return i*1.0;}
};
class MyClass {
public:
MyClass(AnotherClass & obj) { obj_ = &obj;};
void compute(FuncType foo);
void run();
protected:
AnotherClass * obj_; /*pointer to obj. of another class */
};
void MyClass::compute(FuncType foo)
{
int a=1;
double b;
b= foo(a);
}
void MyClass::run()
{
compute(obj_->funcAnother);
}
/*
*
*/
int main(int argc, char** argv) {
AnotherClass a;
MyClass b(a);
b.run();
return 0;
}
When I try to compile it, it gives:
main.cpp:39:31: error: no matching function for call to ‘MyClass::compute(<unresolved overloaded function type>)’
main.cpp:30:6: note: candidate is: void MyClass::compute(double (*)(int))
What's wrong here?
p/s/ AnotherClass * obj_; should stay like that because I write some function to the big library and can't change it.
-------------- working version by Benjamin -------
#include <cstdlib>
using namespace std;
class AnotherClass {
public:
AnotherClass() {};
double funcAnother(int i) {return i*1.0;}
};
struct Foo
{
/*constructor*/
Foo(AnotherClass & a) : a_(a) {};
double operator()(int i) const
{
return a_.funcAnother(i);
}
AnotherClass & a_;
};
class MyClass {
public:
MyClass(AnotherClass & obj) { obj_ = &obj;};
template<typename FuncType>
void compute(FuncType foo);
void run();
protected:
AnotherClass * obj_; /*pointer to obj. of another class */
};
template<typename FuncType>
void MyClass::compute(FuncType foo)
{
int a=1;
double b;
b= foo(a);
}
void MyClass::run()
{
Foo f(*obj_);
compute(f);
}
/*
*
*/
int main(int argc, char** argv) {
AnotherClass a;
MyClass b(a);
b.run();
return 0;
}
Thank you everybody very much for the help!
Since,
funcAnother(int i);
is a member function it passes an implicit this and then the prototype does not match the type of your function pointer.
The typedef for pointer to member function should be:
typedef double (AnotherClass::*funcPtr)(int);
Here is a modified compilable version of your code. Please check the comments inline to understand the changes, Also I left out the other details, you can add that up.
The following function class will match the signature of your FuncType:
struct Foo
{
AnotherClass & a_;
Foo(AnotherClass & a) a_(a) {}
double operator()(int i) const
{
return a_.funcAnother(i);
}
};
Change MyClass::compute to a template, thusly:
template<typename FuncType>
void MyClass::compute(FuncType foo)
{
int a=1;
foo(a);
}
Then you can call run like this:
void MyClass::run()
{
compute(Foo(*obj_));
}
If your compiler supports lambdas (and there's a good chance it does), then you can forgo the function class and simply define run like this:
void MyClass::run()
{
auto f = [this](int i) {
return obj_->funcAnother(i);
};
compute(f);
}
In the underlying example: How can I avoid that the method get_Children will complain that the return type is not correct?
Or in compiler language:
test.cpp: In member function std::vector<A, std::allocator<A> > B::getVector() const:
test.cpp:38: error: passing const Box<A> as this argument of std::vector<T,std::allocator<_CharT> > Box<T>::get_Children() [with T = A] discards qualifiers
#include <vector>
using namespace std;
template <class T> class Box
{
private:
std::vector<T> m_data;
public:
Box() {};
virtual ~Box() {}
void Add(T const &d);
void Remove();
T get_Child(size_t i) const;
size_t get_ChildCount() const;
std::string get_ChildNames() const;
std::vector<T> get_Children() { return m_data; }
};
class A
{
public:
A();
~A();
};
class B
{
private:
Box<A> m_Container;
B(const B &orig);
public:
B();
~B();
std::vector<A> getVector() const { return m_Container.get_Children();}
};
int main()
{
B b;
std::vector<A> a_vector;
a_vector = b.getVector();
return 0;
}
Declare Box<T>::get_Children() as const. Because B::getVector() is const, it cannot access a non-const function on a member of B.