【问题标题】:Passing an object as argument to a Class constructor将对象作为参数传递给 Class 构造函数
【发布时间】:2013-12-12 05:27:40
【问题描述】:

我正在尝试从 Python 和 PyQt 迁移到 C++ 和 Qt。我从一些基本的 OOP 开始,但我偶然发现了这一点:

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;

    };

我正在尝试将 GPoint 对象传递给 GQuadrilateral 构造函数。我收到以下链接错误:

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

请给我一个提示好吗?



编辑 1:在 gpoint.h;

中添加了条件指令

【问题讨论】:

  • 您是否将#include "gpoint.h" 添加到您的gquadrilateral.h 文件中?另外,请确保将#ifndef GPOINT_H #define GPOINT_H 添加到 gpoint.h 的开头,然后在文件末尾添加 #endif
  • 您需要在gquadrilateral.h 中包含gpoint.h。也不要在gquadrilateral.h 中重新定义类。
  • 这是一个链接器错误
  • 对于那些喋喋不休地谈论标题的人:是的,你是对的,那不好,但这不应该导致链接器错误,据我所知,这实际上不是问题,因为他将标题包含在编译单元(main.cpp)。

标签: c++ class constructor arguments


【解决方案1】:

您可能会错过将对象链接在一起的阶段。在这种情况下,您必须运行

  `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`

将所有对象链接在一起。

【讨论】:

  • 没关系...我在 12 分钟前写过.. 那时链接器问题可能仍然存在@DaddyPumpkin
【解决方案2】:

这个错误意味着构造函数根本没有实现,只是声明了,或者你没有链接到包含这个特定构造函数的库或对象。 (顺便说一句,这同样适用于析构函数)。

【讨论】:

  • 是的,你是对的,似乎 someone 评论了 *.cpp 文件中的某些行:D。使用当前实现 GQuadrilateral(GPoint origin, int width, int height); 可以在 GQuadrilateral 类中修改 origin 参数的值?谢谢。
  • 不,它不能,因为原点是复制/临时的。请改用指针 (GPoint*) 或引用 (GPoint&amp;)。更准确地说:您可以在函数内部对其进行修改,但您的更改不会反映到外部,因为您正在使用的参数是一个副本,不再与传递的参数相关。
猜你喜欢
  • 1970-01-01
  • 2012-11-14
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2016-04-21
  • 2017-05-16
  • 2011-11-28
相关资源
最近更新 更多