I'm trying to include a member variable in the class I write,
MyClass.h
#include <SomeClass.h>
Class MyClass{
public:
MyClass(int par);
SomeClass B;
}
MyClass.cpp
#include "MyClass.h"
#include "SomeClass.h"
MyClass::MyClass(int par){
B=SomeClass(par);
}
However SomeClass
takes variables for its constructor, so the above code yields no matching function for call to "SomeClass::SomeClass()"
What should I do here?
Update:
Seems like member initializer list is the way to go, but how if I want to use an array of SomeClass
objects? So MyClass.h becomes:
#include <SomeClass.h>
Class MyClass{
public:
MyClass(int par);
SomeClass B[2];
}
You can't quite get what you want, but you can get what you need. To provide array access to the set of objects, create an array of pointers
Then initialize the pointers in your constructor:
This is clearly tedious for more than a small number of objects, but fine for anything you are likely to do on a microcontroller. Now you have an array of object pointers that can be used as you need:
Note that you are consuming additional memory for a pointer to each object.
use member initializer list