B
BSand0764
I'm getting an error that I can't seem to resolve. When I compile the
Functor related logic in a test program, the files compile and execute
properly (see Listing #1).
However, when I incorporate the same logic within my simulation, the
class that implements the functor logic has problems compiling. I get
the following errors:
-- Building myTest.cpp --
In file included from myTest.cpp:52:
.../tstDir/myFunctor.h:19: error: expected unqualified-id before '*'
token
.../tstDir/myFunctor.h:19: error: expected `)' before '*' token
.../tstDir/myFunctor.h:19: error: expected `,' or `...' before '*'
token
.../tstDir/myFunctor.h:19: error: `TSpecificFunctor' declared as
function returning a function
.../tstDir/myFunctor.h:19: error: `<anonymous>' has incomplete type
.../tstDir/myFunctor.h:19: error: invalid use of `void'
.../tstDir/myFunctor.h:19: error: expected `;' before ')' token
myTest.cpp: In member function `virtual void
tst::myTest::Initialize()':
myTest.cpp:527: error: no matching function for call to
`tst::TSpecificFunctor<tst::myScats>::TSpecificFunctor(tst::myScats*,
void (tst::myScats::*)())'
.../tstDir/myFunctor.h:14: note: candidates are:
tst::TSpecificFunctor<tst::myScats>::TSpecificFunctor(const
tst::TSpecificFunctor<tst::myScats>&)
Listing #2 provides the functor implementation as used in my
simulation. Can anyone tell me what is causing the compilation error
and how to possibly resolve it?
LISTING #2 (code in simulation)
In myTest.cpp:
#include "ScatFunctor.h"
#include "myScats.h"
#include "myFunctor.h"
void myTest::Initialize()
{
using namespace tst;
myScats objB;
TSpecificFunctor<myScats> specFunc( &objB, &myScats::Initialize);
myFunctor* funcTable[] = { &specFunc };
funcTable[0]->Call();
}
In myFunctor.h:
#ifndef myFunctor_H
#define myFunctor_H
#include "tst_config.h"
#include "ScatFunctor.h"
BEGIN_TST_NAMESPACE
template <class TClass> class TSpecificFunctor : public TFunctor
{
public:
// Constructor - takes pointer to an object and pointer to
// a member function and stores them in two private variables
TSpecificFunctor(TClass* _pt2Object, void(TClass::_*fpt) ())
{ pt2Object = _pt2Object; fpt = _fpt; };
// override function "call" in base class
virtual void Call()
{ (*pt2Object.*fpt) (); };
private:
void (TClass::*fpt) ();
TClass* pt2Object;
};
END_TST_NAMESPACE
#endif
In ScatFunctor.h:
#ifndef ScatFunctor_H
#define ScatFunctor_H
#include "tst_config.h"
BEGIN_TST_NAMESPACE
class TFunctor
{
public:
virtual void Call(); // call using function
};
END_TST_NAMESPACE
#endif
In myScats.h:
#ifndef myScats_H
#define myScats_H
#include "tst_config.h"
BEGIN_TST_NAMESPACE
class myScats
{
public:
// Constructor/Destructor
myScats() {};
virtual ~myScats();
void Initialize() {"In Scats Initialize"};
protected:
private:
};
END_TST_NAMESPACE
#endif // End of Listing #2
LISTING #1 (stand-alone test program)
In BaseFunctor.cpp:
#include "BaseFunctor.h"
#include "DerivedFunctor.h"
#include "sea.h"
int main (int argc, char* argv[])
{
sea objB;
// instantiate DerivedFunctor objects ...
// functor encapsulating pointer to object and to a member of
class sea
TSpecificFunctor<sea> specFuncSea(&objB, &sea:isplay);
// array with pointers to the Base class (TFunctor) and initialize
it
TFunctor *vT = { &specFuncSea };
vT->Call();
return 0;
}
In BaseFunctor.h:
#ifndef BaseFunctor_H
#define BaseFunctor_H
// abstract base class
class TFunctor
{
public:
// Derived classes will use a pointer to an object and
// a pointer to a member function to make the function call
virtual void Call()=0; //call using provided function name
};
#endif
In DerivedFunctor.h:
#ifndef TSpecificFunctor_H
#define TSpecificFunctor_H
#include "BaseFunctor.h"
template <class TClass> class TSpecificFunctor : public TFunctor
{
public:
// Constructor - takes pointer to an object and pointer to
// a member function and stores them in two private variables
TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt) ())
{ pt2Object = _pt2Object; fpt = _fpt; };
// override function "Call" in base class
virtual void Call() // executes member function
{ (*pt2Object.*fpt) (); };
private:
void (TClass::*fpt) (); // pointer to a member function
TClass* pt2Object; // pointer to an object
};
#endif
In Sea.h:
#ifndef sea_H
#define sea_H
#include <iostream>
using namespace std;
class sea
{
public:
sea() {};
// Sea's Functions
void Display() { cout << "In Sea Display" << endl; };
};
#endif
The above (LISTING #1) is compiled with the following script and
the resulting binary executes properly:
#/bin/sh
echo compiling C++ using -ansi -pedantic-errors -Wall -fPIC -g
g++ -ansi -pedantic-errors -Wall -Wno-deprecated -fPIC -g $1 $2 $3
Any help regarding some possible solutions for the compilation errors
would be greatly appreciated!
Danny
Functor related logic in a test program, the files compile and execute
properly (see Listing #1).
However, when I incorporate the same logic within my simulation, the
class that implements the functor logic has problems compiling. I get
the following errors:
-- Building myTest.cpp --
In file included from myTest.cpp:52:
.../tstDir/myFunctor.h:19: error: expected unqualified-id before '*'
token
.../tstDir/myFunctor.h:19: error: expected `)' before '*' token
.../tstDir/myFunctor.h:19: error: expected `,' or `...' before '*'
token
.../tstDir/myFunctor.h:19: error: `TSpecificFunctor' declared as
function returning a function
.../tstDir/myFunctor.h:19: error: `<anonymous>' has incomplete type
.../tstDir/myFunctor.h:19: error: invalid use of `void'
.../tstDir/myFunctor.h:19: error: expected `;' before ')' token
myTest.cpp: In member function `virtual void
tst::myTest::Initialize()':
myTest.cpp:527: error: no matching function for call to
`tst::TSpecificFunctor<tst::myScats>::TSpecificFunctor(tst::myScats*,
void (tst::myScats::*)())'
.../tstDir/myFunctor.h:14: note: candidates are:
tst::TSpecificFunctor<tst::myScats>::TSpecificFunctor(const
tst::TSpecificFunctor<tst::myScats>&)
Listing #2 provides the functor implementation as used in my
simulation. Can anyone tell me what is causing the compilation error
and how to possibly resolve it?
LISTING #2 (code in simulation)
In myTest.cpp:
#include "ScatFunctor.h"
#include "myScats.h"
#include "myFunctor.h"
void myTest::Initialize()
{
using namespace tst;
myScats objB;
TSpecificFunctor<myScats> specFunc( &objB, &myScats::Initialize);
myFunctor* funcTable[] = { &specFunc };
funcTable[0]->Call();
}
In myFunctor.h:
#ifndef myFunctor_H
#define myFunctor_H
#include "tst_config.h"
#include "ScatFunctor.h"
BEGIN_TST_NAMESPACE
template <class TClass> class TSpecificFunctor : public TFunctor
{
public:
// Constructor - takes pointer to an object and pointer to
// a member function and stores them in two private variables
TSpecificFunctor(TClass* _pt2Object, void(TClass::_*fpt) ())
{ pt2Object = _pt2Object; fpt = _fpt; };
// override function "call" in base class
virtual void Call()
{ (*pt2Object.*fpt) (); };
private:
void (TClass::*fpt) ();
TClass* pt2Object;
};
END_TST_NAMESPACE
#endif
In ScatFunctor.h:
#ifndef ScatFunctor_H
#define ScatFunctor_H
#include "tst_config.h"
BEGIN_TST_NAMESPACE
class TFunctor
{
public:
virtual void Call(); // call using function
};
END_TST_NAMESPACE
#endif
In myScats.h:
#ifndef myScats_H
#define myScats_H
#include "tst_config.h"
BEGIN_TST_NAMESPACE
class myScats
{
public:
// Constructor/Destructor
myScats() {};
virtual ~myScats();
void Initialize() {"In Scats Initialize"};
protected:
private:
};
END_TST_NAMESPACE
#endif // End of Listing #2
LISTING #1 (stand-alone test program)
In BaseFunctor.cpp:
#include "BaseFunctor.h"
#include "DerivedFunctor.h"
#include "sea.h"
int main (int argc, char* argv[])
{
sea objB;
// instantiate DerivedFunctor objects ...
// functor encapsulating pointer to object and to a member of
class sea
TSpecificFunctor<sea> specFuncSea(&objB, &sea:isplay);
// array with pointers to the Base class (TFunctor) and initialize
it
TFunctor *vT = { &specFuncSea };
vT->Call();
return 0;
}
In BaseFunctor.h:
#ifndef BaseFunctor_H
#define BaseFunctor_H
// abstract base class
class TFunctor
{
public:
// Derived classes will use a pointer to an object and
// a pointer to a member function to make the function call
virtual void Call()=0; //call using provided function name
};
#endif
In DerivedFunctor.h:
#ifndef TSpecificFunctor_H
#define TSpecificFunctor_H
#include "BaseFunctor.h"
template <class TClass> class TSpecificFunctor : public TFunctor
{
public:
// Constructor - takes pointer to an object and pointer to
// a member function and stores them in two private variables
TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt) ())
{ pt2Object = _pt2Object; fpt = _fpt; };
// override function "Call" in base class
virtual void Call() // executes member function
{ (*pt2Object.*fpt) (); };
private:
void (TClass::*fpt) (); // pointer to a member function
TClass* pt2Object; // pointer to an object
};
#endif
In Sea.h:
#ifndef sea_H
#define sea_H
#include <iostream>
using namespace std;
class sea
{
public:
sea() {};
// Sea's Functions
void Display() { cout << "In Sea Display" << endl; };
};
#endif
The above (LISTING #1) is compiled with the following script and
the resulting binary executes properly:
#/bin/sh
echo compiling C++ using -ansi -pedantic-errors -Wall -fPIC -g
g++ -ansi -pedantic-errors -Wall -Wno-deprecated -fPIC -g $1 $2 $3
Any help regarding some possible solutions for the compilation errors
would be greatly appreciated!
Danny