No matching function call call to constructor in header file - c++

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.

Related

In C++ I am getting an Error while I am executing my code with get set and constructor methods [duplicate]

This question already has answers here:
Does a type require a default constructor in order to declare an array of it?
(9 answers)
Object array initialization without default constructor
(14 answers)
Closed 6 months ago.
In my code I am using the get, set and Employee constructor for getting and take the employee data for that I am creating an one .h file and two .cpp file. While executing these files i am getting error.
So my first .h file is emp.h:
#include<iostream>
class Employee
{
private:
std::string emp_Name,emp_ID,emp_Address;
public:
Employee(std::string name, std::string id, std::string address):emp_Name(name),
emp_ID(id),emp_Address(address)
{
}
void setempname(std::string name);
void setempid(std::string id);
void setempage(std::string address);
std::string getempname();
std::string getempid();
std::string getempaddress();
};
My first .cpp file is emp.cpp
#include "emp.h"
void Employee::setempname(std::string name)
{
emp_Name=name;
}
void Employee::setempid(std::string id)
{
emp_ID=id;
}
void Employee::setempage(std::string address)
{
emp_Address=address;
}
std::string Employee::getempname()
{
return emp_Name;
}
std::string Employee::getempid()
{
return emp_ID;
}
std::string Employee::getempaddress()
{
return emp_Address;
}
My second .cpp file is Main.cpp:
#include "emp.h"
int main()
{
Employee emp[2];
emp[0]= Employee ("Rohi","E345","Clk");
return 0;
}
The Error I am getting is:
g++ emp.cpp Main.cpp
Main.cpp: In function ‘int main()’:
Main.cpp:4:23: error: no matching function for call to ‘Employee::Employee()’
Employee emp[2];
^
In file included from Main.cpp:1:
emp.h:7:3: note: candidate: ‘Employee::Employee(std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)’
Employee(std::string name, std::string id, std::string address):emp_Name(name),
^~~~~~~~
emp.h:7:3: note: candidate expects 3 arguments, 0 provided
emp.h:2:7: note: candidate: ‘Employee::Employee(const Employee&)’
class Employee
^~~~~~~~
emp.h:2:7: note: candidate expects 1 argument, 0 provided
emp.h:2:7: note: candidate: ‘Employee::Employee(Employee&&)’
emp.h:2:7: note: candidate expects 1 argument, 0 provided
So Anyone please help me to solve these error.

push_back a class object using vector.

How to add object of class to vector in another class.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class info{
private:
int id;
string name;
public:
info(int extId, string extName) {
this->id = extId;
this->name = extName;
}
};
class db {
private:
vector<info> infoVector;
public:
void pushData(info * data) {
this->infoVector.push_back(&data);
}
};
int main(){
info * testData = new info(123, "nice");
db database;
database.pushData(testData);
return 0;
}
I am creating a object of info class. The object contains one int and one string variables. Then I am creating db object and I am passing there a testData object.
I got error message while building project.
main.cpp: In member function ‘void db::pushData(info*)’:
main.cpp:23:44: error: no matching function for call to ‘std::vector<info>::push_back(info*&)’
this->infoVector.push_back(data);
^
In file included from /usr/include/c++/5/vector:64:0,
from main.cpp:2:
/usr/include/c++/5/bits/stl_vector.h:913:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = info; _Alloc = std::allocator<info>; std::vector<_Tp, _Alloc>::value_type = info]
push_back(const value_type& __x)
^
/usr/include/c++/5/bits/stl_vector.h:913:7: note: no known conversion for argument 1 from ‘info*’ to ‘const value_type& {aka const info&}’
What am I doing wrong?
It looks like you are trying to pass the address of an info * type to vector<info>::push_back, which only accepts types of const info & or info &&. Try using the dereference operator * instead of the address-of operator & when you call push_back:
this->infoVector.push_back(*data);
This isn't a great way to use pointers, however, and could lead to memory leakage or segfaults if data is removed from the vector or if it is deleted. It is better for the vector to own its members, so you might consider doing this instead:
class db {
private:
vector<info> infoVector;
public:
void pushData(info data) { // note: not a pointer
this->infoVector.push_back(data); // note: not address-of
}
};
int main(){
info testData(123, "nice"); // note: not a pointer
db database;
database.pushData(testData);
return 0;
}
Otherwise, if you really want infoVector to contain pointers, declare it as:
std::vector<info*> infoVector;
Then remove the address-to operator.
P.S., avoid using namespace std whenever possible!
You have vector<info> and you want to put info *, try to do:
int main(){
info testData(123, "nice");
db database;
database.pushData(testData);
return 0;
}

