Templates and Array of structs that contain pointer to method in class












-2














I am trying to create a class that defines an array of command structs, each of which has two CHAR variables, one CHAR*, one INT and a pointer to a void function that accepts two CHAR arguments. There will be multiple instances of this array, each in a different class. I feel like I am close, but missing something critical. The Arduino GNU compiler seems to agree that I am missing something. Here's the code (with modifications as per Bo R);



<<<<<<<<<< Commands.h >>>>>>>>>>>>>>>>>

#pragma once
//template <class T>
class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
command { 'a','?',"FOO", &Commands::foo },
command { 'b','x',"BAR", &Commands::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );

void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
( this->*myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


<<<<<<<<<< StructArray.ino >>>>>>>>>>>>>>>>>

#include "Commands.h"
Commands cmd;
void setup() {
Serial.begin ( 115200 );
Serial.println ( "EXECUTING:" );
cmd.execute ( 'a', '?' );
cmd.execute ( 'b', '?' );
cmd.execute ( 'b', 'x' );
Serial.println ( "DONE" );
}

void loop(){}


If I refactor Commands into Template.h and Foo.h (as below), I get four compile errors that I don't understand how to fix:



Severity Code Description File Line
Error error: invalid use of template-name 'Template' without an argument list D:DocumentsArduinoStructArrayFoo.h 6
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: invalid use of template-name 'Foo' without an argument list D:DocumentsArduinoStructArrayStructArray.ino 7



Here is the code for Template.h:



#pragma once
template <class T>
class Template {
public:
typedef void ( T::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act, int cmdSize ) {
for (int i = 0; i < cmdSize; i++) {
if (T::myCommands[i].sel == sel && T::myCommands [i].act == act) {
Serial.println ( T::myCommands [i].desc );
( this->*T::myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


And Foo.h:



#pragma once
#include "Template.h"
template<class T>
class Foo {
public:
Template::command myCommands [2] = {
command { 'a','?',"FOO-A", &Foo::foo },
command { 'b','x',"FOO-B", &Foo::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );


void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}
};









share|improve this question




















  • 2




    What's the error message?
    – Matthieu Brucher
    Nov 20 '18 at 22:00










  • #pragma once is only available to msvc, last i bothered to check many years ago.
    – johnathan
    Nov 20 '18 at 22:33










  • Did you mean FunctionPointer f; instead of void ( *FunctionPointer )( char, char ); ?
    – Jarod42
    Nov 20 '18 at 22:44
















-2














I am trying to create a class that defines an array of command structs, each of which has two CHAR variables, one CHAR*, one INT and a pointer to a void function that accepts two CHAR arguments. There will be multiple instances of this array, each in a different class. I feel like I am close, but missing something critical. The Arduino GNU compiler seems to agree that I am missing something. Here's the code (with modifications as per Bo R);



<<<<<<<<<< Commands.h >>>>>>>>>>>>>>>>>

#pragma once
//template <class T>
class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
command { 'a','?',"FOO", &Commands::foo },
command { 'b','x',"BAR", &Commands::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );

void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
( this->*myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


<<<<<<<<<< StructArray.ino >>>>>>>>>>>>>>>>>

#include "Commands.h"
Commands cmd;
void setup() {
Serial.begin ( 115200 );
Serial.println ( "EXECUTING:" );
cmd.execute ( 'a', '?' );
cmd.execute ( 'b', '?' );
cmd.execute ( 'b', 'x' );
Serial.println ( "DONE" );
}

void loop(){}


If I refactor Commands into Template.h and Foo.h (as below), I get four compile errors that I don't understand how to fix:



Severity Code Description File Line
Error error: invalid use of template-name 'Template' without an argument list D:DocumentsArduinoStructArrayFoo.h 6
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: invalid use of template-name 'Foo' without an argument list D:DocumentsArduinoStructArrayStructArray.ino 7



Here is the code for Template.h:



#pragma once
template <class T>
class Template {
public:
typedef void ( T::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act, int cmdSize ) {
for (int i = 0; i < cmdSize; i++) {
if (T::myCommands[i].sel == sel && T::myCommands [i].act == act) {
Serial.println ( T::myCommands [i].desc );
( this->*T::myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


And Foo.h:



#pragma once
#include "Template.h"
template<class T>
class Foo {
public:
Template::command myCommands [2] = {
command { 'a','?',"FOO-A", &Foo::foo },
command { 'b','x',"FOO-B", &Foo::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );


void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}
};









share|improve this question




















  • 2




    What's the error message?
    – Matthieu Brucher
    Nov 20 '18 at 22:00










  • #pragma once is only available to msvc, last i bothered to check many years ago.
    – johnathan
    Nov 20 '18 at 22:33










  • Did you mean FunctionPointer f; instead of void ( *FunctionPointer )( char, char ); ?
    – Jarod42
    Nov 20 '18 at 22:44














-2












-2








-2







I am trying to create a class that defines an array of command structs, each of which has two CHAR variables, one CHAR*, one INT and a pointer to a void function that accepts two CHAR arguments. There will be multiple instances of this array, each in a different class. I feel like I am close, but missing something critical. The Arduino GNU compiler seems to agree that I am missing something. Here's the code (with modifications as per Bo R);



<<<<<<<<<< Commands.h >>>>>>>>>>>>>>>>>

#pragma once
//template <class T>
class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
command { 'a','?',"FOO", &Commands::foo },
command { 'b','x',"BAR", &Commands::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );

void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
( this->*myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


<<<<<<<<<< StructArray.ino >>>>>>>>>>>>>>>>>

#include "Commands.h"
Commands cmd;
void setup() {
Serial.begin ( 115200 );
Serial.println ( "EXECUTING:" );
cmd.execute ( 'a', '?' );
cmd.execute ( 'b', '?' );
cmd.execute ( 'b', 'x' );
Serial.println ( "DONE" );
}

void loop(){}


If I refactor Commands into Template.h and Foo.h (as below), I get four compile errors that I don't understand how to fix:



Severity Code Description File Line
Error error: invalid use of template-name 'Template' without an argument list D:DocumentsArduinoStructArrayFoo.h 6
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: invalid use of template-name 'Foo' without an argument list D:DocumentsArduinoStructArrayStructArray.ino 7



Here is the code for Template.h:



#pragma once
template <class T>
class Template {
public:
typedef void ( T::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act, int cmdSize ) {
for (int i = 0; i < cmdSize; i++) {
if (T::myCommands[i].sel == sel && T::myCommands [i].act == act) {
Serial.println ( T::myCommands [i].desc );
( this->*T::myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


And Foo.h:



#pragma once
#include "Template.h"
template<class T>
class Foo {
public:
Template::command myCommands [2] = {
command { 'a','?',"FOO-A", &Foo::foo },
command { 'b','x',"FOO-B", &Foo::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );


void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}
};









share|improve this question















I am trying to create a class that defines an array of command structs, each of which has two CHAR variables, one CHAR*, one INT and a pointer to a void function that accepts two CHAR arguments. There will be multiple instances of this array, each in a different class. I feel like I am close, but missing something critical. The Arduino GNU compiler seems to agree that I am missing something. Here's the code (with modifications as per Bo R);



<<<<<<<<<< Commands.h >>>>>>>>>>>>>>>>>

#pragma once
//template <class T>
class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
command { 'a','?',"FOO", &Commands::foo },
command { 'b','x',"BAR", &Commands::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );

void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
( this->*myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


<<<<<<<<<< StructArray.ino >>>>>>>>>>>>>>>>>

#include "Commands.h"
Commands cmd;
void setup() {
Serial.begin ( 115200 );
Serial.println ( "EXECUTING:" );
cmd.execute ( 'a', '?' );
cmd.execute ( 'b', '?' );
cmd.execute ( 'b', 'x' );
Serial.println ( "DONE" );
}

void loop(){}


If I refactor Commands into Template.h and Foo.h (as below), I get four compile errors that I don't understand how to fix:



Severity Code Description File Line
Error error: invalid use of template-name 'Template' without an argument list D:DocumentsArduinoStructArrayFoo.h 6
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: 'myCommands' was not declared in this scope D:DocumentsArduinoStructArrayFoo.h 11
Error error: invalid use of template-name 'Foo' without an argument list D:DocumentsArduinoStructArrayStructArray.ino 7



Here is the code for Template.h:



#pragma once
template <class T>
class Template {
public:
typedef void ( T::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

void show ( char sel, char act ) {
Serial.print ( "SEL = " );
Serial.print ( sel );
Serial.print ( " ACT = " );
Serial.println ( act );
}

void execute ( char sel, char act, int cmdSize ) {
for (int i = 0; i < cmdSize; i++) {
if (T::myCommands[i].sel == sel && T::myCommands [i].act == act) {
Serial.println ( T::myCommands [i].desc );
( this->*T::myCommands [i].funcPtr )( sel, act );
return;
}
}
Serial.print ( F ( "Unknown SEL/ACT Pair:" ) );
Serial.print ( sel );
Serial.print ( '/' );
Serial.println ( act );
}
};


And Foo.h:



#pragma once
#include "Template.h"
template<class T>
class Foo {
public:
Template::command myCommands [2] = {
command { 'a','?',"FOO-A", &Foo::foo },
command { 'b','x',"FOO-B", &Foo::bar },
};

int cmdSize = sizeof ( myCommands ) / sizeof ( myCommands [0] );


void foo ( char sel, char act ) {
show ( { sel }, { act } );
}

void bar ( char sel, char act ) {
show ( { sel }, { act } );
}
};






c++ arrays templates struct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:10

























asked Nov 20 '18 at 21:59









Bob Jones

1,08152452




1,08152452








  • 2




    What's the error message?
    – Matthieu Brucher
    Nov 20 '18 at 22:00










  • #pragma once is only available to msvc, last i bothered to check many years ago.
    – johnathan
    Nov 20 '18 at 22:33










  • Did you mean FunctionPointer f; instead of void ( *FunctionPointer )( char, char ); ?
    – Jarod42
    Nov 20 '18 at 22:44














  • 2




    What's the error message?
    – Matthieu Brucher
    Nov 20 '18 at 22:00










  • #pragma once is only available to msvc, last i bothered to check many years ago.
    – johnathan
    Nov 20 '18 at 22:33










  • Did you mean FunctionPointer f; instead of void ( *FunctionPointer )( char, char ); ?
    – Jarod42
    Nov 20 '18 at 22:44








2




2




What's the error message?
– Matthieu Brucher
Nov 20 '18 at 22:00




What's the error message?
– Matthieu Brucher
Nov 20 '18 at 22:00












#pragma once is only available to msvc, last i bothered to check many years ago.
– johnathan
Nov 20 '18 at 22:33




#pragma once is only available to msvc, last i bothered to check many years ago.
– johnathan
Nov 20 '18 at 22:33












Did you mean FunctionPointer f; instead of void ( *FunctionPointer )( char, char ); ?
– Jarod42
Nov 20 '18 at 22:44




Did you mean FunctionPointer f; instead of void ( *FunctionPointer )( char, char ); ?
– Jarod42
Nov 20 '18 at 22:44












1 Answer
1






active

oldest

votes


















1














If I start you out on the non-templated version, you will see that you need to make some changes.



class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
{ 'a','?',"FOO", &Commands::foo },
{ 'b','x',"BAR", &Commands::bar }
};

int cmdSize = sizeof ( this->myCommands ) / sizeof ( this->myCommands [0] );

void foo ( char sel, char act ) {
char buf[2] = {sel};
Serial.println ( buf );
}

void bar ( char sel, char act ) {
char buf[2] = { sel };
Serial.println ( buf );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
(this->*myCommands [i].funcPtr)( sel, act );
}
}
}

};


Once that is solved you can attack the templating (which I didn't see the purpose right now in this example since the foo and bar were part of the template class.)






share|improve this answer





















  • My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
    – Bob Jones
    Nov 21 '18 at 19:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402232%2ftemplates-and-array-of-structs-that-contain-pointer-to-method-in-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If I start you out on the non-templated version, you will see that you need to make some changes.



class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
{ 'a','?',"FOO", &Commands::foo },
{ 'b','x',"BAR", &Commands::bar }
};

int cmdSize = sizeof ( this->myCommands ) / sizeof ( this->myCommands [0] );

void foo ( char sel, char act ) {
char buf[2] = {sel};
Serial.println ( buf );
}

void bar ( char sel, char act ) {
char buf[2] = { sel };
Serial.println ( buf );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
(this->*myCommands [i].funcPtr)( sel, act );
}
}
}

};


Once that is solved you can attack the templating (which I didn't see the purpose right now in this example since the foo and bar were part of the template class.)






share|improve this answer





















  • My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
    – Bob Jones
    Nov 21 '18 at 19:14
















1














If I start you out on the non-templated version, you will see that you need to make some changes.



class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
{ 'a','?',"FOO", &Commands::foo },
{ 'b','x',"BAR", &Commands::bar }
};

int cmdSize = sizeof ( this->myCommands ) / sizeof ( this->myCommands [0] );

void foo ( char sel, char act ) {
char buf[2] = {sel};
Serial.println ( buf );
}

void bar ( char sel, char act ) {
char buf[2] = { sel };
Serial.println ( buf );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
(this->*myCommands [i].funcPtr)( sel, act );
}
}
}

};


Once that is solved you can attack the templating (which I didn't see the purpose right now in this example since the foo and bar were part of the template class.)






share|improve this answer





















  • My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
    – Bob Jones
    Nov 21 '18 at 19:14














1












1








1






If I start you out on the non-templated version, you will see that you need to make some changes.



class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
{ 'a','?',"FOO", &Commands::foo },
{ 'b','x',"BAR", &Commands::bar }
};

int cmdSize = sizeof ( this->myCommands ) / sizeof ( this->myCommands [0] );

void foo ( char sel, char act ) {
char buf[2] = {sel};
Serial.println ( buf );
}

void bar ( char sel, char act ) {
char buf[2] = { sel };
Serial.println ( buf );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
(this->*myCommands [i].funcPtr)( sel, act );
}
}
}

};


Once that is solved you can attack the templating (which I didn't see the purpose right now in this example since the foo and bar were part of the template class.)






share|improve this answer












If I start you out on the non-templated version, you will see that you need to make some changes.



class Commands {
public:
typedef void ( Commands::*FunctionPointer )( char, char );

struct command {
char sel;
char act;
char const *desc;
FunctionPointer funcPtr;
};

command myCommands [2] = {
{ 'a','?',"FOO", &Commands::foo },
{ 'b','x',"BAR", &Commands::bar }
};

int cmdSize = sizeof ( this->myCommands ) / sizeof ( this->myCommands [0] );

void foo ( char sel, char act ) {
char buf[2] = {sel};
Serial.println ( buf );
}

void bar ( char sel, char act ) {
char buf[2] = { sel };
Serial.println ( buf );
}

void execute ( char sel, char act ) {
for (int i = 0; i < cmdSize; i++) {
if (myCommands [i].sel == sel && myCommands [i].act == act) {
Serial.println ( myCommands [i].desc );
(this->*myCommands [i].funcPtr)( sel, act );
}
}
}

};


Once that is solved you can attack the templating (which I didn't see the purpose right now in this example since the foo and bar were part of the template class.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 22:52









Bo R

616110




616110












  • My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
    – Bob Jones
    Nov 21 '18 at 19:14


















  • My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
    – Bob Jones
    Nov 21 '18 at 19:14
















My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
– Bob Jones
Nov 21 '18 at 19:14




My goal is to have the typedef, the Command struct definition, the execute and show methods in a single class and to have multiple classes, each with its own set of commands and command handling functions, but I am still trying to GROK Templates and am missing some important details, as shown by the compiler error messages.
– Bob Jones
Nov 21 '18 at 19:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402232%2ftemplates-and-array-of-structs-that-contain-pointer-to-method-in-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga