Well i got it all working properly. Turns out the problem was evidently Dev-C++. The below code runs in Visual C++, Borland Builder 6.0 and CodeWarrior, but not in Dev-C++. Below is the errors received and below that is the code.
line1:
/Dev-Cpp/Bin/../lib/gcc-lib/mingw32/2.95.3-6/../../../../include/g++-3/stl_alloc.h C:\Documents and Settings\Gary Register\My Documents\code\FunctionPointer\examples\C
In instantiation of `allocator<int (FPclass::*)()>':
line2:
25 C:\Documents and Settings\Gary Register\My Documents\code\FunctionPointer\examples\Untitled1.cpp
instantiated from here
line3:
750 C:\Dev-Cpp\include\g++-3\stl_alloc.h
`allocator<int (FPclass::*)()>::address(int (FPclass::* &)()) const' has already been declared in `allocator<int (FPclass::*)()>'
// Here the working code for all other compilers
// fp.cpp : Investigation of vector <member function pointers> v;
// #include "stdafx.h" // only used in Visual C++
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
class FPclass
{
public:
FPclass();
~FPclass();
int CallMemberFunction(int option);
int number_of_functions();
private:
int func0(void);
int func1(void);
int func2(void);
int func3(void);
typedef int (FPclass::*FuncPointer)(void);
vector<FuncPointer> functions;
};
int FPclass::number_of_functions()
{
return functions.size();
}
int FPclass::CallMemberFunction(int option)
{
if (option < functions.size()) {
(this->*functions[option])();
}
return 1;
}
int FPclass::func0(void)
{
cout << "member function 0 has been called" << endl;
return 1;
}
int FPclass::func1(void)
{
cout << "member function 1 has been called" << endl;
return 1;
}
//***********************************************
int main(int argc, char* argv[])
{
FPclass fp;
for (int i =0; i<fp.number_of_functions();i++)
fp.CallMemberFunction(i);
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-03-02
Incidently, this code also works with g++ on SunOS. I understand that Dev-C++ is just a front end product. This code however may point out an issue with g++ under mingw. I'm not sure what it points out to developers whom may be interested, but this has been compiled and run under several compilers with no errors.
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well i got it all working properly. Turns out the problem was evidently Dev-C++. The below code runs in Visual C++, Borland Builder 6.0 and CodeWarrior, but not in Dev-C++. Below is the errors received and below that is the code.
line1:
/Dev-Cpp/Bin/../lib/gcc-lib/mingw32/2.95.3-6/../../../../include/g++-3/stl_alloc.h C:\Documents and Settings\Gary Register\My Documents\code\FunctionPointer\examples\C
In instantiation of `allocator<int (FPclass::*)()>':
line2:
25 C:\Documents and Settings\Gary Register\My Documents\code\FunctionPointer\examples\Untitled1.cpp
instantiated from here
line3:
750 C:\Dev-Cpp\include\g++-3\stl_alloc.h
`allocator<int (FPclass::*)()>::address(int (FPclass::* &)()) const' has already been declared in `allocator<int (FPclass::*)()>'
// Here the working code for all other compilers
// fp.cpp : Investigation of vector <member function pointers> v;
// #include "stdafx.h" // only used in Visual C++
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
class FPclass
{
public:
FPclass();
~FPclass();
int CallMemberFunction(int option);
int number_of_functions();
private:
int func0(void);
int func1(void);
int func2(void);
int func3(void);
typedef int (FPclass::*FuncPointer)(void);
vector<FuncPointer> functions;
};
FPclass::FPclass()
{
functions.push_back(&FPclass::func0);
functions.push_back(&FPclass::func1);
}
FPclass::~FPclass() {}
int FPclass::number_of_functions()
{
return functions.size();
}
int FPclass::CallMemberFunction(int option)
{
if (option < functions.size()) {
(this->*functions[option])();
}
return 1;
}
int FPclass::func0(void)
{
cout << "member function 0 has been called" << endl;
return 1;
}
int FPclass::func1(void)
{
cout << "member function 1 has been called" << endl;
return 1;
}
//***********************************************
int main(int argc, char* argv[])
{
FPclass fp;
for (int i =0; i<fp.number_of_functions();i++)
fp.CallMemberFunction(i);
return 0;
}
Incidently, this code also works with g++ on SunOS. I understand that Dev-C++ is just a front end product. This code however may point out an issue with g++ under mingw. I'm not sure what it points out to developers whom may be interested, but this has been compiled and run under several compilers with no errors.
Thanks