Hello, I am making a library to help me interface with a proprietary inverter, we are going to communicate with it over CAN. I would like to use the MCP2515 library inside of the inverter library. When creating an MCP2515 object, you have to give it the pin number that its connected to. I would like to just create an inverter object, and give it to the mcp2515 library. When i do this is what i do not know. here are some snippets of my codes:
#include <mcp2515.h>
//PD400 pd400(10);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
the error I get is
In file included from C:\Users\Eric\Documents\Formula Electric\PD400Library\TestingLibrary\TestingLibrary.ino:1:0:
C:\Users\Eric\Documents\Arduino\libraries\PD400/PD400.h:17:17: error: expected identifier before numeric constant
MCP2515 mcp(10);
^~
C:\Users\Eric\Documents\Arduino\libraries\PD400/PD400.h:17:17: error: expected ',' or '...' before numeric constant
exit status 1
Error compiling for board Arduino Uno.
I think that you cannot construct the object in the class' declarations like that. I struggled so much with this at some point and I couldn't understand the answers I found either.
class Thing{
int m=0;
}
I remember at some point I also thought I could do that, because I thought that it was equivalent to the "global" scope found on the main .ino file. It is not
A good place to construct objects contained in a class is in this part of the constructor where you assign parameters to object's properties. I don't know the name of that, but I can show:
class Class1{
public:
int m=0;
Class1(int _m):m(_m){}
};
class Class2{
public:
Class1 ownedObject;
Class2():ownedObject(10){
}
};
Another thing you can approach this is using a reference. The object does not own the object but expects to get a reference to it, so that it can use it
class Class1{
public:
int m=0;
Class1(int _m):m(_m){}
};
class Class2{
public:
Class1 & sharedObject;
Class2(Class1 & _sharedObject):sharedObject(_sharedObject){
}
};
Class1 nolol = Class1(33);
Class2 lol = Class2(nolol);
The line is inside a class declaration so the compiler thinks you are declaring a variable or function. It sees the type MCP2515 and the name 'mcp' and then sees an '(' which means 'mcp' is a function. After that should be the argument list but instead of a type, it gets an integer.
private:
int _pin;
MCP2515 mcp(10);
// Change that to:
private:
int _pin;
MCP2515 mcp;
To pass constructor arguments to a member variable you have to do it in the constructor:
Darn. The member object has no default constructor (constructor with no arguments). I've hit the limit of my knowledge of weird member object initialization. Maybe you have to make 'mcp' a reference?
private:
MCP2515* mcp; // ← Ponter to this kind of thing.
And in your cinstructor… Fire it up like this…
PD400::PD400(int pin) {
mcp = new MCP2515(pin);
}
And to access it, just use → instead of .
-jim lee
Why do it dynamically? Given OP’s requirements, there’s absolutely no advantage to this over just having a private MCP2515 data member per Reply #'s 2 & 8.
johnwasser:
That is weird. I wonder why it refused to take what I suggested:
private:
int _pin;
MCP2515 mcp;
PD400::PD400(int pin) : mcp(10)
{
_pin = pin;
}
They are identical except '10' is a constant and 'pin' is an argument.
Your suggestion was fine. Trouble was (per my post in Reply #6), OP had an extraneous instantiation of an MCP2515 object in the .cpp file that was messing things up. Once that is removed, either way will work. However, for flexibility, I prefer not to hard-code things like pin assignments in the class declaration. Better to have the code that instantiates the object supply them.