struct and functions

Hi all,
Had a little experience with arduino, however I’m getting stuck on something Ive not done before.
Can anyone help me out?

I have a struct like this:

typedef struct {
 unsigned long address; // CAN address
 byte data[7]; // Data array
 unsigned int length; // Length of data
 unsigned long repetition; // Repetition time in ms
 unsigned long lastmessage; // Millis time of last message
 boolean active; // Whether message is active
} CANMsg;

Then I want to create a function that uses the type above. Something like this:

void changeCANBit(CANMsg canMessage, int BytePosition, int BitPosition, int NewBit) {
 switch ((canMessage.data[BytePosition] & (1 << BitPosition)) >> BitPosition) {
 case 0:
 switch (NewBit) {
 case 0:
 // No change - already set as wanted
 break;
 case 1:
 canMessage.data[BytePosition] = (canMessage.data[BytePosition] + (1 << BitPosition));
 break;
 }
 break;
 case 1:
 switch (NewBit) {
 case 0:
 canMessage.data[BytePosition] = (canMessage.data[BytePosition] - (1 << BitPosition));
 break;
 case 1:
 // No change - already set as wanted
 break;
 }
 }
}

I am getting complile errors like this:

Compiling 'TestINO' for 'Arduino/Genuino Zero (Native USB Port)'
 
TestINO.ino: 35:19: error: variable or field 'changeCANBit' declared void
   unsigned int length;   \\ Length of data
 
TestINO.ino: 35:19: error: 'CANMsg' was not declared in this scope
 
TestINO.ino: 35:38: error: expected primary-expression before 'int
   unsigned int length;   \\ Length of data
 
TestINO.ino: 35:56: error: expected primary-expression before 'int
   unsigned int length;   \\ Length of data
 
Error compiling project sources
TestINO.ino: 35:73: error: expected primary-expression before 'int
   unsigned int length;   \\ Length of data
 
TestINO.ino: In function void mainProg()
 
TestINO.ino: 459:33: error: 'changeCANBit' was not declared in this scope
   changeCANBit(canOut1, 0, 4, 1)
 
TestINO.ino: 469:33: error: 'changeCANBit' was not declared in this scope
   changeCANBit(canOut1, 0, 4, 0)
 
TestINO.ino: 482:33: error: 'changeCANBit' was not declared in this scope
   changeCANBit(canOut1, 0, 4, 1)
 
TestINO.ino: 491:33: error: 'changeCANBit' was not declared in this scope
   changeCANBit(canOut1, 0, 4, 0)
Build failed for project 'TestINO'

In my sketch I have the following:
Declarations:

CANMsg canOut1;

In setup:

	canOut1.address = 0x1CCC0101;
	canOut1.repetition = 100;
	canOut1.length = 8;
	canOut1.active = true;

Then using the function:

changeCANBit(canOut1, 0, 4, 1);

Thanks!

Please post a complete program showing the problem.

I think we may need to see the whole sketch and headers (if any)

459:33: error: 'changeCANBit' was not declared in this scope

Suggests there was no prototype or correctly formed declaration / call...
Was the CANMsg struct instance ever created before it was used ??

No line numbers gets harder.

OK I dropped the changeCANBit function above the setup routine, that has solved the last few errors.
I still get these errors though:

Compiling 'TestINO' for 'Arduino/Genuino Zero (Native USB Port)'
 
TestINO.ino: 26:19: error: variable or field 'changeCANBit' declared void
   byte VIN[16]
 
TestINO.ino: 26:19: error: 'CANMsg' was not declared in this scope
 
TestINO.ino: 26:38: error: expected primary-expression before 'int
   byte VIN[16]
 
TestINO.ino: 26:56: error: expected primary-expression before 'int
Error compiling project sources
Build failed for project 'TestINO'
   byte VIN[16]
 
TestINO.ino: 26:73: error: expected primary-expression before 'int
   byte VIN[16]

Im just seeing if I can strip some code out as its quite huge!
Ill replicate the error in something smaller before posting here

Don't strip, try to make a simplified sketch to show the problem. Spoiler, most of the time you'll find the error while doing so :wink:

Im reworking my code to use a .h file.
Since putting the other functions in here it all sems to be working so far!

Your struct definition is incorrect.
Please read this:
http://www.cplusplus.com/doc/tutorial/structures/

You're create an unnamed struct, then instantiating a single object 'CANMsg'. It is an object/instance, not a type name.

For example:
http://cpp.sh/7yr24

Pieter

Read it, also read about "typedef" on the same website.

The OP defined a type, at least that's what C programmers see. cplusplus.com sez if you want you can, so tell me more.

What does typedef mean/do in the OP's fragment?

a7

I stand corrected then.

In C++, the 'typedef' is implicit when defining a struct, so normally, you don't type it.

septillion:
Don't strip, try to make a simplified sketch to show the problem. Spoiler, most of the time you'll find the error while doing so :wink:

MCVE should be required reading before being allowed to post a question.

You cannot modify the CANMsg struct in the method when the argument is declared as "CANMsg canMessage", you either need to declare it as "CANMsg &canMessage" (variable reference) or as "CANMsg *canMessage" (pointer reference).

milesfrankland:
I have a struct like this:
Then I want to create a function that uses the type above. Something like this:

This would be a good time to learn a little object oriented programming. Just one small step will get you started.