相关疑难解决方法(0)

为什么静态成员变量不能与三元运算符一起使用?

这是交易.我有一个静态类,其中包含几个用于获取输入的静态函数.该类包含一个私有静态成员变量,用于指示用户是否输入了任何信息.每种输入方法都会检查用户是否输入了任何信息,并相应地设置状态变量.我认为这是使用三元运算符的好时机.不幸的是,我不能,因为编译器不喜欢这样.

我复制了这个问题,然后尽可能地简化了我的代码,使其易于理解.这不是我的原始代码.

这是我的头文件:

#include <iostream>

using namespace std;

class Test {
public:
    void go ();
private:
    static const int GOOD = 0;
    static const int BAD = 1;
};
Run Code Online (Sandbox Code Playgroud)

这是我使用三元运算符的实现:

#include "test.h"

void Test::go () {
    int num = 3;
    int localStatus;
    localStatus = (num > 2) ? GOOD : BAD;
}
Run Code Online (Sandbox Code Playgroud)

这是主要功能:

#include <iostream>
#include "test.h"

using namespace std;

int main () {
    Test test = Test();
    test.go();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我收到此错误消息:

test.o: In function `Test::go()':
test.cpp:(.text+0x17): undefined reference …
Run Code Online (Sandbox Code Playgroud)

c++ static ternary-operator static-members

15
推荐指数
2
解决办法
2196
查看次数

标签 统计

c++ ×1

static ×1

static-members ×1

ternary-operator ×1