Constructors



  • Hi, When trying to run the program with these classes, the compiler says" No matching function to call Coordinate::Coordinate - I am not sure where are the mistakes:

    @#ifndef GPSCOORD_H
    #define GPSCOORD_H
    #include <QString>
    #include "coordinate.h"

    class GPSCoord :public Coordinate {
    public:
    GPSCoord();
    void setCoord(int d, int m, int s, char cd);
    QString toString(bool decimal) const;

    private:
    Coordinate latitude;
    Coordinate longitude;
    };
    #endif // GPSCOORD_H

    //coordinate.cpp
    #include <QString>
    #include "coordinate.h"
    #include <iostream>
    using namespace std;

    // constructor
    Coordinate::Coordinate(int d, int m, int s, char cd){
    degrees = d;
    minutes = m;
    seconds = s;
    cardinalDirection = cd;
    }
    //gpscoord.cpp
    #include <QString>
    #include "gpscoord.h"
    #include "coordinate.h"

    //no-arg constructor which initialize
    GPSCoord::GPSCoord(): Coordinate(0,0,0,'N'){}

    void GPSCoord::setCoord(int d, int m, int s, char cd){
    degrees = d;
    minutes = m;
    seconds = s;
    cardinalDirection = cd;

    //main.cpp
    #include <QTextStream>
    #include "coordinate.h"
    #include "gpscoord.h"
    #include <iostream>

    int main(){

        QTextStream cout(stdout);
        Coordinate coord1(20,25,23,'N');
        cout<<coord1<<endl;,
        coord1.degrees = (20);
        coord1.minutes = (21);
        coord1.seconds = (30);
        coord1.cardinalDirection = 'N';
        coord1.toString();
        coord1.toDecimal();
        coord1.toString();
    
        GPSCoord gpscoord1;
        gpscoord1.setCoord(15,30,29,'N');
        gpsCoord1.toString();
        gpscoord1.latitude = (25.23676);
        gpscoord1.longitude = (23.4567);
        //cout << strbuf.str;
    

    return 0;
    }



  • There is no default constructor for class Coordinate ... The Coordinate constructor expects values and creating a GPSCoord tries to create two Coordinate member variables without the arguments.



  • I have added a default constructor, but now the compiler says int Coordinate::degrees are private in gpscoord.cpp. This is an assignment - the data members are private

    @//coordinate.cpp
    #include <QString>
    #include "coordinate.h"
    #include <iostream>
    using namespace std;

    // default constructor

    Coordinate::Coordinate():degrees(0),minutes(0),seconds(0),cardinalDirection('N'){};

    //gpscoord.cpp
    #include <QString>
    #include "gpscoord.h"
    #include "coordinate.h"
    #include <iostream>

    //no-arg constructor which initialize
    GPSCoord::GPSCoord(): Coordinate(degrees, minutes, seconds, cardinalDirection){}@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.