成员对象构造函数和枚举

发布于 2024-11-23 23:20:55 字数 428 浏览 0 评论 0原文

为什么这不能编译?

File.hpp

class CTest
{
  public:
    enum enumTest { EN_TEST };

    //constructor:
    CTest(enumTest f_en);
};

AnotherFile.hpp

#include "File.hpp"

class CAnotherTest
{
  public:
    CTest obj_Test(CTest::EN_TEST);
};

Visual Studio 说:错误 C2061:语法错误:标识符“EN_TEST”

armcc 编译器说:错误:#757:常量“CTest::EN_TEST”不是类型名称

谢谢,Mirco

Why does this not compile?

File.hpp

class CTest
{
  public:
    enum enumTest { EN_TEST };

    //constructor:
    CTest(enumTest f_en);
};

AnotherFile.hpp

#include "File.hpp"

class CAnotherTest
{
  public:
    CTest obj_Test(CTest::EN_TEST);
};

Visual Studio says: error C2061: syntax error : identifier 'EN_TEST'

armcc compiler says: error: #757: constant "CTest::EN_TEST" is not a type name

Thanks, Mirco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮邮递员 2024-11-30 23:20:56

你不能像那样初始化。只能对static const整型进行类内初始化。

在构造函数中使用初始化列表,如下所示:

class CAnotherTest
{
  public:

    CTest obj_Test; //member declaration. no initialization here

    static const int value  = 100; //OK. static const integral type!

    CAnotherTest() : obj_Test(CTest::EN_TEST) {}
                 //^^^^^^^^^^^^^^^^^^^^^^^^^^ its called initialization-list
};

const int CAnotherTest::value; //definition goes to .cpp file

You cannot initialize like that. In-class initialization can be done for only static const integral type.

Use initialization-list in the constructor, as:

class CAnotherTest
{
  public:

    CTest obj_Test; //member declaration. no initialization here

    static const int value  = 100; //OK. static const integral type!

    CAnotherTest() : obj_Test(CTest::EN_TEST) {}
                 //^^^^^^^^^^^^^^^^^^^^^^^^^^ its called initialization-list
};

const int CAnotherTest::value; //definition goes to .cpp file
感受沵的脚步 2024-11-30 23:20:55

因为,

CTest obj_Test(CTest::EN_TEST);

被评估为名为 obj_Test 的函数。现在它应该有一个类型参数,但是,CTest::EN_TEST 是一个值,而不是类型

如果希望 obj_Test 是一个对象,那么您可以在构造函数中将 CTest::EN_TEST 传递给它:

class CAnotherTest
{
public:
  CAnotherTest () : obj_Test(CTest::EN_TEST) {}
};

Because,

CTest obj_Test(CTest::EN_TEST);

is evaluated as a function named obj_Test. Now it should have argument as a type, however, CTest::EN_TEST is a value, not a type.

If it's intended that obj_Test an object then you have pass CTest::EN_TEST to it in the constructor:

class CAnotherTest
{
public:
  CAnotherTest () : obj_Test(CTest::EN_TEST) {}
};
等数载,海棠开 2024-11-30 23:20:55

因为您的 CAnotherTest 语法是错误的。也许你的意思是这样的?

class CAnotherTest
{
  public:
    // Constructor     vvv Initialise member variable
    CAnotherTest() : obj_Test(CTest::EN_TEST) {}

    // Member variable
    CTest obj_Test;
};

Because your syntax for CAnotherTest is wrong. Perhaps you mean something like this?

class CAnotherTest
{
  public:
    // Constructor     vvv Initialise member variable
    CAnotherTest() : obj_Test(CTest::EN_TEST) {}

    // Member variable
    CTest obj_Test;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文