初级菜鸟问一个弱智的类内函数指针数组定义问题,求指点

呵呵程序喵 2017-08-07 05:03:37
class Widget
{
Widget()
{
pa[0] = &f;
pa[1] = &g;
pa[2] = &i;
pa[3] = &j;
}

void select(int idx)
{
(this->*pa[idx])();
}

private:
void f(){cout<<"void f()"<<endl;}
void g(){cout<<"void g()"<<endl;}
void i(){cout<<"void i()"<<endl;}
void j(){cout<<"void j()"<<endl;}
enum {cnt = 4};
void (Widget::*pa[cnt])() ={f,g,i,j};
};


void (Widget::*pa[cnt])() ={f,g,i,j}; //这一行一直报错,Widget::Widget::pa”: 无法指定数组的显式初始值设定项,编译器用的QT
...全文
372 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
做或不做 2017-08-11
  • 打赏
  • 举报
回复
以下观点仅是个人对于问题解决方法的倾向 并不是想误导别人。仅供参考。 首先纠正下你一个问题 编译器用的qt这句话 总感觉怪怪的。 不知道上面的人 为什么没有纠正。 不讨论问题本质。如果你使用的是VS2015的编译器的话 我猜测大概率你是可以编译过去的。 如果你是在widows下编程 那么你编译器用的只能是 VS2010 VS2012 VS2013 VS2015等其中的一个吧。 msvc是编译器 ,QT只是一个界面框架。 特别的就是实现中采用了有别于C++模板的元对象编程技术。 给你讲一个故事 就是说美国人为了在太空设计一种可以使用的墨水钢笔 花费了100万研究出来 苏联人不使用这种带墨水的钢笔 直接使用电子笔。 其实我想说的是就算是C++11的标准 也可以通过非C++11标准的C++重写成别的样子。不要纠结怎么可以编译过去。 假设就算打死你都不可能编译的过去 你就算怎么改 感觉也是无济于事的。因为前面根本就没有路。
Really_want 2017-08-10
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

//g++ -g -Wall -std=c++11 -o a.exe main.cpp

struct A
{
	int a[2] = {1,2}; //C++11支持这种语法
};

int main(int argc,char* agrv[])
{
	A a;
	cout<< a.a[0] << "," << a.a[1] <<endl;
	return 0;
}

constCpp 2017-08-08
  • 打赏
  • 举报
回复
成员变量能直接初始化吗?
jena_wy 2017-08-08
  • 打赏
  • 举报
回复
只有静态常量整型数据成员才可以在类中初始化
呵呵程序喵 2017-08-07
  • 打赏
  • 举报
回复
您说的我还不太明白,我改用了public试了,依然不行,这跟C++11关系大吗?我用的是比较新的QT版本
引用 9 楼 ccssddnn218 的回复:
好吧,其实跟是否private没关系,重点是c++11 我排错时,在还没有用C++11前有private的提示,误以为有区别了。
呵呵程序喵 2017-08-07
  • 打赏
  • 举报
回复
多谢!我再自己分析一下
引用 6 楼 zhao4zhong1 的回复:
仅供参考:
#include <stdio.h>
double A[300][100];
double valuex[300];
double valuey[300];
int i;
double fun00(double x,double y) {return x  +   y  ;};
double fun01(double x,double y) {return x*x+ 3*y  ;};
double fun02(double x,double y) {return x  + 2*y*x;};
//...  fun03(double x,double y) {return ...+...   ;};
//...
//...  fun98(double x,double y) {return ...+...   ;};
double fun99(double x,double y) {return x/2+20*y  ;};
double (*funNN[100])(double,double)={
    fun00,
    fun01,
    fun02,
//  fun03,
//  ...
//  fun98,
    fun99,
};
int main() {
    double x,y;
    int f,FN;

    for (i=0;i<300;i++) {
        valuex[i]=(double)i;
        valuey[i]=(double)i;
    }
    FN=3;
    for (i=0;i<300;i++) {
         x=valuex[i];
         y=valuey[i];
         for (f=0;f<FN;f++) A[i][f]=funNN[f](x,y);
    }
    for (i=0;i<3;i++) {
        for (f=0;f<FN;f++) printf("%lg ",A[i][f]);
        printf("\n");
    }
	return 0;
}
//0 0 0
//2 4 3
//4 10 10
Really_want 2017-08-07
  • 打赏
  • 举报
