-2

Error : matching function for call to "cargaison::cargaison()" don t know where the problem comes from !!! i want to creat a cpp file (societe.cpp) using the hpp file named societe.cpp but it looks like the compilator can not find the constructor in the hpp file, but it's declared properly, please guys help !!

Header file:

#include<iostream>
#include"cargaison.hpp"
using namespace std;
class societe{

    private:
        cargaison carg[100];
        int tailleMaxs ;
        int static tailleTabs ;
    public:
        societe(int tmax);
        societe(float pods,float vol,int num, int tmaxs);
        ~societe();
        void affich_societe();
        cargaison getcargaison(int i);
        void setcargaison(int i, cargaison a);
        void ajout_cargaison(float pods,float vol,int num,int ref, float dist, bool b);
        void supp_cargaison(int numc);
 }; 

Source file:

#include <iostream>
#include "societe.hpp"
using namespace std;
int tailltabs = 0;
societe::societe(int tmax)/* here where the error is */
{
    tailleMaxs = 100;
    tailleTabs = tmax;
}
societe::societe(float pods, float vol, int num, int tmaxs)
{
    tailleMaxs = tmaxs;
    tailleTabs = 1;
}

societe::~societe()
{
    for (int i = 0; i < tailleTabs - 1; i++) {
        carg[i].~cargaison();
    }
}
void societe::affich_societe()
{
    for (int i = 0; i < tailleTabs - 1; i++) {
        carg[i].affichCargaison();
    }
}
cargaison societe::getcargaison(int i)
{
    return carg[i];
}
void societe::setcargaison(int i, cargaison a)
{
    carg[i] = a;
}

void societe::ajout_cargaison(float pods, float vol, int num, int ref, float dist, bool b)
{
    if (b == 0) {

        routiere[tailleTabs + 1].routiere(pods, vol, num, ref, dist);
    }
    if (b == 1) {

        aerienne[tailleTabs + 1].aerienne(pods, vol, num, ref, dist);
    }
    tailleTabs++;
}
void societe::supp_cargaison(int num)
{
    for (int i = 0; i < tailletabs - 1; i++) {
        if (carg[i].getRef() == num) {
            for (int j = i; j < tailltabs - 1; j++) {
                carg[j] = carg[j + 1];
                break;
            }
            carg[j - 1].~cargaison();
        }
    }
    tailleTabs--;
}

the cargaison.hpp

#include<iostream>
#include"marchandise.hpp"
class cargaison {

    protected:
        marchandise march[100];
        int tailleMax ;
        int static tailletab ;
    public:
        void addMarchandise(marchandise a);
        void suppMarchandise(int num);
        void affichParId(int num);
        void affichTT();
        float affichPoidTotal();
        float affichVolTotal();
        float affichCout();
        cargaison(int tmax);
        cargaison(float pods,float vol,int num,int tmax);
        ~cargaison();
        void affichCargaison();
        void modiferMarch(float npods,float nvol,int nnum, int onum);
    };  
5
  • 2
    1. Where's the header file? 2. Please indent the code properly, it's unreadable. 3. Remove everything that's not revelant to the problem, this link may help: minimal reproducible example. Dec 31, 2017 at 15:47
  • 3
    Don't take the advice of whoever told you to explicitly call the destructor.
    – molbdnilo
    Dec 31, 2017 at 16:00
  • Could you please show us the definition of cargaison in cargaison.hpp ? And show with a comment on which line you get the error message ? (By the way, using namespace std; in a header file is a very bad practice).
    – Christophe
    Dec 31, 2017 at 16:54
  • i showed the cargaison.hpp and the error too Dec 31, 2017 at 17:15
  • @KossayRhafiri • I do not see a cargaison:: cargaison() default constructor in the header file.
    – Eljay
    Dec 31, 2017 at 17:16

1 Answer 1

1

You seem to have both a class and an array, both named cargaison. That will cause some confusion. Not only for the compiler but also for everyone reading the code.

For example, in code like this

void societe::setcargaison(int i, cargaison a){
    cargaison[i]=a;
}

how do we know when cargaison is the name of a type and when it is the name of an array? Or, maybe, also the type of the array? :-)

And, if you create an array, the compiler will call the default constructor for all array members. Does the class have a default constructor?

Also, explicitly calling a destructor like cargaison[i].~cargaison(); is wrong in at least 99.99% of all cases. If you have an array, the compiler will do that for you when the array goes our of scope, or when the containing object is destroyed.

1
  • 1
    Have you declared and defined a cargaison::cargaison() constructor?
    – Eljay
    Dec 31, 2017 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.