Hello,
I have a compile error trying to build come code with GCC. It works with
MSVC 7, so I was hoping it was valid C++. What do I need to change to
make this compile under GCC?
We are trying to use a pointer to a member function of a template class
to speed up part of our code, where we are selecting one of a number of
similar functions, so a template function makes the code easier to maintain.
This inane example reproduces the problem while retaining the structure
of the original code.
//------------------------------------------------------------------------------
template<class T>
class TestClass
{
public:
TestClass() {}
void setFunc(bool, bool, bool);
private:
template<bool,bool,bool> void calculate();
protected:
void (TestClass::*pFunc)();
float data;
};
//------------------------------------------------------------------------------
template<class T>
template<bool PLUS, bool MODULO, bool SQUARE>
void TestClass<T>::calculate()
{
if (PLUS)
{
data += 10;
if (MODULO)
{
data %= 5;
}
}
else
{
data -= 7;
}
if (SQUARE)
{
data *= data;
}
}
//------------------------------------------------------------------------------
template<class T>
void TestClass<T>::setFunc(bool plus, bool modulo, bool square)
{
if (plus)
{
if (modulo)
{
if (square)
{
pFunc = &TestClass<T>::calculate<true,true,true>;
}
else
{
pFunc = &TestClass<T>::calculate<true,true,false>;
}
}
else
{
if (square)
{
pFunc = &TestClass<T>::calculate<true,false,true>;
}
else
{
pFunc = &TestClass<T>::calculate<true,false,false>;
}
}
}
else
{
if (modulo)
{
if (square)
{
pFunc = &TestClass<T>::calculate<false,true,true>;
}
else
{
pFunc = &TestClass<T>::calculate<false,true,false>;
}
}
else
{
if (square)
{
pFunc = &TestClass<T>::calculate<false,false,true>;
}
else
{
pFunc = &TestClass<T>::calculate<false,false,false>;
}
}
}
}
//------------------------------------------------------------------------------
int main()
{
TestClass<int> testClass;
testClass.setFunc(true, true, false);
}
//------------------------------------------------------------------------------
This gives the following error message when compiled.
testclass.cpp: In member function 'void TestClass<T>::setFunc(bool,
bool, bool)':
testclass.cpp:57: error: expected primary-expression before ';' token
testclass.cpp:61: error: expected primary-expression before ';' token
testclass.cpp:68: error: expected primary-expression before ';' token
testclass.cpp:72: error: expected primary-expression before ';' token
testclass.cpp:82: error: expected primary-expression before ';' token
testclass.cpp:86: error: expected primary-expression before ';' token
testclass.cpp:93: error: expected primary-expression before ';' token
testclass.cpp:97: error: expected primary-expression before ';' token
testclass.cpp: In member function 'void TestClass<T>::setFunc(bool,
bool, bool) [with T = int]':
testclass.cpp:109: instantiated from here
testclass.cpp:57: error: address of overloaded function with no
contextual type information
testclass.cpp:61: error: address of overloaded function with no
contextual type information
testclass.cpp:68: error: address of overloaded function with no
contextual type information
testclass.cpp:72: error: address of overloaded function with no
contextual type information
testclass.cpp:82: error: address of overloaded function with no
contextual type information
testclass.cpp:86: error: address of overloaded function with no
contextual type information
testclass.cpp:93: error: address of overloaded function with no
contextual type information
testclass.cpp:97: error: address of overloaded function with no
contextual type information
GCC version is powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple
Computer, Inc. build 5247)