简体   繁体   English

使用编译时常量会引发错误

[英]using compile time constant throws error

in the below program i have used static const int init.在下面的程序中,我使用了 static const int init。 But it is throwing error /tmp/ccEkWmkT.o(.text+0x15d): In function check::operation()': : undefined reference to check::init'但它抛出错误 /tmp/ccEkWmkT.o(.text+0x15d): In function check::operation()': : undefined reference to check::init'

This error is coming only when used with vector.仅当与矢量一起使用时才会出现此错误。 Can someone please help?有人可以帮忙吗? what is the exact behaviour??确切的行为是什么?

#include<vector>
#include<iostream>
using namespace std;

class check{
static const int init=1;
public:
    check(){}
    void operation();
};

void check::operation(){
    vector<int> dummy;
    dummy.push_back(init);
}

int main(){
    check ck;
    ck.operation();
}

"what is the exact behaviour?" “确切的行为是什么?”

The problem is that push_back takes a reference parameter.问题是push_back需要一个参考参数。 You can use the value of a static const int member variable without providing a separate definition of the object, but you can't use a reference to the object itself (since it doesn't exist).您可以使用static const int成员变量的,而无需提供 object 的单独定义,但您不能使用对 object 本身的引用(因为它不存在)。 The meaning of "using" the member itself is defined in the section of the standard on the One Definition Rule, 3.2/2. “使用”成员本身的含义在标准中关于单一定义规则 3.2/2 的部分中定义。

One fix is to provide a definition in exactly one translation unit:一种解决方法是在一个翻译单元中提供定义:

const int check::init;

If you do this, you can also choose to move the = 1 initialization from the declaration (inside the class) to the definition (outside the class).如果这样做,您还可以选择将= 1初始化从声明(类内)移动到定义(类外)。

Another fix is to create a temporary from the member variable (this only uses the value, it doesn't care where the object is located and hence doesn't care whether it exists), then pass a reference to the temporary:另一个解决方法是从成员变量创建一个临时变量(这仅使用该值,它不关心 object 的位置,因此不关心它是否存在),然后传递对临时变量的引用:

dummy.push_back(int(init));

Of course there's a potential maintenance issue there, that if the types of init and dummy both change to, say, long long [*], and the value changes from 1 to something bigger than INT_MAX , then you're in trouble.当然,这里存在一个潜在的维护问题,如果initdummy的类型都更改为long long [*],并且值从1更改为大于INT_MAX的值,那么你就有麻烦了。 For that reason you could use +init , since the unary + operator also creates a temporary for its result.出于这个原因,您可以使用+init ,因为一元 + 运算符也会为其结果创建一个临时值。 Readers and future maintainers might be a bit puzzled by it, though.不过,读者和未来的维护者可能会对此感到有些困惑。

[*] Supposing your implementation has long long . [*] 假设你的实现long long

You've to provide the definition of the static member outside the class (in .cpp file) as:您必须在 class(在.cpp文件中)之外提供static成员的定义:

//check.h  (same as before)
class check
{
    static const int init=1; //declaration and in-class initialization
public:
    check(){}
    void operation();
};

Then in check.cpp file, do this:然后在check.cpp文件中,执行以下操作:

//check.cpp
#include "check.h"

const int check::init;  //definition

If you pass it by reference it is "used" and you may have to define in the.cpp file so that it gets an address.如果您通过引用传递它,则它是“已使用”的,您可能必须在 .cpp 文件中进行定义,以便它获得地址。

If you just use the value of the constant, you might get away with not defining it.如果您只使用常量的值,您可能会侥幸不定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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