OOP referring to classes within a class

Hi,

I have the following code snippet that I'm trying to get working.

main.cpp

#include "Arduino.h"
#include "CanMod.h"

CanMod CAN0(10, 500); //CS pin, Bus speed

int main() {
  CAN0.setup();

  while(true) {
    return 0;
  }
}

CanMod.h

#include "Arduino.h"
#include "mcp_can.h"
#include <SPI.h>    

class CanMod {
public:
  CanMod(int csPin, int canSpeed);
  void setup();
  void read();
  void write();
  void interrupt();
private:
  int _csPin;
  int _canSpeed;
};

CanMod.cpp

#include "CanMod.h"

CanMod::CanMod(int csPin, int canSpeed) {
  _csPin = csPin;
  _canSpeed = canSpeed;
}

void CanMod::setup() {
  begin(_canSpeed);
  MCP_CAN(_csPin);
}

void CanMod::read() {

}

void CanMod::write() {

}

void interrupt() {

}

Upon building I get the following error:

src/CanMod.cpp: In member function 'void CanMod::setup()':
src/CanMod.cpp:9:18: error: 'begin' was not declared in this scope
begin(_canSpeed);
^
src/CanMod.cpp:10:17: error: no matching function for call to 'MCP_CAN::MCP_CAN()'
MCP_CAN(_csPin);

I'm wanting to create an instance of CanMod, that internally creates an instance of MCP_CAN to tap in to its features. How can I do that?

I'm sorry if this question hasn't been presented too straight-forward, but I'm new to the subject. Please refrain from a 'read the error' type of reply, without lending at least the tip of a spoon towards resolving the problem, as I've spent time studying this; but don't yet understand fully what I can or can't do.

Thanks, in advance, for any assistance.

You don't have a "begin" method in your class, nor is it declared in your header, hence the scope problem.

src/CanMod.cpp:9:18: error: 'begin' was not declared in this scope
begin(_canSpeed);
^

Is there some scope in which you think you have defined a begin() method in the CanMod class? I do not see one.

src/CanMod.cpp:10:17: error: no matching function for call to 'MCP_CAN::MCP_CAN()'
MCP_CAN(_csPin);

It appears that the MCP_CAN class' constructor does not take an argument. Why are you supplying one?

This:

  while(true) {
    return 0;
  }

is one of the more bizarre snippets I've seen in a while....

Regards,
Ray L.

PaulS:
Is there some scope in which you think you have defined a begin() method in the CanMod class? I do not see one.
It appears that the MCP_CAN class' constructor does not take an argument. Why are you supplying one?

If you remove my attempt at a class, the MCP_CAN class instance would usually be created like this, and passes the SPI CS pin:

MCP_CAN CAN(CS_PIN)

//later on the module's speed is set like this

CAN.begin(CAN_500KBPS)

It is these two bits I'm wanting to call from the CanMod class I created.

So, you need to properly create an instance of MCP_CAN in your class, AND you need to call that instance's begin() method.

    private:
       MCP_CAN CAN;
CanMod::CanMod(int csPin, int canSpeed) : CAN(csPin)
{
     _csPin = csPin;
     _canSpeed = canSpeed;
}

Delta_G:
Or possibly better let your class derive from MCP_CAN so that it is actually an instance of both.

This is one of the things that I still have trouble with, doing true OOP. Instead of calling the other class, BE the other class.

Thanks for the assist, and especially to PaulS, as your code was spot on.

This now successfully compiles - and I learnt something about how to nest classes.