#include .h and extern


hello  :)  :)  :)  :)

i'm newbies in ide arduino
 
file .ino

code: [select]


#include ex.h

const int = 0 ;

foo snmp ;

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

}

void loop() {
  // put main code here, run repeatedly:
 snmp.test () ;
}



file ex.h
code: [select]


#ifndef ex_h
#define ex_h


class foo {
 void test () ;
} ;

#endif



file ex.cpp
code: [select]


#include <arduino.h>

 void foo::test {
  = 0 ;  <--- error " not delared in scope "
 }



in c++ , use extern in .h off project ..
with project .ino ???

thank lot !!!!

there many things wrong here don't know start....

let's start here:
code: [select]

#include ex.h

that's not how include something. try
code: [select]

#include "ex.h"


next, ex.cpp needs include ex.h.

and ex.h needs include arduino.h

code: [select]

void foo::test {

is not how define function. try
code: [select]

void foo::test(){



and because it's private , declare in ex.h
code: [select]

public:
  void foo::test();


code: [select]

const int = 0 ;
//....
a = 0 ;

how think can assign value const? that's whole idea of const, can't...

then off more complex matters. ex.cpp compiled separate of file.ino. has therefor no knowledge it's use in file.ino , variables there. if want indeed change variable on there need tell compiler exist putting in ex.h
code: [select]

extern int a; //and have make a int instead of const int because can never work.


but really, why want this. it's ugly way of doing it. class can work on single variable a, no matter how many objects of class foo have. , relies on declaring variable in sketch. why not part of object? in ex.h
code: [select]

class foo {
  public:
    void test ();
  protected:
    int a; //which needs better name
} ;


and if want variable in sketch (because variable has no 1 1 relation object, pass reference. google see means ;)

in ex.h
code: [select]

class foo {
 public:
   void test (int& a) ;
} ;

in ex.cpp
code: [select]

void foo::test(int& a) {
  a = 0 ;
}


Arduino Forum > Using Arduino > Programming Questions > #include .h and extern


arduino

Comments

Popular posts from this blog

Help needed for choosing soldering station

Error Message when accessing Adobe

Fuelino for Arduino Nano - motorcycle Fuel Injection control and data logger