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.
Related
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;
}
Can someone let me know what I am doing wrong? I am making the object in my main and trying to pass a string variable to its setter. I keep getting the same error "No viable conversion"
#define PatientType_hpp
#include "PersonType.hpp"
#include "DoctorType.hpp"
#include "dataType.hpp"
class PatientType : public PersonType
{
private:
DoctorType drName;
public:
DoctorType getDrName() const;
void setDrName(DoctorType);
};
#endif /* PatientType_hpp */
//setters and getters
DoctorType PatientType::getDrName() const {
return drName;
}
void PatientType::setDrName(DoctorType drName) {
this->drName = drName;
}
#ifndef DoctorType_hpp
#define DoctorType_hpp
#include "PersonType.hpp"
#include <stdio.h>
class DoctorType: public PersonType
{
private:
string drSpecialty;
public:
string getDrSpecialty()const;
void setDRSpecialty(string);
};
#endif /* DoctorType_hpp */
#include "DoctorType.hpp"
#include <iostream>
string DoctorType::getDrSpecialty()const
{
return drSpecialty;
}
void DoctorType::setDRSpecialty(string drSpecialty)
{
this->drSpecialty=drSpecialty;
}
int main(int argc, const char *argv[]) {
PatientType example;
string drName = "Mr.Scott";
example.setDrName(drName);
// ERROR No viable conversion from 'std::__1::string aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'DoctorType'
}
I'm expecting for it to compile because I am passing in a string into the Patient type which i think accepts strings.
The problem lies here:
void PatientType::setDrName(DoctorType drName)
Here, you expect to send a DoctorType parameter. However, in calling you use:
example.setDrName(drName); where drName is a string, not a DoctorType parameter.
The fix is obvious: either modify the prototype so as it accepts a string parameter or, in calling the method, give it a DoctorType parameter.
The issue is this function:
void PatientType::setDrName(DoctorType drName) {
Here, this function expects parameter of type DoctorType but you are passing std::string.
example.setDrName(drName); // drName is std::string. So, Type mismatch
There are numerous ways to solve this:
Option 1: Change the function signature to void PatientType::setDrName(const std::string &drName) {
Option 2: Less trivial but it works. Define a parameterised constructor in DoctorType accepting std::string as parameter.
Like this:
DoctorType::DoctorType(const std::string &name): name(name) { }
I think Option 2 is suitable in your scenario.
As rightly suggested by #t.niese, you must explicitly create the object of DoctorType and define constructor as explicit. Like this:
explicit DoctorType::DoctorType(const std::string &name): name(name) { }
and while calling it:
example.setDrName(DoctorType(drName));
Below is my code:
// this code illustrates iterating through a nested hashmap.
#include <iostream>
#include "imported.hpp"
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
#define MAX_LINE_LENGTH 999
using namespace std;
class State
{
public:
vector<string> vec;
string state_string;
State(string state_string, vector<string> vec);
};
State::State(string state_string, vector<string> vec)
{
this->state_string = state_string;
this->vec = vec;
}
class Heuristic
{
public:
State goal_state;
string type;
Heuristic(string type, State goal_state);
};
Heuristic::Heuristic(string type, State goal_state)
{
this->type = type;
this->goal_state = goal_state;
}
int main(int argc, char const *argv[])
{
}
When I try to compile it using:
g++ filename.cpp
The following output is produced:
$ g++ main.cpp
main.cpp: In constructor ‘Heuristic::Heuristic(std::string, State)’:
main.cpp:36:51: error: no matching function for call to ‘State::State()’
Heuristic::Heuristic(string type, State goal_state)
^
main.cpp:21:1: note: candidate: State::State(std::string, std::vector<std::basic_string<char> >)
State::State(string state_string, vector<string> vec)
^~~~~
main.cpp:21:1: note: candidate expects 2 arguments, 0 provided
main.cpp:12:7: note: candidate: State::State(const State&)
class State
^~~~~
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: State::State(State&&)
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
I am confused as to why this is happening since I am not even calling the constructor but rather am defining a function's method signature into which the user should be able to pass an existent State object. Please assist.
The Heuristic constructor is built using assignment operators which involves the default construction of its member objects. Since State does not have a default constructor, this form of construction will fail.
There are two ways of solving this:
If the members have user-defined constructors, provide default-constructors also for them .
Use initializer lists for your constructor instead of assignments within the body of the constructor.
Of these two methods, the second one is more preferable. The reasons for this are outlined in the FAQ: Should my constructors use “initialization lists” or “assignment”?
I'm a beginner with C++
I'm having a trouble when I set the header class values.
CalucateNumbers::CalucateNumbers() {
ResetValues();
}
void CalucateNumbers::ResetValues() {
firstNumber = 0;
secondNumber = 8;
}
CalucateNumber is missing exception specification noexcept
Help please?
This is the C plus plus Code file with the name FBullCowGame.cpp
#include "FBullCowGame.hpp"
FBullCowGame::FBullCowGame() {
Reset();
}
void FBullCowGame::Reset() {
CurrentTries = 0;
MaxTries = 8;
}
This is the header file with the name FBullCowGame.hpp
#ifndef FBullCowGame_hpp
#define FBullCowGame_hpp
#include <stdio.h>
#include <string>
#endif /* FBullCowGame_hpp */
class FBullCowGame {
public:
void Reset(); // TODO Make a reset void
// Not important.., The important is this ^^
private:
int CurrentTries;
int MaxTries;
};
Here is the MCVE on godbolt.
You were incorrect when you said "Yes it does" when asked whether the definition matches the header. It does not match the header, because it's not even present in the header!
Your class FBullCowGame doesn't declare a custom constructor, so the compiler created a default one. You then try to create a custom one, and the compiler thinks you're trying to implement the default constructor (which happens to be noexcept), so it says "This redeclaration doesn't match the implicit declaration."
Your real problem is that you forgot to tell the compiler "I'm going to give this class a custom constructor."
class FBullCowGame {
public:
FBullCowGame(); // <----- you forgot this
void Reset(); // TODO Make a reset void
// Not important.., The important is this ^^
private:
int CurrentTries;
int MaxTries;
};
(You also have a problem with the scope of the #ifdef guard in your header file.)
That's a very misleading error message. The problem is that the class definition does not declare a default constructor, but the source code attempts to implement one. To fix this, add a declaration for the default constructor to the class definition.
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.