Issue passing a reference to another class within a class - c++

I have created the following class which has an object being passed in by reference and is implemented the following way.
Diablo_Serial_4DLib Display(&DisplaySerial);
ZenDisplay ui(Display);
class ZenDisplay
{
public:
ZenDisplay(Diablo_Serial_4DLib &display);
void setup();
private:
Diablo_Serial_4DLib* _display;
};
The constructor is straight forward and works as expected, no problem so far.
// Constructor /////////////////////////////////////////////////////////////////
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display)
{
_display = &display;
}
I want to instantiate ZenSpeakerGroup class and pass in the same reference into it's constructor
class ZenSpeakerGroup
{
public:
ZenSpeakerGroup(Diablo_Serial_4DLib &display);
private:
Diablo_Serial_4DLib* _display;
};
ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib &display)
{
_display = &display;
}
I have modified the original working class to the following
class ZenDisplay
{
public:
ZenDisplay(Diablo_Serial_4DLib &display);
void setup();
private:
Diablo_Serial_4DLib* _display;
ZenSpeakerGroup _speakerGroup;
};
// Constructor /////////////////////////////////////////////////////////////////
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(&display)
{
_display = &display;
}
Now I get the following error and not 100% sure what I am doing wrong.
Arduino: 1.8.16 (Mac OS X), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 921600, None"
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp: In constructor 'ZenDisplay::ZenDisplay(Diablo_Serial_4DLib&)':
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp:14:78: error: no matching function for call to 'ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib*)'
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(&display)
^
In file included from /Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.h:14,
from /Users/xxx/Documents/Arduino/libraries/ZenOne/ZenDisplay.cpp:9:
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:18:5: note: candidate: 'ZenSpeakerGroup::ZenSpeakerGroup(Diablo_Serial_4DLib&)'
ZenSpeakerGroup(Diablo_Serial_4DLib &display);
^~~~~~~~~~~~~~~
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:18:5: note: no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'Diablo_Serial_4DLib&'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: candidate: 'constexpr ZenSpeakerGroup::ZenSpeakerGroup(const ZenSpeakerGroup&)'
class ZenSpeakerGroup
^~~~~~~~~~~~~~~
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'const ZenSpeakerGroup&'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: candidate: 'constexpr ZenSpeakerGroup::ZenSpeakerGroup(ZenSpeakerGroup&&)'
/Users/xxx/Documents/Arduino/libraries/ZenOne/ZenSpeakerGroup.h:15:7: note: no known conversion for argument 1 from 'Diablo_Serial_4DLib*' to 'ZenSpeakerGroup&&'
exit status 1
Error compiling for board DOIT ESP32 DEVKIT V1.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I also tried the following with no success.
_speakerGroup = new ZenSpeakerGroup(&display)
I would appreciate some help on this.

This is the correct way of passing the display reference to ZenSpeakerGroup
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display) : _speakerGroup(display)
{
_display = &display;
}

I have made this mistake aswell it's a typo
make sure when you want to intialize a reference define it as a refference
class ZenDisplay{
public:
ZenDisplay(Diablo_Serial_4DLib &display);
void setup();
private:
Diablo_Serial_4DLib& _display; // <--make sure this is a refference
};
now initialize the refference
ZenDisplay::ZenDisplay(Diablo_Serial_4DLib &display){
_display = display;
}

Related

Constructor of virtual genetic class [duplicate]

