简体   繁体   中英

Passing an object as argument to a Class constructor

I'm trying to migrate from Python and PyQt to C++ and Qt. I've started with some basic OOP but I've stumbled upon this:

main.cpp

#include <iostream>
#include "src/gpoint.h"
#include "src/gquadrilateral.h"

int main() {
    std::cout << "Running..." << std::endl;
    GPoint origin1(23, 50);
    GQuadrilateral rectangle1(origin1, 100, 100);
    std::cout << "Finished..." << std::endl;
    return 0;
}

gpoint.h

#ifndef GPOINT_H
#define GPOINT_H

class GPoint {

    public:
        GPoint(int x = 0, int y = 0);
        int x();
        int y();
        ~GPoint();

    protected:

    private:
        int m_x;
        int m_y;

    };

#endif

gquadrilateral.h

class GPoint;

class GQuadrilateral {

    public:
        GQuadrilateral(GPoint origin, int width, int height);
        int width();
        int height();
        ~GQuadrilateral();

    protected:

    private:
        int m_width;
        int m_height;

    };

I'm trying to pass a GPoint object to the GQuadrilateral constructor. I'm getting the following linking error:

Building target: geometrix.exe
Invoking: Cygwin C++ Linker
g++  -o "geometrix.exe"  ./src/gpoint.o ./src/gquadrilateral.o  ./main.o   
./main.o: In function `main':
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:21: undefined reference to `GQuadrilateral::GQuadrilateral(GPoint, int, int)'
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:21:(.text+0x109): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GQuadrilateral::GQuadrilateral(GPoint, int, int)'
makefile:45: recipe for target 'geometrix.exe' failed
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:21: undefined reference to `GQuadrilateral::~GQuadrilateral()'
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:21:(.text+0x148): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GQuadrilateral::~GQuadrilateral()'
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:24: undefined reference to `GQuadrilateral::~GQuadrilateral()'
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:24:(.text+0x178): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GQuadrilateral::~GQuadrilateral()'
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:21: undefined reference to `GQuadrilateral::~GQuadrilateral()'
/cygdrive/d/workspaces/geometrix/Debug/../main.cpp:21:(.text+0x189): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GQuadrilateral::~GQuadrilateral()'
collect2: error: ld returned 1 exit status
make: *** [geometrix.exe] Error 1

Can you give me a hint, please?



Edit 1: added conditional directives in gpoint.h ;

This error means that the constructor is not implemented at all, just declared, or that you havent linked against the libraries or objects which contain this specific constructor. (The same applies to the desctructor aswell by the way).

You could be missing the stage in which objects are linked together. In that case you have to run

  `g++ -Wall -pedantic -c gpoint.c
   g++ -Wall -pedantic -c gquadrilateral.c
   g++ -Wall -pedantic -c main.c
   g++ -Wall -pedantic -o output main.o gpoint.o gquadrilateral.o`

to link all objects together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM