no matching function for call to c++ contructor - c++

I have a problem with c++ derived class constructor , I looked everywhere for similar problems but I dont seem to find any , I wish I will fin help in here.
basically I have a base classe with a constructor that has 4 parameters , and I derived a class from that base class but this time it has 6 parameters ,
both constructors are defined in a cpp file .
here are my files (in frensh):
**Entraineur.h**
class Entraineur: public Personne {
public:
Entraineur(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num, std::string
m_numRAMQ, std::string m_sexe);
virtual ~Entraineur();
private:
std::string m_prenom;
std::string m_nom;
util::Date m_dateNaissance;
std::string m_telephone;
std::string m_numRAMQ;
std::string m_sexe;
**Entraineur.cpp**
#include "Personne.h"
#include "Entraineur.h"
#include <string>
#include "Date.h"
Entraineur::Entraineur(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num,
std::string m_numRAMQ, std::string m_sexe) {
}
Entraineur::~Entraineur() {
// TODO Auto-generated destructor stub
}
**Personne.h**
class Personne {
public:
Personne(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num);
virtual ~Personne();
private:
std::string m_prenom;
std::string m_nom;
util::Date m_dateNaissance;
std::string m_telephone;
std::string m_numRAMQ;
std::string m_sexe;
};
**Personne.cpp**
Personne::Personne(std::string p_nom, std::string p_prenom, util::Date p_date, std::string p_num) {
// TODO Auto-generated constructor stub
//setters
asgNom(p_nom);
asgPrenom(p_prenom);
asgDateNaissance(p_date);
asgTelephone(p_num);
}
Personne::~Personne(){
std::cout<< "detruit";
}
all the #includes are there.
please I beg for help.

Related

Why do I get a "no matching constructor error?

I want my code to take a name, mail and car as argument types, and I try to do so in a class named Person. In main(), I try to give that class a variable a which I can call later in cout. However, I get this exact error:
no matching constructor for initialization of "Person"
How can I fix this?
The h. file
#pragma once
#include <iostream>
#include "car.h"
#include <string>
class Person{
private:
std::string name;
std::string mail;
Car* car;
public:
Person(std::string name, std::string mail);
Person(std::string name, std::string, Car* car);
void setMail(std::string mail);
std::string getMail() const;
std::string getName() const;
bool hasAvailableFreeSeats();
friend std::ostream& operator <<(std::ostream& os, const Person& person);
};
The cpp file:
#include "person.h"
std::string Person:: getMail() const{
return mail;
}
std:: string Person:: getName() const{
return name;
}
void Person:: setMail(std::string mail){
this -> mail = mail;
}
Person:: Person(std::string name, std::string mail) : Person(name, mail, nullptr){};
Person::Person(std::string name, std::string, Car* car) : name{name}, mail{mail}, car{car}{};
bool Person:: hasAvailableFreeSeats(){
if (car != nullptr){
return car-> hasFreeSeats();
}
}
std::ostream& operator <<(std::ostream& os, const Person& person){
return os << person.name << ": " << person.mail << "\n";
}
main:
#include "person.h"
int main(){
std::string name{"Ola Normann"};
std::string mail{"ola.normann#norge.no"};
std::unique_ptr<Car> car{new Car{5}};
Person a{name, mail, std::move(car)};
};
First off, you have a typo in your 3-parameter Person constructor. The 2nd parameter has no name assigned to it, so you end up initializing the mail class member with itself, not with the caller's input:
Person::Person(std::string name, std::string, Car* car) : name{name}, mail{mail}, car{car}{};
^ no name here! ^ member!
That should be this instead:
Person::Person(std::string name, std::string mail, Car* car) : name{name}, mail{mail}, car{car}{};
^ has a name now! ^ parameter!
Now, that being said, your main() code is passing 3 values to the Person constructor:
Person a{name, mail, std::move(car)};
Your Person class only has 1 constructor that accepts 3 parameters:
Person(std::string name, std::string, Car* car);
In main(), your name and mail variables are std::string objects, which is fine, but your car variable is a std::unique_ptr<Car> object, not a Car* pointer. std::move(car) will return a std::unique_ptr<Car>&& rvalue reference, which Person does not accept, hence the compiler error. std::unique_ptr is not implicitly convertible to a raw pointer. You would have to use its get() method instead:
Person a{name, mail, car.get()};
Which defeats the purpose of using std::unique_ptr in the first place. You should instead change the Person class to hold a std::unique_ptr object instead of a raw pointer, eg:
.h
#pragma once
#include <iostream>
#include <string>
#include <memory>
#include "car.h"
class Person{
private:
std::string name;
std::string mail;
std::unique_ptr<Car> car;
public:
...
Person(std::string name, std::string mail);
Person(std::string name, std::string mail, std::unique_ptr<Car> car);
...
};
.cpp
#include "person.h"
...
Person::Person(std::string name, std::string mail) : Person(name, mail, nullptr){};
Person::Person(std::string name, std::string, std::unique_ptr<Car> car) : name{name}, mail{mail}, car{std::move(car)}{};
...
main
#include "person.h"
int main(){
std::string name{"Ola Normann"};
std::string mail{"ola.normann#norge.no"};
std::unique_ptr<Car> car{new Car{5}};
Person a{name, mail, std::move(car)};
// alternatively:
// Person a{name, mail, std::make_unique<Car>(5)};
};

I want to pass in constructor mode of file

I created child class of ofstream. I want to pass in constructor mode of file. For example ios::app. How can i do it ? What should i write in my_file constructor to put it in ofstream class constructor? I know that it's int type but how to understand what is value of ios::app?
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class my_file : public ofstream {
string name;
public:
my_file(string name, const char* filename) : ofstream(filename) { this->name = name; }
inline const string get() { return this->name; }
};
int main(void) {
my_file file("Name","new.txt" /* , ios::app */ );
return 0;
}
I know that it's int type but how to understand what is value of ios::app?
Wrong, that's not a int!
Go to ofstream doc http://www.cplusplus.com/reference/fstream/ofstream/, then click (constructor) to see what the parameters are and then you can see that mode is of type std::ios_base::openmode (as described here)
So simply do:
class my_file : public ofstream {
string name;
public:
my_file(string name, const char* filename, std::ios_base::openmode mode ) : ofstream(filename,mode) { this->name = name; }
inline const string get() { return this->name; }
};
Then:
my_file file("Name","new.txt", ios::app);

C++ Immutability of classes and pros/cons

There are three ways I know of to create an immutable object.
Method 1: internally immutable class members, internally and externally unmodifiable.
#ifndef INTERNALLY_IMMUTABLE_HPP
#define INTERNALLY_IMMUTABLE_HPP
#include <string>
class internally_immutable
{
public:
internally_immutable(const std::string & str)
: str_(str)
{
}
std::string get_str() const
{
return str_;
}
private:
const std::string str_;
};
#endif
Method 2: externally immutable class members, externally unmodifiable.
#ifndef EXTERNALLY_IMMUTABLE_HPP
#define EXTERNALLY_IMMUTABLE_HPP
#include <string>
#include <vector>
class externally_immutable
{
public:
externally_immutable(const std::string & str)
: str_(str)
{
}
std::string get_str() const
{
return str_;
}
private:
std::string str_;
};
#endif
Method 3: type immutable, partially externally unmodifiable, as someone could bypass your typedef.
#ifndef TYPED_IMMUTABLE_HPP
#define TYPED_IMMUTABLE_HPP
#include <string>
#include <vector>
typedef const typed_mutable typed_immutable;
class typed_mutable
{
public:
typed_mutable(const std::string & str)
: str_(str),
vec_()
{
}
void set_str(const std::string & str)
{
str_ = str;
}
std::string get_str() const
{
return str_;
}
private:
std::string str_;
};
#endif
What are the pros and cons of each immutable type? Compiler optimizations, impediments, usage of each type... Are there other ways of creating immutable objects in C++? What is the recommended, or most common way to create these immutable classes in C++?
you can make the immutable class when will you make your constructor private in this case your derived class can't access base class.
like as .
class externally_immutable
{
private :
externally_immutable(const std::string & str)
: str_(str){}
// to do
const string str_;
};

Error no member function declared in class when compiling

I am pretty new to c++ and have no idea why I am getting this error, except that I think it's to do with using the string type for getter methods.
The error message:
C:\Users\Robin Douglas\Desktop\week6>g++ -c Student.cpp
Student.cpp:15:31: error: no 'std::string Student::get_name()' member function d
eclared in class 'Student'
Student.cpp:20:43: error: no 'std::string Student::get_degree_programme()' membe
r function declared in class 'Student'
Student.cpp:25:32: error: no 'std::string Student::get_level()' member function
declared in class 'Student'
Student.hpp
#include <string>
class Student
{
public:
Student(std::string, std::string, std::string);
std::string get_name;
std::string get_degree_programme;
std::string get_level;
private:
std::string name;
std::string degree_programme;
std::string level;
};
Student.cpp
#include <string>
#include "Student.hpp"
Student::Student(std::string n, std::string d, std::string l)
{
name = n;
degree_programme = d;
level = l;
}
std::string Student::get_name()
{
return name;
}
std::string Student::get_degree_programme()
{
return degree_programme;
}
std::string Student::get_level()
{
return level;
}
The following code defines fields (variables) rather then methods.
public:
Student(std::string, std::string, std::string);
std::string get_name;
std::string get_degree_programme;
std::string get_level;
Then, when you implement it in the .cpp file the compiler complains that you try to implement a method that was not declared (since you declared get_name to be a variable).
std::string Student::get_name()
{
return name;
}
To fix, just change your code as below:
public:
Student(std::string, std::string, std::string);
std::string get_name();
std::string get_degree_programme();
std::string get_level();

Char to char* C++

I'm having an error with my constructor in my classes.
In my .cpp file I've got:
Player::Player()
{
m_name="Jane";
m_amt=100;
}
and in my .h file I've got:
// Default constructor, does nothing.
Player();
// Creates a Player. Player name and amount.
Player(const char &name, int amt);
I'm getting the error:
error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
m_name="Jane";
So I tried converting it to a *char but that still doesn't work. Can someone help please?
You can either take a pointer:
Player(const char* name, int amt) { ... }
and you could use std::string to store the name and use char* just to construct this string:
private:
std::string m_name;
public:
Player(const char* name, int amt) : m_name(name) { }
or just use std::string everywhere and pass by reference to avoid redundant copies being created:
private:
std::string m_name;
public:
Player(const std::string& name, int amt) : m_name(name) { }
Note that the last version is quite flexible and it's possible to also pass const char* to it, example:
#include <iostream>
#include <string>
class Player {
private:
std::string m_name;
public:
Player(const std::string& name) : m_name(name){}
void printName() { std::cout << m_name; }
};
int main() {
Player p("Liho");
p.printName();
}
Show your full class/code. Following compiles fine for me.
cpp file...
#include"player.h"
Player::Player()
{
m_name="Jane";
m_amt=100;
}
.h file...
class Player {
public:
// Default constructor, does nothing.
Player();
// Creates a Player. Player name and amount.
Player(const char *name, int amt);
private:
char* m_name;
int m_amt;
};