Multiple Definition Error - Definition Guards

Hello All,

I am trying to write a program for my Arduino Nano with the following structure: A mastermind .ino file which calls functions from a number of .cpp/.h files in the same directory. So far I have been trying to get my testing mastermind file to include one of my testing .cpp/.h files, and have run into some issues, I think because the .h file for my secondary file uses an array of function pointers.

The file names are Testing3.ino, t3p0.cpp, and t3p0.h

I ran into a bunch of issues getting my array of function pointers working, and finally found a method (by reading a number of forums, this one included) that I believe will work. The issue I have now is that when I try to compile my master file, I get a multiple definition error for my array of function pointers.

The error seems like the kind I would expect if I did not have include guards in my t3p0.h file, which I do have. I have included code snippets of the method for creating an array of function pointers that I believe should work, as well as a screen shot of the error method I get when I attempt to compile. I have deleted the other (commented out) methods I tried to use for creating an array of function pointers, but I can provide them if they may be of assistance.

Right now the code doesn't actually do anything, that will come later. I am adapting code I already wrote to work in a method I find more elegant/less hacky.

Testing3.ino:

#include "t3p0.h"

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

t3p0.cpp:

#include "t3p0.h"






void pat0::p0_s0(){

}

void pat0::p0_s1(){

}

void pat0::p0_s2(){

}

t3p0.h:

/*
 * t3p0.h
 *
 *  Created on: Dec 13, 2018
 *      Author: 
 */

#ifndef T3P0_H_
#define T3P0_H_

class pat0
{

    public:

        void p0_s0(); // Function for state 0 of pattern 0.

        void p0_s1(); // Function for state 1 of pattern 0.

        void p0_s2(); // Function for state 2 of pattern 0.


};

        typedef void (pat0::*pat0fn)(); //typedef used for an array of function pointers within pat0 class

        pat0fn pat0_array[3] = {&pat0::p0_s0, &pat0::p0_s1, &pat0::p0_s2}; // array of function pointers within
        //pat0 class.



#endif /* T3P0_H_ */

and finally the error I get when attempting to compile (attached):

Has anyone encountered this before? I feel like I am missing something very basic but haven't been able to find a similar enough situation on this and other forums.
I very much appreciate any help and insight folks can offer.

Please post errors on your post or attach as text file.

My cell phone does not want to display your image, not your fault.

I'm not a C++ programmer but can it be that some methods are outside the class (misplaced }) in your .h file?

Array should only be DECLARED in the .h file (as extern). It should be DEFINED in the .cpp file.

sterretje:
Please post errors on your post or attach as text file.

My cell phone does not want to display your image, not your fault.

I'm not a C++ programmer but can it be that some methods are outside the class (misplaced }) in your .h file?

Apologies, and thanks for the feedback. I've posted the error message below.

sketch\t3p0.cpp.o (symbol from plugin): In function `pat0::p0_s0()':

(.text+0x0): multiple definition of `pat0_array'

sketch\Testing3.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Nano.

gfvalvo:
Array should only be DECLARED in the .h file (as extern). It should be DEFINED in the .cpp file.

Thanks so much! That makes a ton of sense. I think that fixed it. Here are the changes I made:

/*
 * t3p0.h
 *
 *  Created on: Dec 13, 2018
 *      Author: 
 */

#ifndef T3P0_H_
#define T3P0_H_

class pat0
{

    public:

        void p0_s0(); // Function for state 0 of pattern 0.

        void p0_s1(); // Function for state 1 of pattern 0.

        void p0_s2(); // Function for state 2 of pattern 0.


};

        typedef void (pat0::*pat0fn)();

      
        extern pat0fn pat0_array[3];


#endif /* T3P0_H_ */

t3p0.cpp:

#include "t3p0.h"


pat0fn pat0_array[3] = {&pat0::p0_s0, &pat0::p0_s1, &pat0::p0_s2};  




void pat0::p0_s0(){

}

void pat0::p0_s1(){

}

void pat0::p0_s2(){

}

No changes made to Testing3.ino.

I haven't tested it with more than compiling, but it compiled. Does that look correct?

This is similar to how I defined the functions that will be my states in the .h file, but will be writing them in my .cpp file, right? Does the wrong way cause the multiple definitions error because the array is an array of functions defined in that same file? Does that sound accurate?

Thanks again!

The calling syntax is a little weird:

pat0 Pat0;
pat0 * Pat0_p = &Pat0;

void setup()
{
  (Pat0 .* pat0_array[0])();
  (Pat0 .* pat0_array[1])();
  (Pat0 .* pat0_array[2])();

  (Pat0_p ->* pat0_array[0])();
  (Pat0_p ->* pat0_array[1])();
  (Pat0_p ->* pat0_array[2])();
}

johnwasser:
The calling syntax is a little weird:

pat0 Pat0;

pat0 * Pat0_p = &Pat0;

void setup()
{
  (Pat0 .* pat0_array[0])();
  (Pat0 .* pat0_array[1])();
  (Pat0 .* pat0_array[2])();

(Pat0_p ->* pat0_array[0])();
  (Pat0_p ->* pat0_array[1])();
  (Pat0_p ->* pat0_array[2])();
}

Thanks! Just so I'm clear, are you writing this in the .ino master file? I was under the impression that I don't need/shouldn't have setup{}; in my .cpp file, and that was just an Arduino thing. Is this incorrect?

nsbernar:
Just so I'm clear, are you writing this in the .ino master file?

Yes.