简体   繁体   English

未解决的外部符号热度

[英]Unresolved External Symbol Hilarity

I discovered a novel approach to Java-like enumeration through a class implementation right here on StackOverflow. 我在StackOverflow上通过类实现发现了一种类似于Java枚举的新颖方法。

Symbols class declared in its' own header 在其自己的标头中声明的Symbols类

class Symbols {

public:

    Symbols() {}

    static const Symbols CREATE;
              . . .
private:

    std::string the_symbol;

    // Symbols constructor
    Symbols (std::string symbol){
        this->the_symbol = symbol;
    }

public:

    std::string get_symbol(void) {
        return the_symbol;
    }

    void set_symbol(std::string new_value) { the_symbol = new_value; }  

    void initialize(void){
        const Symbols CREATE = Symbols("create");
                     . . .
    }
};

Used in classes in several ways 在课堂上以多种方式使用

bool RecursiveDescentParser::type_name(){
if (accept(Symbols::STRING) || accept(Symbols::INTEGER)){
    return true;
}
else
    return false;

}

and

symbol_ = Symbols::WHERE;

if (!boost::iequals(tokenizer_->peekAtNext(), symbol_.get_symbol(), loc_)){
    // complete any tokens
    std::string temp = tokenizer_->completeToken();                     
}

The Symbols header is included in several classes. Symbols头包含在几个类中。 There are many compiler errors of the type 有很多类型的编译器错误

1>recursive_descent_parser.obj : error LNK2001: unresolved external symbol "public: static class Symbols const Symbols::VALUES" (?VALUES@Symbols@@2V1@B)

What gives? 是什么赋予了? I have received in all 39 unresolved externals and some of which are unrelated to this question. 我收到了所有39个尚未解决的外部问题,其中一些与该问题无关。 I am properly defining header files to ensure no duplicate declarations and all classes compile properly. 我正确定义了头文件,以确保没有重复的声明,并且所有类均正确编译。

You've declared the static member; 您已经声明了静态成员; but it also needs to be defined and initialised in exactly one source file. 但是还需要在一个源文件中对其进行定义和初始化。

const Symbols Symbols::CREATE("create");

I guess you think that's what the initialize function is doing; 我猜您认为这就是initialize函数正在执行的操作; but it's just creating a local object of type Symbols , then destroying it when the function returns. 但是它只是创建一个Symbols类型的本地对象,然后在函数返回时销毁它。

With static const Symbols CREATE; 使用static const Symbols CREATE; you only declare that a static const object of type Symbols is somewhere, which makes the compiler happy. 您只需声明一个Symbols类型的静态const对象在某个地方,这会使编译器感到满意。

But there needs to be some place where the CREATE object is actually defined, that's why the linker can't find it. 但是需要在某个地方实际定义CREATE对象,这就是链接器找不到它的原因。

You'll have to put 你必须把

const Symbols Symbols::CREATE;

in one of your cpp files. 在您的一个 cpp文件中。 That way a place of storage will actually be allocated so that the linker can find it. 这样,实际上将分配一个存储位置,以便链接程序可以找到它。

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

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