0
Initialize object from .cpp file
Quick question, can I initialize an object (found uninitialized inside .h file) from inside a method inside a .cpp file? I tried with "obj = Object ();" but it doesn't work from within the method, it only works if I put it inside the file.h EX: file.h Object obj; EX: file.cpp void XXXX::myFun(int param) { obj = Object(param) }
44 Respuestas
+ 2
So you're also programming Arduino, I understand now why you didn't care to know it 😅
I think I found a solution which is replacing OtherClass a; by OtherClass* a;
Then, replacing a = OtherClass(0); by a = new OtherClass(0);
With this, all works fine but don't forget to apply the same corections for b
+ 1
Listen, I developed once a library for arduino (deleted now) and I had this problem and beside the new operator I've found two solutions :
1_Inspired from Kotlin : replace the OtherClass constructor by OtherClass::OtherClass() {}, then make :
OtherClass::init(int param) {}, next : OtherClass a; and finally in the begin() method replace a = OtherClass(0) by a.init(0) (do the same for b)
2_Initialize a and b with a defult value when declaration (ex: OtherClass a = OtherClass(1)) then do what you want in the begin() method
After one of these solutions, c.a.fun() will work
0
Try to use instead : Object obj() or Object obj = new Object()
0
Wrong and wrong
0
One minute, please reformulate your question, you want to program a module or execute a simple code ?
0
I'm building a library, in the header file there are two uninitialized objects, in the cpp file I use a "begin()" method to initialize the two objects in the header file, only it's not as simple as I thought since I just write:
a = Object(param);
it gives me an error
0
the problem is just that. If I write
Object a = Object(param);
inside the header file, the code compiles perfectly.
0
Listen, I will make a tutorial about module programming in C++ because I did it too but without the files, I can't understand your code, but you may check this article which will help you :
https://www.arduino.cc/en/Hacking/libraryTutorial
It's a C++ library making tutorial for Arduino, if you find these functions : digitalWrite, delay and pinMode, just understand that they are simple functions, but if you follow me, I'll be able to explain what are these functions and what is Arduino
0
I don't care what is arduino, I care if and how an obj can be initialized from within a method in cpp file
0
Please, give me the files
0
And from where Object comes ?
0
I don't understand, do I have to use a pointer? Should I use ClassName::Obj? What can I do ?
0
//XXXX.h
#ifndef XXXX_h
#define XXXX_h
#include "Object.h"
Object obj;
void myFun(int);
#endif
//XXXX.cpp
#include "Object.h"
#include "XXXX.h"
void myFun(int param) {
obj = Object(param);
}
//main.cpp
#include <iostream.h>
#include "XXXX.h"
int main() {
begin(5);
return 0;
}
I think this is what you want
0
//Class.h#
#ifndef CLASS_H
#define CLASS_H
#include "OtherClass.h"
class Class {
public:
Class();
void begin();
OtherClass a;
OtherClass b;
};
#endif
//Class.cpp#
#include "Class.h"
Class::Class() {
}
void Class::begin() {
a = OtherClass(0);
b = OtherClass(0);
}
//OtherClass.h#
#ifndef OTHER_CLASS_H
#define OTHER_CLASS_H
class OtherClass {
public:
OtherClass(int param);
};
#endif
//OtherClass.cpp#
#include "OtherClass.h"
OtherClass::OtherClass(int param) {
}
0
This is code
0
Thank you but your code is correct, so what's the problem or the error ?
0
not compile
0
What's the error message ?
0
C:\Users\Mick\Documents\Arduino\libraries\Class\Class.cpp: In constructor 'Class::Class()':
C:\Users\Mick\Documents\Arduino\libraries\Class\Class.cpp:4:14: error: no matching function for call to 'OtherClass::OtherClass()'
Class::Class() {
^
In file included from C:\Users\Mick\Documents\Arduino\libraries\Class\Class.h:4:0,
from C:\Users\Mick\Documents\Arduino\libraries\Class\Class.cpp:1:
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:8:5: note: candidate: OtherClass::OtherClass(int)
OtherClass(int param);
^~~~~~~~~~
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:8:5: note: candidate expects 1 argument, 0 provided
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate: constexpr OtherClass::OtherClass(const OtherClass&)
class OtherClass {
^~~~~~~~~~
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate expects 1 argument, 0 provided
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate: constexpr OtherClass::OtherClass(OtherClass&&)
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate expects 1 argument, 0 provided
C:\Users\Mick\Documents\Arduino\libraries\Class\Class.cpp:4:14: error: no matching function for call to 'OtherClass::OtherClass()'
Class::Class() {
^
In file included from C:\Users\Mick\Documents\Arduino\libraries\Class\Class.h:4:0,
from C:\Users\Mick\Documents\Arduino\libraries\Class\Class.cpp:1:
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:8:5: note: candidate: OtherClass::OtherClass(int)
OtherClass(int param);
^~~~~~~~~~
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:8:5: note: candidate expects 1 argument, 0 provided
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate: constexpr OtherClass::OtherClass(const OtherClass&)
class OtherClass {
^~~~~~~~~~
C:\Users\Mick\Docume
0
nts\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate expects 1 argument, 0 provided
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate: constexpr OtherClass::OtherClass(OtherClass&&)
C:\Users\Mick\Documents\Arduino\libraries\Class\OtherClass.h:5:7: note: candidate expects 1 argument, 0 provided