Problem with structures

Hello,

I recently start to use IDE above 1 version, till now i have used 023 version of Arduino IDE. The following code works on 023 but doesn't on 1.0.4 and i will appreciate if someone will explain why and how to fix the problem

after the includes and defines at the begining of the sketch i have

typedef struct TextVal
{
  uint16_t Value;//crt value
  uint16_t MaxVal;//maximum value for the setting
};

then i use the structure in one array

TextVal ROM_Shadow[52];

and this array is used in several functions like the one below

/////////////////////////////////////////////////////////
//Description: Updates DigiText Object value
//Parameters: TextVal structure
//Returns: None
/////////////////////////////////////////////////////////
void UpdateDigi(TextVal &MyVal)
{
  if(CrtDigi == 255)
    return;
  if(Digi[CrtDigi].GetDigiBorder())
  {
    Digi[CrtDigi].SetDigiValue(MyVal.Value);
    if(ROM_Shadow[ROM_Buzz].Value)
      Serial.print('S');//Play Beep
  }
}

when compiling i get errors for all the functions similar with the one above

GVI_Remote_Touch:98: error: variable or field 'UpdateDigi' declared void GVI_Remote_Touch:98: error: 'TextVal' was not declared in this scope GVI_Remote_Touch:98: error: 'MyVal' was not declared in this scope GVI_Remote_Touch:99: error: variable or field 'SmallPlus' declared void GVI_Remote_Touch:99: error: 'TextVal' was not declared in this scope GVI_Remote_Touch:99: error: 'MyVal' was not declared in this scope

Thanks for your help

Your typedef statement looks wrong to me. The typedef name needs to go at the end.

For your declaration, try

struct TextVal  Rom_shadow[52];

and see if that works.

I'd also declare that function with argument type struct TextVal* and give it a pointer to the structure instance rather than copy the whole structure.

after a lot of searches i Found the solution

  1. create a Header file and put the structure definition in it (h file in the same folder as the sketch)
#ifndef Functions_h
#define Functions_h

typedef struct TextVal
{
  uint16_t Value;//crt value
  uint16_t MaxVal;//maximum value for the setting
};
#endif

the in the main code add

#include "Functions.h"

and it works...

You can make a function prototype and then it compiles in one file. This compiles OK for example:

typedef struct TextVal
{
  uint16_t Value;//crt value
  uint16_t MaxVal;//maximum value for the setting
};

TextVal ROM_Shadow[52];

void UpdateDigi(TextVal &MyVal);  // <----- add this

void UpdateDigi(TextVal &MyVal)
{
// whatever
}

void setup () { }
void loop () { }

Thanks Nick, i will try this.

I'd also declare that function with argument type struct TextVal* and give it a pointer to the structure instance rather than copy the whole structure.

michinyon, can you please explain in detail this

[quote author=Nick Gammon link=topic=156263.msg1170848#msg1170848 date=1364163621] You can make a function prototype and then it compiles in one file. This compiles OK for example: [/quote]

Is that behaviour version-specific? With 1.0.1 I seem to get the autogenerated prototypes added (in the wrong place) for all functions regardless of whether I included my own prototypes, so it's not possible to use user-defined types in a function signature unless the types are declared in a header file. (Your example won't compile for me.)

Yes it's a new feature. 1.0.3 onwards, I think.

Thanks Nick,
Your suggestion works