Change object values from a function

Hello, I have a class that have to parameters X, Y. In the class there are SetX funcion to set new value to X, and SetY funcion to set new value to Y. But there is a problem. Every time i use the Set function from the main Loop it work's good, but when im trying use it in other function it works only when the other function is working and when the function end the X is not changing...

Thank's for anyone that could help :wink:

Then show us that code. This text alone is gibberish…

septillion: Then show us that code. This text alone is gibberish...

This is the h file:

#ifndef point_h
#define point_h

#include "Arduino.h"
class point{
  public:
    point(byte _x, byte _y);
    
    void SetPoint(byte Newx, byte Newy);
    byte GetX();
    byte GetY();
    void SetX(byte _x);
    void SetY(byte _y);
    
  private:
    byte x, y;
};
#endif

This is the cpp file:

#include "point.h"
point::point(byte _x, byte _y){
  x = _x;
  y = _y;
  
}
byte GetX(){
  return x;
}
byte GetY(){
  return y;
}
void SetX(byte _x){
  x = _x;
}
void SetY(byte _y){
  y = _y;
}

and this is the main file:

// here it's not changing the value
void MovePoint(point p){
  byte c = 0;
  while(c != 5){
    byte x = p.GetX();
    p.SetX(x++);
    c++
  }
}
void setup() {
  // here it's changing the value
  byte x = p.GetX();
  p.SetX(x++);
}

void loop() {

}
    c++

Missing something? Here, I've got an endless supply - ;;;;;;;;;;;;;;;;;;;;;;;;;

// here it's not changing the value

And you call that function where?

That main file (aka your sketch) doesn't make sense. For starters, it doesn't include your library. Then it never calls the function MovePoint() and in setup you try to do stuff on an object p which does not exist...

So if you really want help you should stop posting nonsense like this.

PS You also got standards mixed up... Classes start with a Capital, methods and functions don't. And it's more normal to make class variables start with a underscore and local (parameters) not.

PaulS:     c++

Missing something? Here, I've got an endless supply - ;;;;;;;;;;;;;;;;;;;;;;;;;

// here it's not changing the value

And you call that function where?

septillion: That main file (aka your sketch) doesn't make sense. For starters, it doesn't include your library. Then it never calls the function MovePoint() and in setup you try to do stuff on an object p which does not exist...

So if you really want help you should stop posting nonsense like this.

PS You also got standards mixed up... Classes start with a Capital, methods and functions don't. And it's more normal to make class variables start with a underscore and local (parameters) not.

Sorry, it's just an example, i fixed it but it's still not working anyway...

the h flie:

#ifndef point_h
#define point_h

#include "Arduino.h"
class point{
  public:
    point(byte _x, byte _y);
    
    void SetPoint(byte Newx, byte Newy);
    byte GetX();
    byte GetY();
    void SetX(byte _x);
    void SetY(byte _y);
    
  private:
    byte x, y;
};
#endif

this is the cpp file:

#include "point.h"
point::point(byte _x, byte _y){
  x = _x;
  y = _y;
  
}
byte GetX(){
  return x;
}
byte GetY(){
  return y;
}
void SetX(byte _x){
  x = _x;
}
void SetY(byte _y){
  y = _y;
}

and this is the main code:

void MovePoint(point p){
  byte c = 0;
  while(c != 5){
    byte x = p.GetX();
    p.SetX(x++);
    c++;
  }
}
void setup() {
  // Works:
  byte num = p.GetX();
  p.SetX(num + 1);

  //Doesn't work:
  MovePoint(p);
}

void loop() {

}

And agian, im sorry, this example just dememstate my problem with class that i have in my project, so i wrote it quicky without notice the mistakes...

You have no way of knowing that the code "doesn't work". On what are you basing this wild-ass notion that the code doesn't work?

PaulS: You have no way of knowing that the code "doesn't work". On what are you basing this wild-ass notion that the code doesn't work?

I checked it a moment ago... If you don't have something smart to say then don't say it

I checked it a moment ago...

How? There are no Serial.print() statements. You do NOT know what the code is doing.

PaulS: How? There are no Serial.print() statements. You do NOT know what the code is doing.

It's not rellevant code, so i took it down before i upload it here, I have no idea why do you think I would lie about it... And I don't know what's your problem with me, but if you looking to troll or disturb, grow up and stop it.

Where do you declare your point object?

At first glance the problem is the fact that your function MovePoint creates a local copy of object p, while in Setup you work directly on the object. Thats assuming that your Point is in global namespace and that by "not changing" you mean checking it somewhere in loop, or in setup.

Disty: Where do you declare your point object?

At first glance the problem is the fact that your function MovePoint creates a local copy of object p, while in Setup you work directly on the object. Thats assuming that your Point is in global namespace and that by "not changing" you mean checking it somewhere in loop, or in setup.

I did an object from the class point called p just above the function as global variable but now i see i forgot to copy it to here.. anyway he is there.

The problem is that it doesn't change p value in the function, only in the setup and i don't understand why.

I didn't really understand your answer, what do you mean by saying that the function MovePoint creates a local copy of object p, while in Setup you work directly on the object?

Just step back for a moment, let all those object be

byte x = 10;

void changeX(byte x){
  x = 100;
}

void setup(){
  Serial.begin(115200);
  changeX(x);
  
  Serial.print(F("And x is: "));
  Serial.println(x);
}

void loop(){
}

After that, why do you expect to print?

The way you pass your point to MovePoint creates a local copy (existing just in function) of point - no changes are made to your point in global namespace. If you want your MovePoint to be able to modify your point you need to pass it by reference.

void MovePoint(point& p) // This can modify your point, as you pass it by reference

septillion: Just step back for a moment, let all those object be

byte x = 10;

void changeX(byte x){  x = 100; }

void setup(){  Serial.begin(115200);  changeX(x);    Serial.print(F("And x is: "));  Serial.println(x); }

void loop(){ }




After that, why do you expect to print?

100

Disty: The way you pass your point to MovePoint creates a local copy (existing just in function) of point - no changes are made to your point in global namespace. If you want your MovePoint to be able to modify your point you need to pass it by reference.

void MovePoint(point& p) // This can modify your point, as you pass it by reference

I have just checked it and it's working. I really thank you, im suck all this week with this problem

*Thanks anyone who helped 8)

oron: 100

Then you expectation is wrong! Try it ;)

Read a bit about variable scope :)

septillion: Then you expectation is wrong! Try it ;)

Read a bit about variable scope :)

Ohh it really didn't work :wink: I have many syntex problems becaues im still new in arduino, I usually work with C# so it's a bit differnt.

thank you for your helping man :slight_smile: