【问题标题】:c++ class variable as class member [duplicate]c ++类变量作为类成员[重复]
【发布时间】:2014-11-28 12:45:24
【问题描述】:

我在以下代码中的字符串class AInv { 上收到消息:undefined reference to AVector::AVector():

#ifndef AINV_HPP_
#define AINV_HPP_
#include "AVector.hpp"
class AInv {
    public:
        double w;
        double g;
        AVector vector;
};
#endif /* AINV_HPP_ */

AVector的定义:

#ifndef AVECTOR_HPP_
#define AVECTOR_HPP_

class AVector {
    public:
        double x;
        double y;
        double z;

        AVector();
        AVector(const AVector &v);
};
#endif /* AVECTOR_HPP_ */

是否可以使用类变量(不是地址)作为类成员?我想避免变量向量的显式构造函数/析构函数。

【问题讨论】:

  • 错误undefined reference是链接错误。您可能没有AVector 的默认构造函数的定义,或者它没有被链接到
  • 感谢尼尔!我确实跳过了makefile中的AVector.o。问题解决了!

标签: c++ class gcc


【解决方案1】:

是否可以使用类变量(不是地址)作为类成员?

是的。

我想避免变量向量的显式构造函数/析构函数。

不需要显式析构函数; AInv 析构函数将自动调用类类型的每个成员的析构函数,无论您是自己编写还是让编译器生成隐式析构函数。

为避免显式调用构造函数,成员类型必须具有可访问的默认构造函数。你已经声明了一个,所以没关系。

但是,错误消息表明您忘记定义默认构造函数,或者没有链接到包含定义的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2015-04-28
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多