#include .h and extern

hello :slight_smile: :slight_smile: :slight_smile: :slight_smile:

I'm newbies in IDE Arduino

file .ino

#include ex.h

const int a = 0 ;

foo snmp ;

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

}

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

file ex.h

#ifndef ex_h
#define ex_h


class foo {
 void test () ;
} ;

#endif

file ex.cpp

#include <Arduino.h>

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

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

Thank a lot for your help !!!!

There are so many things wrong here I don't know where to start....

Let's start here:

#include ex.h

That's not how you include something. Try

#include "ex.h"

Next, the ex.cpp also needs to include the ex.h.

And ex.h needs to include Arduino.h

void foo::test {

Is not how you define a function. Try

void foo::test(){

And because now it's private and you declare it in ex.h like

public:
  void foo::test();
const int a = 0 ;
//....
a = 0 ;

How do you think you can assign a value to a const? That's the whole idea of a const, you can't...

Then off to the more complex matters. ex.cpp is compiled completely separate of file.ino. It has therefor no knowledge about it's use in file.ino and what variables are there. If you want to indeed change a variable over there you need to tell the compiler it will exist by putting in the ex.h

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

But really, why do you want to do this. It's a very ugly way of doing it. The class can now only work on that single variable a, no matter how many objects of class foo you have. And it relies on YOU declaring variable a in your sketch. Why is a not part of the object? So in ex.h

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

And if you really want to do something with a variable in the sketch (because the variable has no 1 to 1 relation with the object, pass it by reference. Google it to see what it means :wink:

in ex.h

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

In ex.cpp

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

Excuse me for a lot of error ... This is for example . -:frowning:

i would like to access to a global variable on .ino since another .cpp ... where are you put : extern int a; ???

It's very very difficult to speak English for me !!!!

thank a lot

parabole:
Excuse me for a lot of error ... This is for example . -:frowning:

If you make examples with a lot of extra errors it's hard to find the error you're looking for :wink:

parabole:
i would like to access to a global variable on .ino since another .cpp ... where are you put : extern int a; ???

But why? In 99% of the cases that's a pretty ugly way of doing it. Variable a as part of the class or passed by reference makes more sense.

parabole:
It's very very difficult to speak English for me !!!!

What's you native language? If my replies are to hard to understand, maybe try one of the international boards (at the bottom of the boards list) if it exists in your native language.

thank

french ....