This question already has answers here:
error: use of deleted function
(6 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 3 months ago.
I have this code:
sensor.h:
template<class T>
class Sensor {
public:
uint8_t address;
T data;
virtual void collectData() = 0;
Sensor(uint8_t address);
};
class TemperatureSensor: public Sensor<float> {
void collectData();
};
sensor.cpp:
template<typename T>
Sensor<T>::Sensor(uint8_t address) {
this->address = address;
}
void TemperatureSensor::collectData() {
//some code for collecitng data
}
main function:
TemperatureSensor sensor;
Serial.printf("%d", sensor.address);
Error:
src\sensor.cpp: In function 'void test()':
src\sensor.cpp:11:23: error: use of deleted function 'TemperatureSensor::TemperatureSensor()'
11 | TemperatureSensor sensor;
| ^~~~~~
In file included from src\sensor.cpp:1:
src/sensor.h:14:7: note: 'TemperatureSensor::TemperatureSensor()' is implicitly deleted because the default definition would be ill-formed:
14 | class TemperatureSensor: public Sensor<float> {
| ^~~~~~~~~~~~~~~~~
src/sensor.h:14:7: error: no matching function for call to 'Sensor<float>::Sensor()'
src/sensor.h:11:9: note: candidate: 'Sensor<T>::Sensor(uint8_t) [with T = float; uint8_t = unsigned char]'
11 | Sensor(uint8_t address);
| ^~~~~~
src/sensor.h:11:9: note: candidate expects 1 argument, 0 provided
src/sensor.h:6:7: note: candidate: 'constexpr Sensor<float>::Sensor(const Sensor<float>&)'
6 | class Sensor {
| ^~~~~~
src/sensor.h:6:7: note: candidate expects 1 argument, 0 provided
src/sensor.h:6:7: note: candidate: 'constexpr Sensor<float>::Sensor(Sensor<float>&&)'
src/sensor.h:6:7: note: candidate expects 1 argument, 0 provided
*** [.pio\build\nodemcuv2\src\sensor.cpp.o] Error 1
I want to have multiplte options of same base class(Sensor class) and extend it(I think this is rigth name). I cant create new instance of TemperatureSensor, from error i asume that i need to pass reference of Sensor, but I cant create new Sensor, beacuse it is virtual. Also this is not expected by me behavior. I want to create TemperatureSensor using constructor defined by Sensor ex: TemperatureSensor sensor(0xbeef/*address*/)
I want to create TemperatureSensor using constructor defined by Sensor ex: TemperatureSensor sensor(0xbeef/*address*/)
If you want to use base class constructor directly, you can use using
class TemperatureSensor: public Sensor<float> {
using Sensor::Sensor;
void collectData();
};

Error: no matching function for call to ‘foo::foo()’

I have the following two files
foobar.h
#ifndef FOOBAR_H
#define FOOBAR_H
#include <cstring>
class Foo {
public:
int x;
Foo(int x);
};
class Bar {
public:
char* name;
Foo foo;
Bar(Foo foo);
};
#endif // FOOBAR_H
and foobar.cpp
#include "foobar.h"
Foo::Foo(int x) {
// Do something
}
Bar::Bar(Foo foo) {
// Do something
}
Attempting to compile these with g++ -c foobar.cpp -o foobar.o results in the following error:
foobar.cpp: In constructor ‘Bar::Bar(Foo)’:
foobar.cpp:9:17: error: no matching function for call to ‘Foo::Foo()’
Bar::Bar(Foo foo) {
^
foobar.cpp:5:1: note: candidate: ‘Foo::Foo(int)’
Foo::Foo(int x) {
^~~
foobar.cpp:5:1: note: candidate expects 1 argument, 0 provided
In file included from foobar.cpp:1:
foobar.h:5:7: note: candidate: ‘constexpr Foo::Foo(const Foo&)’
class Foo {
^~~
foobar.h:5:7: note: candidate expects 1 argument, 0 provided
foobar.h:5:7: note: candidate: ‘constexpr Foo::Foo(Foo&&)’
foobar.h:5:7: note: candidate expects 1 argument, 0 provided
As far as I understand the output of g++ is that it requires me to have a default constructor for foo. Why? I wish to pass a foo object to the constructor of bar, why would it need to invoke a default constructor anywhere? I do not want to have a no-args constructor anyways, since I NEED that x has a specific user defined value.
You are trying to default construct a Foo here:
Bar::Bar(Foo foo) {
// the member variable `foo` would have been default constructed here
// Do something
}
But Foo doesn't have a default constructor. One possible solution would be to initialize it in the member initializer list:
Bar::Bar(Foo foo) : foo(std::move(foo)) { // now uses the move constructor instead
// Do something
}

In Constructor ClassName::ClassName() ... ClassName doesn't have any field named 'VariableName'

I'm dealing with an issue right now in C++, where I have a defined variable existing within my class definition, the compiler gets angry and tells me that said variable has not been defined within the scope, and that the constructor doesn't have any field for the variable in question.
I have a simple class hierarchy where I have the base class, Neuron, and two subclasses HiddenNeuron and OutputNeuron, that are both inheriting from Neuron in public virtual mode.
I've made a neuron.h file where I defined the virtual header class, and as such, I have been told by my instructor that I need not write an implementation file for it.
I've then written two header files for HiddenNeuron and OutputNeuron, in which I define them, and two corresponding cpp files that implement them, respectively.
When I try running
g++ neuralnet.cpp input_neuron.cpp hidden_neuron.cpp output_neuron.cpp
I get a bunch of errors like:
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
and
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
Here's the code for my classes:
neuron.h:
#ifndef NEURON_H
#define NEURON_H
class Neuron {
//protected:
// double long value;
public:
Neuron(void);
//Neuron(const double long val);
//
virtual double long activation(void) const =0;
virtual void input(double long val);
};
#endif
output_neuron.h:
#ifndef OUTPUT_NEURON_H
#define OUTPUT_NEURON_H
#include "neuron.h"
class OutputNeuron : public virtual Neuron {
private:
double long value;
public:
OutputNeuron(void);
OutputNeuron(const double long val);
// this should return a value between 0 & 1
virtual double long activation(void) const;
// vector multiplication of the weight vector
// and all the values of the last layer
// with an additional corresponding bias
// added onto the end.
virtual void input(double long val);
};
#endif
output_neuron.cpp:
#include "output_neuron.h"
#include <cmath>
OutputNeuron::OutputNeuron(void) : value(0) { }
OutputNeuron::OutputNeuron(const double long val)
: value(val) { }
inline double long OutputNeuron::activation(void) const {
return 1 / (1 + exp((-1)*value));
}
inline void OutputNeuron::input(double long val) {
value = val;
}
hidden_neuron.h:
#ifndef HIDDEN_NEURON_H
#define HIDDEN_NEURON_H
#include "neuron.h"
class HiddenNeuron : public virtual Neuron {
private:
double long value;
public:
HiddenNeuron(void);
HiddenNeuron(const double long val);
// activation function that takes the stored value
// and squashes it and returns it
virtual double long activation(void) const;
virtual void input(double long val);
};
#endif
hidden_neuron.cpp:
#include "hidden_neuron.h"
#include <cstdlib>
#include <cmath>
HiddenNeuron::HiddenNeuron(void)
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
inline void HiddenNeuron::input(double long val) { value = val; }
inline double long HiddenNeuron::activation(void) const {
return 1/(1 + exp((-1)*value));
}
FULL ERROR LOG:
neuralnet.cpp: In member function ‘void NeuralNet::initialize(bool)’:
neuralnet.cpp:72:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:5:0,
from neuralnet.cpp:1:
hidden_neuron.h:5:7: note: because the following virtual functions are pure within ‘HiddenNeuron’:
class HiddenNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
neuralnet.cpp:85:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
neuralnet.cpp:98:18: error: invalid new-expression of abstract class type ‘OutputNeuron’
temp = new OutputNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:7:0,
from neuralnet.cpp:1:
output_neuron.h:5:7: note: because the following virtual functions are pure within ‘OutputNeuron’:
class OutputNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron()’:
hidden_neuron.cpp:6:5: error: class ‘HiddenNeuron’ does not have any field named ‘value’
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron(long double)’:
hidden_neuron.cpp:8:53: error: class ‘HiddenNeuron’ does not have any field named ‘value’
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
^~~~~
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
hidden_neuron.cpp:10:52: note: suggested alternative: ‘val’
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
val
hidden_neuron.cpp: In member function ‘virtual long double HiddenNeuron::activation() const’:
hidden_neuron.cpp:13:26: error: ‘value’ was not declared in this scope
return 1/(1 + exp((-1)*value));
^~~~~
hidden_neuron.cpp:13:26: note: suggested alternative: ‘valloc’
return 1/(1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron()’:
output_neuron.cpp:4:36: error: class ‘OutputNeuron’ does not have any field named ‘value’
OutputNeuron::OutputNeuron(void) : value(0) { }
^~~~~
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron(long double)’:
output_neuron.cpp:7:5: error: class ‘OutputNeuron’ does not have any field named ‘value’
: value(val) { }
^~~~~
output_neuron.cpp: In member function ‘virtual long double OutputNeuron::activation() const’:
output_neuron.cpp:10:28: error: ‘value’ was not declared in this scope
return 1 / (1 + exp((-1)*value));
^~~~~
output_neuron.cpp:10:28: note: suggested alternative: ‘valloc’
return 1 / (1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In member function ‘virtual void OutputNeuron::input(long double)’:
output_neuron.cpp:14:3: error: ‘value’ was not declared in this scope
value = val;
^~~~~
output_neuron.cpp:14:3: note: suggested alternative: ‘val’
value = val;
^~~~~
val
If anyone can help me, I've been pulling my hair out with this issue.
Edit: using class design as it's a requirement for the course I'm currently enrolled in.
Edit 2: included the full error log
Edit 3: Okay so this is really bizarre, what I wrote works in a separate project directory as pointed out by user idontseethepoint, however when it's in the same directory as my main project, it still gives me the exact same error.

No matching function for call to constructor class

I'm trying to make the constructor for a subclass. But I keep getting this error message. I've tried searching here, but none of the answers I found applied to my problem. Sorry if it's been asked before.
In constructor 'EixoDinamico::EixoDinamico(double, double, Serie*, bool)':
error: no matching function for call to 'Eixo::Eixo()'
note: candidates are:
note: Eixo::Eixo(std::string, double, double)
note: candidate expects 3 arguments, 0 provided
note: Eixo::Eixo(const Eixo&)
note: candidate expects 1 argument, 0 provided
EDIT: If I rewrite the code so that the subclass is now a class on its own, the problem disappears, but I need it to be a subclass.
Here are the codes:
Eixo.h
#ifndef EIXO_H
#define EIXO_H
#include <iostream>
using namespace std;
class Eixo
{
public:
Eixo(string titulo, double minimo, double maximo);
virtual ~Eixo();
private:
string titulo;
double minimo;
double maximo;
};
#endif // EIXO_H'
Eixo.cpp
#include "Eixo.h"
#include <iostream>
Eixo::Eixo(string titulo, double minimo, double maximo)
{
this->maximo = maximo;
this->minimo = minimo;
this->titulo = titulo;
}
Eixo::~Eixo()
{
//dtor
}
EixoDinamico.h
#ifndef EIXODINAMICO_H
#define EIXODINAMICO_H
#include "Eixo.h"
class EixoDinamico : public Eixo
{
public:
EixoDinamico(double minimoPadrao, double maximoPadrao, Serie*
base, bool orientacaoHorizontal);
virtual ~EixoDinamico();
private:
};
#endif // EIXODINAMICO_H
EixoDinamico.cpp
#include "EixoDinamico.h"
#include "Eixo.h"
EixoDinamico::EixoDinamico(double minimoPadrao, double maximoPadrao, Serie*
base, bool orientacaoHorizontal):Eixo()
{
if(base->getQuantidade()<2){
inicioEixo = minimoPadrao;
fimEixo = maximoPadrao;
}
limiteInferior = base->getLimiteInferior();
limiteSuperior = base->getLimiteSuperior();
if (orientacaoHorizontal){
inicioEixo = limiteInferior->getX();
fimEixo = limiteSuperior->getX();
}
else{
inicioEixo = limiteInferior->getY();
fimEixo = limiteSuperior->getY();
}
}
EixoDinamico::~EixoDinamico()
{
//dtor
}
In the constructor of EixoDinamico you're calling the default constructor of Eixo (Eixo()), but that doesn't exist. The declaration of a custom construct for Eixo disables the automatic generation of a default constructor and you haven't declared one explicitly. To do that, add
Eixo() = default;
to the declaration of Eixo or implement one yourself.
Also make sure that calling the default constructor is really what you want. As underscore_d has pointed out, that doesn't make much sense.

No matching function call call to constructor in header file

I have seen similar questions asked and tried their solutions but the answers to them do not seem to work. I have the following code:
.h
#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;
struct DialogueNode;
struct DialogueOption {
string text;
DialogueNode *next_node;
int return_code;
DialogueOption(string t, int rc, DialogueNode * nn) : text{t},
return_code{rc}, next_node{nn} {}
};
struct DialogueNode {
string text;
vector <DialogueOption> dialogue_options;
DialogueNode();
DialogueNode(const string &);
};
struct DialogueTree {
DialogueTree() {}
void init();
void destroyTree();
int performDialogue();
private:
vector <DialogueNode*> dialogue_nodes;
};
.cpp
#include "dialogue_tree.h"
DialogueNode::DialogueNode(const string &t) : text{t} {}
void DialogueTree::init() {
string s = "Hello";
for(int i = 0; i < 5; i++) {
DialogueNode *node = new DialogueNode(s);
dialogue_nodes.push_back(node);
delete node;
}
}
void DialogueTree::destroyTree() {
}
int DialogueTree::performDialogue() {
return 0;
}
int main() {
return 0;
}
I get the error: error: no matching function for call to ‘DialogueNode:: DialogueNode(std::__cxx11::string&)’ DialogueNode *node = new DialogueNode(s);
EDIT additional notes on error
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode()
dialogue_tree.h:17:8: note: candidate expects 0 arguments, 1 provided
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(const DialogueNode&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const DialogueNode&’
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(DialogueNode&&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘DialogueNode&&’
Which makes no sense to me because I have the constructor defined to take a string as an argument.
You've declared your constructor as:
DialogueNode(const string);
But defined it as:
DialogueNode(const string &t);
Those two aren't the same; the former takes a const string while the latter takes a const string reference. You'll have to add the & to specify a reference argument:
DialogueNode(const string &);
it is because in the constructor you are specifying that the parameter will be a string of constant type and when creating an object you are passing a string. The type mismatch is the problem, either fix the constructor parameter to string or change when you are creating an object.