Go Down

Topic: [solved] how to call a class from my own library, inside another own library (Read 1 time) previous topic - next topic

WizenedEE

This is how it should be, where "arduino" is the sketchbook directory
Code: [Select]
arduino/libraries/Cpu/
arduino/libraries/Cpu/Cpu.h
arduino/libraries/Cpu/Cpu.cpp
arduino/libraries/Cpu/utilities
arduino/libraries/Cpu/utilities/CpuOutput.h
arduino/libraries/Cpu/utilities/CpuOutput.cpp

Bonetky

Thanks a lot for the answer, now arduino knows where the libraries are

But now I have more troubles, I can not initalize a CpuOutput object properly in Cpu class:

If I write
Code: [Select]
'CpuOutput outputObject;' in Cpu.h file as a variable, to use in Cpu.cpp (I don't even call it on Cpu.cpp yet), It does not compile having the following error:
Code: [Select]
undefined reference to `CpuOutput::~CpuOutput()'
undefined reference to `CpuOutput::CpuOutput()'

Seems like is waiting for the constructor?

If I write
Code: [Select]
'CpuOutput outputObject(); in Cpu.h (calling constructor, with the intention to use it as a CpuOtput variable to use his own methods). It complies (I guess compiler detect that correspond with CpuOutput constructor). But when I try to use it in Cpu.cpp, no matter how I try to use 'outputObject', I have always the same errors calling his .setParameters function (maybe cause don't recognize it as a cpuOutput variable, but as a function):

attemp and error:
Code: [Select]
outputObject.setParameters(d18, d19, d20, d21, a1, a0);
error: '((Cpu*)this)->Cpu::outputObject' does not have class type



So, create a member variable = CpuOutput constructor not found, and calling constructor = there's no object to use with, cause this is a function? I'm right understanding the errors?

how can I use a CpuOutput object inside Cpu class? or in a general mode, using a class2 object inside class1, considering above errors?


Very grateful for your interest, I wish one day I could help others too

pYro_65

Cpu.cpp does not include Cpu.h, this may cause problems.

Code: [Select]
CpuOutput outputObject;

It should be in the cpp or declared extern, if you want it in the header, the compiler may also need CpuOutput.h to be included in the header.

Code: [Select]
CpuOutput outputObject();

This is incorrect as it is a function prototype, taking no parameters and returning a CpuOutput object.


Bonetky

Sorry for de syntax, #include Cpu1284.h is actually the include for Cpu.h, it's an error caused by change my real code in order to show us a easier and summary code. So Cpu.cpp has the Cpu.h include, and the "CpuOutput outputObject;" error still exist

Can someone help me?

WizenedEE

That compiler error means that you're calling CpuOutput's no-argument constructor, but it doesn't exist. That's because you are never giving it arguments. Google for "c++ initializer lists" They're a way of passing arguments to the constructor of an object inside a class. Here's an example:
Code: [Select]

class inner {
public:
  inner(int ii) : i(ii) {} // This is equivalent to...
  // inner(int ii) {i = ii;}
private:
  int i;
};

class outer {
public:
  outer() : in(3) {} // Passes 3 to the constructor of inner
private:
  inner in;
};

class outerWithMultiple {
  outerWithmultiple() : in1(18), in2(393) {} // passes 18 and 383 to the constructors for in1 and in2
private:
  inner in1;
  inner in2;
};

Go Up