1

ISSUE

When i am iterating through an array of pointers to functions i am always getting an error in this example case of code. But if i wont iterate through them everything work fine as it ca be seen by the code below too.

Code

class OutterClass {
private:
   
  class InnerClass{
    public:
    
    InnerClass(){}
    
    float Func1 (const float &x ) { return (x > 0) ? x : 0            ;}
    float Func2 (const float &x ) { return 1 / (1 + exp(-x))          ;}
    float Func3 (const float &x ) { return (x > 0) ? x : (exp(x) - 1) ;}

    void test_ISSUE(){  
      //this doesn't work
      /*
      for(int i=0; i<3; i++)
        Serial.println(((this)->*(Function_ptrs)[i])(2));
      */

      // this works
      Serial.println(((this)->*(Function_ptrs)[0])(2));
      Serial.println(((this)->*(Function_ptrs)[1])(2));
      Serial.println(((this)->*(Function_ptrs)[2])(2));
      
    }  
    
  };
  typedef float (InnerClass::*method_function) (const float &);
  static const method_function (Function_ptrs)[3] = {
    &InnerClass::Func1,
    &InnerClass::Func2,
    &InnerClass::Func3
  };
  
public:
  
  InnerClass *Reference;
  OutterClass(){ Reference = new InnerClass();}  
};


void setup() { 
  Serial.begin(9600); 
  
  OutterClass *Obj = new OutterClass();
  Obj->Reference->test_ISSUE();
}

void loop () {}

Recreating the Error

just uncomment for(int i=0; i<3; i++) ...

Error

...: In function `test_ISSUE':

.../sketch_jan23a.ino:17: undefined reference to `OutterClass::Function_ptrs'
.../sketch_jan23a.ino:17: undefined reference to `OutterClass::Function_ptrs'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Uno.

Any idea?

UPDATE SOLUTION

just used inline static const method_function (Function_ptrs)[3] as stated here and now it works..

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
Giorgos Xou
  • 1,461
  • 1
  • 13
  • 32
  • `static const method_function(Function_ptrs)[3]`-> `static constexpr method_function(Function_ptrs)[3]` – Jabberwocky Jan 24 '21 at 17:55
  • This question shouldn't have been closed. It has been closed once, then I voted to reopen, it was reopened, and now it's closed again, but I can't vote to reopen anymore... – Jabberwocky Jan 24 '21 at 17:58
  • No, this question should be properly closed as a duplicate of countless other questions about the same thing, that were already asked over the course of many, many years. It should also not be tagged as C, since the shown code is C++. Fixed. – Sam Varshavchik Jan 24 '21 at 18:15
  • @SamVarshavchik about 'c' tag, my falt, i forgot to remove it from previous question saved, thanks you. – Giorgos Xou Jan 24 '21 at 18:18
  • @SamVarshavchik But closing a question i believe it's rude towords something like this, should i have said that i am not familiar with c++ for them to not close my question? or should i had tried even more to find a solution by myself? I might be wrong but it feels like it is more like closing the "doors" for someone to make sense and learn rather than [...] – Giorgos Xou Jan 24 '21 at 18:22
  • 1
    A substantially identical question was asked and answered. There's nothing that can be learned that can't be learned from reading the duplicate question. When a substantially identical question was already asked before, Stackoverflow's guidelines indicate that the question should be closed as a duplicate, as such. This is not a reflection, or any kind of a judgement of a duplicate question. You do not lose any reputation merely for asking a duplicate question. – Sam Varshavchik Jan 24 '21 at 18:29
  • @SamVarshavchik I don't disagree, because this is your thinking, and that's how you think. But, you have to understand (if you want of course) that this is not how I think and how i learn or how I understand things. And i think that this should had been respected (because still i havent grasped the whole idea [...]) – Giorgos Xou Jan 24 '21 at 18:39
  • @GiorgosXou Closing a question is nothing personal against the asker, and it is not necessarily critical of the question. Specially a duplicate question, the information you need is already available and a link to it is provided. Please find the link to the duplicate question at the top of the question. If the answer is not clear to you, you can try to ask for clarification there. Closing a question is not disrespectful, and it is done according to official guidelines and is not a matter of opinion. – François Andrieux Jan 24 '21 at 18:40
  • Voting to reopen isn't it an official guideline too? because @Jabberwocky something said a moment ago.... but nvm, peace&love! – Giorgos Xou Jan 24 '21 at 18:45
  • Sure, anyone can vote to reopen, everyone is free to vote in whichever way they think is more appropriate. Someone voted to reopen the question after it was closed, then someone else closed the question again. If, in the end, there are more close votes than reopen votes the question remains closed. – Sam Varshavchik Jan 24 '21 at 18:50
  • Anyways i am sorry ... but i just got mad because i had 2 days now trying to find a way to fix this simple thing and when i posted here, i got closed with a reference that i had to understand by myself and that pissed me off – Giorgos Xou Jan 24 '21 at 18:51
  • C++ is the most complicated general purpose programming language in use today. It takes a long time to learn it. Very long time. You have nothing to apologize for. Just allow yourself plenty of time to figure everything out. – Sam Varshavchik Jan 24 '21 at 19:50
  • @GiorgosXou my first comment is the answer actually. – Jabberwocky Jan 24 '21 at 20:18
  • oh, i didn't realise it, thanks – Giorgos Xou Jan 24 '21 at 21:10
  • @Jabberwocky actually static constexpr doesn't seem to solve it for me, but whatever – Giorgos Xou Jan 24 '21 at 21:20

0 Answers0