LusoRobótica - Robótica em Português

Robótica => Iniciantes => Tópico iniciado por: OxyGen em 01 de Julho de 2010, 14:54

Título: Classes
Enviado por: OxyGen em 01 de Julho de 2010, 14:54
Boas a todos...

Estou com um pequeno problema: não consigo criar um tipo de dados (class) para trabalhar com o Arduino.

Supostamente em java seria assim:

Código: [Seleccione]
public Class Medicamento
{
int codigo;
char nome;
}[color=red];[/color]


em C não faço a mínima ideia de como fazer... está-me sempre a dar um erro "unqualified-id at end of input"... Alguma ajuda sff...

EDIT: Já descobri... faltava ";" no final da ultima chaveta...

Cumprimentos
Título: Re: Classes
Enviado por: Njay em 01 de Julho de 2010, 17:06
Já agora, em C não tens "classes" no "verdadeiro" sentido da palavra. Em C++ é que tens classes (quando programas no ambiente do Arduino estás a programar em C++).
Título: Re: Classes
Enviado por: OxyGen em 01 de Julho de 2010, 19:17
Então se eu quiser fazer uma coisa do tipo:

Código: [Seleccione]
typedef struct _Teste
{
  int position;
  int frequency;
}Teste;

int ler(Teste* k, int cont)
{
  return cont;
}

static int cont=1;

int main()
{
  Teste novo[12];
  cont=ler(novo, cont);

  return 0;
}

Como faço?

Já agora, não percebo porquê mas este código dá erro:
Citar
error: 'Teste' was not declared in this scope In function 'int ler(Teste*, int)':
In function 'int main()':
Bad error line: -3
Título: Re: Classes
Enviado por: Njay em 01 de Julho de 2010, 22:52
Tenta remover o _ do _Teste.

Como fazes o quê? Esse código mas usando classes?
Título: Re: Classes
Enviado por: OxyGen em 01 de Julho de 2010, 23:10
ja experimentei com e sem 'underscore'... Eu sei fazer isto em java... que seria mais ou menos assim:

Código: [Seleccione]
Class Teste{

int exp;
int txt;
}

Teste[] novo = new Teste[100]; // Construtor
int contador=0;

//passar argumentos para as funções:

ler(Teste,contador);

static void ler(Teste[] k , int a)
{
}
Título: Re: Classes
Enviado por: preytender em 01 de Julho de 2010, 23:17
Assim já funciona, não sei porque é que o typef não funciona mas se quiseres.


Código: [Seleccione]
typedef struct _Teste{
  int position;
  int frequency;
}Teste;

int ler(struct _Teste *k, int cont){
  return cont;
}

static int cont=1;

int main(){
  struct _Teste novo[12];
  cont=ler(novo, cont);
  return 0;
}

Abraço.
Título: Re: Classes
Enviado por: OxyGen em 01 de Julho de 2010, 23:38
Aí é que está... em java não é preciso ponteiros para nada... apenas trabalhas com indices...
Título: Re: Classes
Enviado por: preytender em 01 de Julho de 2010, 23:44
 ;D Estou mais habituado a vb.
Na tua funcao main tambem tens um array de elementos que podes utilizar com indices... apontadores são bons para quando não temos noção do numero de elementos da coleção.

Aqui vai a explicação de como utilizar typedef no arduino se quiseres mesmo muito.
 
"You need to put type definition inside a separate header file, as the function prototypes that are generated by the IDE are inserted above the typedef (but below pre-processor directives like #include).  This header can be another tab in your sketch; use the right facing arrow in the upper right of the IDE. "

Tambem não sabia mas pelo que vimos parece ser verdade.  :o
Abraço.
Título: Re: Classes
Enviado por: OxyGen em 02 de Julho de 2010, 10:41
Pelos vistos... no forum do arduino está lá isto:
Citar
Your original file is absolutely correct C (and C++) code and can be compiled (by itself) by any reasonable compiler.

However, if you want to compile it from inside the Arduino environment, it's probably easiest to do it the "Arduino way."

Create a library and put the struct definition there.

If you don't already have a subdirectory named libraries in your sketchbook drectory create one.  Then, in that directory create a new subdirectory, say teststruct  Put your struct definition in a file named teststruct.h in the teststruct directory.  Delete the struct definition from your main .pde file.

Then in the Arduino environment, import that library.  It will put #include <teststruct.h> at the top of your sketch (the .pde file).  Then the C++ file that actually gets compiled will have things defined in correct sequence to make the compiler happy.

Vou tentar fazer isto e ja digo algo...