首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求指点,关于成员函数指针数组

2013-01-23 
求指导,关于成员函数指针数组代码如下:#include string#include iostream#include functionalusing

求指导,关于成员函数指针数组
代码如下:



#include <string>
#include <iostream>
#include <functional>


using namespace std;

class A
{
public:
int a(int );
int b(int);
int c(int);
int d(int);
typedef int (A::*func)(int);
func Func[4];
A()
{
Func[0]=&A::a;
Func[1]=&A::b;
Func[2]=&A::c;
Func[3]=&A::d;
}
};

int main()
{
A k;
return 1;
}


报错

1>main.obj : error LNK2001: 无法解析的外部符号 "public: int __thiscall A::d(int)" (?d@A@@QAEHH@Z)
1>main.obj : error LNK2001: 无法解析的外部符号 "public: int __thiscall A::c(int)" (?c@A@@QAEHH@Z)
1>main.obj : error LNK2001: 无法解析的外部符号 "public: int __thiscall A::b(int)" (?b@A@@QAEHH@Z)
1>main.obj : error LNK2001: 无法解析的外部符号 "public: int __thiscall A::a(int)" (?a@A@@QAEHH@Z)


求指导。。。 c
[解决办法]

int a(int ){return 0;}
int b(int){return 0;}
int c(int){return 0;}
int d(int){return 0;}


[解决办法]
你的成员函数的定义,没有实现:
class A
{
public:
    int a(int )
{
return 0;
}
    int b(int)
{
return 0;
}
    int c(int)
{
return 0;
}
int d(int)
{
return 0;
}

    typedef int (A::*func)(int);
    func Func[4];
    A()
    {
        Func[0]=&A::a;
        Func[1]=&A::b;
        Func[2]=&A::c;
        Func[3]=&A::d;
    }
};
[解决办法]

#include <string>
#include <iostream>
#include <functional>
 
 
using namespace std;
 
class A
{
public:
  int a(int){return 0;}
  int b(int){return 0;}
  int c(int){return 0;}
  int d(int){return 0;}
  typedef int (A::*func)(int);
  func Func[4];
  A()
  {
    Func[0]=&A::a;
    Func[1]=&A::b;
    Func[2]=&A::c;
    Func[3]=&A::d;
  }
};
 
int main()
{
  A k;
  return 1;
}

[解决办法]
函数声明,没有函数地址。 你要赋值,就要把让编译器知道你函数的完整信息。
没有函数实现,编译器都不知道你的 函数信息。 具体你自己看一下书籍。
就像
class A
{
public:
void func(){}
}
A* a = NULL;
a->func(); //对象为空为什么可以调用 //不写函数实现为什么不能调用

热点排行