回复
好吧,其实跟是否private没关系,重点是c++11 我排错时,在还没有用C++11前有private的提示,误以为有区别了。
Really_want 2017-08-07
  • 打赏
  • 举报
回复
引用 3 楼 m0_38024304 的回复:
多谢大家回复! @赵4老师 加static依然不行 @ccssddnn218 我改用了private 依然不行, 完整程序如下,用的QT,我看的王桂林老师的视频,他在讲课时这样做就行
#include <iostream>

using namespace std;

class Widget
{
public:
    Widget()
    {
        pa[0] = &f;
        pa[1] = &g;
        pa[2] = &i;
        pa[3] = &j;
    }

    void select(int idx)
    {
        (this->*pa[idx])();
    }

private:
    void f(){cout<<"void f()"<<endl;}
    void g(){cout<<"void g()"<<endl;}
    void i(){cout<<"void i()"<<endl;}
    void j(){cout<<"void j()"<<endl;}
    enum {cnt = 4};
    void (Widget::*pa[cnt])() ={f,g,i,j};
};

int main()
{
    Widget w;
    w.select(2);
    return 0;
}
1. 我说了用private不行!!!我代码中使用的是public。 2. 你可能没注意 我图片中的标识,需要C++11标准支持
赵4老师 2017-08-07
  • 打赏
  • 举报
回复
再供参考:
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
赵4老师 2017-08-07
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
double A[300][100];
double valuex[300];
double valuey[300];
int i;
double fun00(double x,double y) {return x  +   y  ;};
double fun01(double x,double y) {return x*x+ 3*y  ;};
double fun02(double x,double y) {return x  + 2*y*x;};
//...  fun03(double x,double y) {return ...+...   ;};
//...
//...  fun98(double x,double y) {return ...+...   ;};
double fun99(double x,double y) {return x/2+20*y  ;};
double (*funNN[100])(double,double)={
    fun00,
    fun01,
    fun02,
//  fun03,
//  ...
//  fun98,
    fun99,
};
int main() {
    double x,y;
    int f,FN;

    for (i=0;i<300;i++) {
        valuex[i]=(double)i;
        valuey[i]=(double)i;
    }
    FN=3;
    for (i=0;i<300;i++) {
         x=valuex[i];
         y=valuey[i];
         for (f=0;f<FN;f++) A[i][f]=funNN[f](x,y);
    }
    for (i=0;i<3;i++) {
        for (f=0;f<FN;f++) printf("%lg ",A[i][f]);
        printf("\n");
    }
	return 0;
}
//0 0 0
//2 4 3
//4 10 10
sdghchj 2017-08-07
  • 打赏
  • 举报
回复
void (Widget::*pa[cnt])() ={&f,&g,&i,&j};
赵4老师 2017-08-07
  • 打赏
  • 举报
回复
《深度探索C++对象模型》 《C++反汇编与逆向分析技术揭秘》
呵呵程序喵 2017-08-07
  • 打赏
  • 举报
回复
多谢大家回复! @赵4老师 加static依然不行 @ccssddnn218 我改用了private 依然不行, 完整程序如下,用的QT,我看的王桂林老师的视频,他在讲课时这样做就行
#include <iostream>

using namespace std;

class Widget
{
public:
    Widget()
    {
        pa[0] = &f;
        pa[1] = &g;
        pa[2] = &i;
        pa[3] = &j;
    }

    void select(int idx)
    {
        (this->*pa[idx])();
    }

private:
    void f(){cout<<"void f()"<<endl;}
    void g(){cout<<"void g()"<<endl;}
    void i(){cout<<"void i()"<<endl;}
    void j(){cout<<"void j()"<<endl;}
    enum {cnt = 4};
    void (Widget::*pa[cnt])() ={f,g,i,j};
};

int main()
{
    Widget w;
    w.select(2);
    return 0;
}
Really_want 2017-08-07
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class Widget
{
public: //构造函数要public
Widget()
{
pa[0] = &f;
pa[1] = &g;
pa[2] = &i;
pa[3] = &j;
}

void select(int idx)
{
(this->*pa[idx])();
}

public: //private不行
void f(){cout<<"void f()"<<endl;}
void g(){cout<<"void g()"<<endl;}
void i(){cout<<"void i()"<<endl;}
void j(){cout<<"void j()"<<endl;}
enum {cnt = 4};
void (Widget::*pa[cnt])() ={f,g,i,j};
};
int main()
{
Widget w;
w.select(2);
return 0;
}

赵4老师 2017-08-07
  • 打赏
  • 举报
回复
加static ?

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