Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用带有数组订阅的指针调用结构变量时出错 //菜单结构 结构菜单 { 字符*名称; 无效(*func)(); }; 菜单*tmpPtr=NULL; //菜单功能原型 void fileFunction(); void editFunction(); void viewFunction(); void exitFunction(); int main(int argc,char*argv[]) { 无符号选择=0; 菜单菜单项指令[]={ {“文件”,fileFunction}, {“编辑”,编辑函数}, {“视图”,视图函数}, {“退出”,退出函数}; tmpPtr=菜单项; while(选择!=4) { 对于(int i=0;i_C++_Pointers - Fatal编程技术网

C++ 使用带有数组订阅的指针调用结构变量时出错 //菜单结构 结构菜单 { 字符*名称; 无效(*func)(); }; 菜单*tmpPtr=NULL; //菜单功能原型 void fileFunction(); void editFunction(); void viewFunction(); void exitFunction(); int main(int argc,char*argv[]) { 无符号选择=0; 菜单菜单项指令[]={ {“文件”,fileFunction}, {“编辑”,编辑函数}, {“视图”,视图函数}, {“退出”,退出函数}; tmpPtr=菜单项; while(选择!=4) { 对于(int i=0;i

C++ 使用带有数组订阅的指针调用结构变量时出错 //菜单结构 结构菜单 { 字符*名称; 无效(*func)(); }; 菜单*tmpPtr=NULL; //菜单功能原型 void fileFunction(); void editFunction(); void viewFunction(); void exitFunction(); int main(int argc,char*argv[]) { 无符号选择=0; 菜单菜单项指令[]={ {“文件”,fileFunction}, {“编辑”,编辑函数}, {“视图”,视图函数}, {“退出”,退出函数}; tmpPtr=菜单项; while(选择!=4) { 对于(int i=0;i,c++,pointers,C++,Pointers,// Menu Structure struct Menu { char* name; void (*func) (); }; Menu* tmpPtr = NULL; // Menu Function prototypes void fileFunction(); void editFunction(); void viewFunction(); void exitFunction(); int main(int argc, char *argv[]) {

// Menu Structure

struct Menu 
{

   char* name;

   void (*func) ();
};

Menu* tmpPtr = NULL;

// Menu Function prototypes

void fileFunction();

void editFunction();

void viewFunction();

void exitFunction();


int main(int argc, char *argv[])
{

   unsigned selection = 0 ;

   Menu menuStruct[] = {  

      {"file", fileFunction},

      {"edit", editFunction},

      {"view", viewFunction},

      {"exit", exitFunction}  };

   tmpPtr = menuStruct;


   while(selection != 4)
   {
      for(int i = 0 ; i < 4 ; i++)
      {
         std::cout << i+1 <<" : " << tmpPtr->name << std::endl;
         tmpPtr++;

      }
      tmpPtr = menuStruct;


      std::cout<< "Enter Selection Value: " ;
      std::cin>>selection;

      if(selection <= 4)
         *(tmpPtr[selection-1]->func)();

   }   

   return 0;
}
是一个问题,因为
tmpPtr[selection-1]
的类型是
菜单
,而不是
菜单*
。您需要使用:

     *(tmpPtr[selection-1]->func)();
更新

由于运算符优先级,该行

     *(tmpPtr[selection-1].func)();

     *(tmpPtr[selection-1].func)();
由于
菜单:func
的返回类型为
void
,因此该行相当于:

     *((tmpPtr[selection-1].func)());
这是不对的

您可以使用:

    *(void)
或者,简单地说:

     (*tmpPtr[selection-1].func)();

当我使用.operator时,它显示的错误是“void value未被忽略,因为它应该被忽略”。为什么?
     tmpPtr[selection-1].func();