no known conversion for templated vs const non-templated vector

In my actual code, I included a library, and as soon as I did that, it started crashing. I managed to sort of extract some of that code into this minimal example, that demonstrates the same kind of error:
// g++ -std=c++11 -g -o test-classcall.exe test-classcall.cpp
#include <iostream>
#include <vector>
#include <stdio.h>
class Cat
{
public:
int Age;
Cat() : Age(0) {}
};
std::vector<Cat> myPCats;
typedef std::vector<Cat> TDVectCats;
TDVectCats myTDCats;
void loopSomeCats() {
printf("this function just to cause searching for matching calls\n");
}
void loopSomeCats(TDVectCats& incats) {
std::vector<Cat>::iterator iter;
for(iter = incats.begin(); iter != incats.end(); iter++) {
printf("hm\n");
}
}
const std::vector<Cat> & getSomeCats() {
return myPCats;
}
void doSomething() {
loopSomeCats(getSomeCats());
}
int main() {
myTDCats.push_back(Cat());
myTDCats.push_back(Cat());
myPCats.push_back(Cat());
doSomething();
std::cout << "Hello World! " << std::endl;
return 0;
}
The result is:
$ g++ -std=c++11 -g -o test-classcall.exe test-classcall.cpp
test-classcall.cpp: In function ‘void doSomething()’:
test-classcall.cpp:36:29: error: no matching function for call to ‘loopSomeCats(const std::vector<Cat>&)’
loopSomeCats(getSomeCats());
^
test-classcall.cpp:36:29: note: candidates are:
test-classcall.cpp:20:6: note: void loopSomeCats()
void loopSomeCats() {
^
test-classcall.cpp:20:6: note: candidate expects 0 arguments, 1 provided
test-classcall.cpp:24:6: note: void loopSomeCats(TDVectCats&)
void loopSomeCats(TDVectCats& incats) {
^
test-classcall.cpp:24:6: note: no known conversion for argument 1 from ‘const std::vector<Cat>’ to ‘TDVectCats& {aka std::vector<Cat>&}’
What especially confuses me, is the last "no known conversion for argument 1 from ‘const std::vector<Cat>’ to ‘TDVectCats& {aka std::vector<Cat>&}’", as if it cannot convert a vector of something, into the vector of the same something, just because of typedef? Or it maybe has to do with the const - but I simply cannot see what I need to change, in order to have a call like loopSomeCats(getSomeCats()); succeed...
You can't pass a reference to a const object to a non-const reference.
loopSomeCats takes a std::vector<Cat>& as argument, and you want to pass a const std::vector<Cat>& to it, but that's not possible.
The const would mean that you don't want anyone to modify the return value, but if you pass it to a function which just takes a non-const reference, then theoretically the function can modify the reference, and you don't want that.
You should drop the const if you want the return value to be modified.

skipping adding constructors when inheriting from std::string class

Tried to argument the std::string so that it supports method "bool operator==(int)". I got errors:
$ g++ -std=c++11 te2.cc
te2.cc: In function ‘int main(int, char**)’:
te2.cc:20:20: error: no matching function for call to ‘mstring::mstring(const char [4])’
te2.cc:20:20: note: candidates are:
te2.cc:10:7: note: mstring::mstring()
te2.cc:10:7: note: candidate expects 0 arguments, 1 provided
te2.cc:10:7: note: mstring::mstring(const mstring&)
te2.cc:10:7: note: no known conversion for argument 1 from ‘const char [4]’ to ‘const mstring&’
te2.cc:10:7: note: mstring::mstring(mstring&&)
te2.cc:10:7: note: no known conversion for argument 1 from ‘const char [4]’ to ‘mstring&&’
Here is the simple source:
#include <unordered_map>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
class mstring : public string {
public:
//mstring (char* p) : std::string(p) {};
bool operator == (int x) {
int n = atoi(this->c_str());
return (n == x);
}
};
int main(int argc, char *argv[])
{
mstring t("123");
if (t == atoi(argv[1])) {
printf("yes\n");
} else {
printf("no\n");
}
}
If I uncomment the constructor /mstring (char* p) : std::string(p) {};, then it compiles and runs fine.
The question is, if it possible to make it work without defining the constructors for mstring, just use the whatever the constructors of the base class (there is no new data member anyway)? Thanks.
What about providing a free standing operator function instead of inheriting from std::string (which makes that code more usable overall):
bool operator==(const std::string& s, int i) {
int n = atoi(s.c_str());
return (n == i);
}
bool operator==(int i, const std::string& s) {
return s == i;
}
Or even more generic:
template<typename T>
bool operator==(const std::string& s, T t) {
std::istringstream iss;
iss << t;
return (s == iss.str());
}
Classes from the std namespace aren't intended to be inherited, but just used in interfaces and function parameters. Inheriting from those classes makes your code less usable, since clients need to use your implementation instead of just using the std type.
Also note: For your particular use case it's not necessary to convert anything at all, unless you want to assert that argv[1] contains a number (where atoi() certainly isn't the best method to do so, look up stoi() instead). You can just compare the strings:
if (std::string("123") == argv[1]) {
printf("yes\n");
} else {
printf("no\n");
}
you can explicitly inherit the constructors by adding
using string::string;
in your class

Error: default argument given for parameter after previous specification

very simple task for me here and I'm not sure why this is giving me problems, I'm simply making two mockup classes try to compile without any logic in their methods whatsoever using headers and declarations already given to me. Honestly this is just a cut and paste job more than anything, and yet I still came across this golden nugget of love -
cbutton.cpp:11:44: error: default argument given for parameter 4 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:11:44: error: default argument given for parameter 5 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:19:41: error: default argument given for parameter 1 of ‘void cio::CButton::draw(int)’ [-fpermissive]
cbutton.h:11:10: error: after previous specification in ‘virtual void cio::CButton::draw(int)’ [-fpermissive]
cbutton.cpp:53:29: error: ‘virtual’ outside class declaration
Here are the files I'm working with. Thank you everyone, as always!
#include "cfield.h"
namespace cio{
class CButton: public CField{
public:
CButton(const char *Str, int Row, int Col,
bool Bordered = true,
const char* Border=C_BORDER_CHARS);
virtual ~CButton();
void draw(int rn=C_FULL_FRAME);
int edit();
bool editable()const;
void set(const void* str);
};
}
#include "cbutton.h"
namespace cio {
CButton::CButton(const char *Str, int Row, int Col,
bool Bordered = true,
const char* Border=C_BORDER_CHARS){
}
void CButton::draw(int rn=C_FULL_FRAME){
}
int CButton::edit(){
return 0;
}
bool CButton::editable()const {
return false;
}
void CButton::set(const void* str){
}
virtual CButton::~CButton(){
}
}
You specified a default argument in the definition of the function, while they already had a default argument in the class declaration. You can declare default arguments in the class declaration or in the function definition, but not both.
EDIT: Missed the end of your errors: error: ‘virtual’ outside class declaration. It's a rather clear compiler error: virtual keywords belongs to class declarations, not function definitions. Simply remove it from the definition of your destructor.
Corrected source:
namespace cio {
CButton::CButton(const char *Str, int Row, int Col,
bool Bordered, // No default parameter here,
const char* Border){ // here,
}
void CButton::draw(int rn){ // and here
}
CButton::~CButton(){ // No virtual keyword here
}
}
You're not allowed to repeat default arguments when you define a function. They belong only on the declaration. (The actual rule isn't quite that simple, because a definition can also be a definition, but you get the idea...)
You dont include the default parameter in your function definition, the prototype is the only one you need to include the default value into.
#include "cbutton.h"
namespace cio {
CButton::CButton(const char *Str, int Row, int Col,
bool Bordered,
const char* Border){ //remove in def
}
void CButton::draw(int rn){
}