Member function pointer to derived

S

Stepan Seycek

Hallo,

I'm writing a test system with the approach of providing the test
infrastructure in a base class and adding the test methods in the
concrete derived test classes by registering member function pointers.

However my code fails to compile with:

g++ -o mfp -WAll mfp.cpp
mfp.cpp: In member function `void Base<T>::registerTest(int (T::*)())':
mfp.cpp:11: error: expected unqualified-id before '.' token


Here's a reduced version of the source:

//-[mfp.cpp]----------------------------------------------------

#include <vector>

template <typename T>
class Base
{
public:
typedef int (T::*TestMethod)();

Base () {}
virtual ~Base() {}
void registerTest(TestMethod tm) {tests_.push_back(tm);}

private:
typedef std::vector<TestMethod> tests_;
};

class Derived : Base<Derived>
{
public:
int testIt() {}
void setup() {registerTest(&Derived::testIt);}
};

int main(int argc, char **argv)
{
Derived test;
test.setup();
}
//--------------------------------------------------------------

Any hint is appreciated!

Br,
Stepan Seycek
 
H

Helge Kruse

This function uses a member tests_.
void registerTest(TestMethod tm) {tests_.push_back(tm);}

This does not define an member but a type. Remove 'typedef'.
typedef std::vector<TestMethod> tests_;


This function must return a value.
int main(int argc, char **argv)
{
Derived test;
test.setup();
}

Any hint is appreciated!
Hints are inline.

Regards,
Helge
 
S

Stepan Seycek

Thanks Paavo,

mea culpa. Works fine after fixing this error.

Br,
Stepan


Paavo said:
Hallo,

I'm writing a test system with the approach of providing the test
infrastructure in a base class and adding the test methods in the
concrete derived test classes by registering member function pointers.

However my code fails to compile with:

g++ -o mfp -WAll mfp.cpp
mfp.cpp: In member function `void Base<T>::registerTest(int (T::*)())':
mfp.cpp:11: error: expected unqualified-id before '.' token


Here's a reduced version of the source:

//-[mfp.cpp]----------------------------------------------------

#include <vector>

template <typename T>
class Base
{
public:
typedef int (T::*TestMethod)();

Base () {}
virtual ~Base() {}
void registerTest(TestMethod tm) {tests_.push_back(tm);}

private:
typedef std::vector<TestMethod> tests_;

tests_ is a type, you cannot push_back anything into it.

hth
Paavo
 
S

Stepan Seycek

Hallo,

I have stripped the posted source too much. In fact my probleme was
related to a missing "typename" (see below). However thanks for you support!

Stepan

//-[mfp.cpp]----------------------------------------------------

#include <map>
#include <string>

template <typename T>
class Base
{
public:
typedef int (T::*TestMethod)();

Base () {}
virtual ~Base() {}
void registerTest(const char* name, TestMethod tm)
{
typename /*(was missing)*/ Tests::value_type val(name, tm);
tests_.insert(val);
}

private:
typedef std::map<std::string, TestMethod> Tests;
Tests tests_;
};

class Derived : Base<Derived>
{
public:
int testIt() {}
void setup() {registerTest("foo", &Derived::testIt);}
};

int main(int argc, char **argv)
{
Derived test;
test.setup();
return 0;
}

//--------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top