【问题标题】:Multiple definition when compiling编译时多重定义
【发布时间】:2017-06-08 05:47:57
【问题描述】:

我有这些文件:

main.cpp
headers.h
calculator.h
calculator.cpp
Supported_operators.h
Supported_operators.c
My_Operations.h
My_Operations.cpp

我的headers.h 文件有(除了标准库):#include "Supported_operators.h",最后我的Supported_operators.h 有以下内容:

#ifndef CALCULATOR_SUPPORTED_OPERATORS_H
#define CALCULATOR_SUPPORTED_OPERATORS_H

#include "headers.h"
//using directives
class Supported_operator {
//stuff
};

list<Supported_operator> supportedOps = {
                                        Supported_operator('+', 2, 'l'),
                                        Supported_operator('-', 2, 'l'),
                                        Supported_operator('*', 3, 'l'),
                                        Supported_operator('/', 3, 'l'),
                                        Supported_operator('^', 4, 'r')
                                        };

#endif //CALCULATOR_SUPPORTED_OPERATORS_H

当我尝试编译程序时,编译器在我的main() 上显示multiple definition of supportedOps[abi:cxx11]

我该如何解决这个问题?

上下文:我正在尝试制作一个计算器。必须将string 输入拆分为令牌。如果 token 是运算符,则根据该运算符执行某些操作。所以我必须创建一个SupportedOperators 的容器,然后检查是否支持标记运算符以及它的属性是什么。

编辑 2: 我的 headers.h 文件现在包含 const list&lt;Supported_operator&gt; supportedOps; 和我的 Supported_operators.cpp 现在有 list&lt;Supported_operator&gt; supportedOps = { }; 但随后 headers.h 触发错误 list does not name a type

编辑 3:headers.h 中的声明更改为 extern const std::list&lt;&gt; supportedOps; 现在会导致一组新错误 - Supported_operator 未在该范围内声明。我以为我已经用#include "Supported_operators.h 覆盖了它,其中声明了Supported_operator。我是否正确地假设编译器还不知道 Supported_operator 类?我该如何解决这个问题?

【问题讨论】:

  • list&lt;Supported_operator&gt; supportedOps - 那个东西属于翻译单元(即cpp文件)和标题中的extern匿名命名空间,或 作为static.

标签: c++ multiple-definition-error


【解决方案1】:

你应该在标题中留下list&lt;Supported_operator&gt; supportedOps;并添加

Supported_operator('+', 2, 'l'),
        Supported_operator('-', 2, 'l'),
        Supported_operator('*', 3, 'l'),
        Supported_operator('/', 3, 'l'),
        Supported_operator('^', 4, 'r')
        };

到您的 Supported_operators.c(pp)

【讨论】:

  • 我试过了,但是在我的Supported_operators.cpp 哪里supportedOps = { Supported_operator('+', 2, 'l'), etc}; 现在有Excess elements in scalar initializer 的问题,编译器也说supportedOps does not name a type
  • @HichigayaHachiman list supportedOps = {/*your stuff*/};,你需要在cpp文件中指定你的变量类型。
  • 好的,我编辑了我的代码,我的headers.h 文件现在包含const list&lt;Supported_operator&gt; supportedOps; 和我的Supported_operators.cpp 现在有list&lt;Supported_operator&gt; supportedOps = { }; 但随后headers.h 触发错误list does not name a type
  • 在添加 extern const std::list&lt;&gt;... 之后,有一个新的挑战 - 显然 Supported_operator 没有在这个范围内声明,即使我有 `#include "Supported_operators.h"。
【解决方案2】:

如果不需要更改supportedOps,也可以直接在变量前面加上const。

const list<Supported_operator> supportedOps = { }

这将通过 ODR 检查,因为所有翻译单元中的变量都保证是相同的。

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多