简体   繁体   中英

Reach main.cpp pointers from a class C++

I am new on C++. I am writing libraries that contains .h and .cpp files for my Arduino. I have a question about that and i will try explain.

I have a main.cpp file which is contains all code inside. After i created Class A. I declare a float variable var1, and char array variable var2[10]. I created instance of Class A on main. Class A has a function that foo(float* fVar1, char *sVar2). When i call function Foo from main, I want this function write own variables to var1 and var2[10]. So after call this function, when i use print var1, i want to see actual value. But i am getting 0. How can i solve this?

And here is the my code:

main.cpp:

#include "A.h"
float var1;
char var2[10];
A instance; // dont getting parameter.
int main()
{
  Serial.println(var1); // It is cout func for Arduino. I am getting 0.
}

Ah:

class A
{
  public:
  void Foo(float *fVar1, char*sVar2);
  private:
  float _fVar1;
  char  _sVar2[10];

}

A.Cpp:

#include "A.h"

void A:: Foo(float *fVar1, char*sVar2)
{
  *fVar2 = Func(); // Func Returning float value.
   dtostrf(*fVar1,5,2,sVar2);
}
void A:: Foo2()
{
  Foo(&_fVar1,sVar2);
}

Afterall, when i call Foo2, I want to write pointers pointing values to main variables var1 and var2[10]. EDIT: I solved the problem. I didnt call function from main. That was bad mistake. Sorry for that. As i said before i am new on c++.

"When i call function Foo from main"

But you're clearly not doing that!?

Inside main() have:

instance.Foo(&var1, var2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM