【问题标题】:cpp Error Duplicate symbol using namespacecpp错误使用命名空间重复符号
【发布时间】:2019-11-21 17:21:42
【问题描述】:

这会让我发疯!

首先,我已经尝试了来自该线程(C++) Linking with namespaces causes duplicate symbol error 的解决方案,但没有成功(也许我做错了)

这是我想要做的:

// ofApp.hpp
#ifndef __ofApp_hpp
#define __ofApp_hpp

namespace vec_color {
    glm::vec4 blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    glm::vec4 blueViolet = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    glm::vec4 darkRed = glm::vec4( 200.f, 0.f, 0.f, 255.f );
    glm::vec4 green = glm::vec4( 0.f, 255.f, 0.f, 255.f );
    glm::vec4 grey = glm::vec4( 128.f, 128.f, 128.f, 255.f );
    glm::vec4 lightGoldenRodYellow = glm::vec4( 250.f, 250.f, 210.f, 255.f );
    glm::vec4 pink = glm::vec4( 255.f, 192.f, 203.f, 255.f );
    glm::vec4 red = glm::vec4( 255.f, 0.f, 0.f, 255.f );
    glm::vec4 yellow = glm::vec4( 255.f, 255.f, 0.f, 255.f );
    glm::vec4 white = glm::vec4( 255.f, 255.f, 255.f, 255.f );
};

...

#endif

然后我得到

duplicate symbol 'vec_color::grey' in:
    /Users/[...]/Build/Intermediates.

所以我尝试了

// ofApp.hpp
#ifndef __ofApp_hpp
#define __ofApp_hpp

namespace vec_color {
    extern glm::vec4 blue;
    extern glm::vec4 blueViolet;
    extern glm::vec4 darkRed;
    extern glm::vec4 green;
    extern glm::vec4 grey;
    extern glm::vec4 lightGoldenRodYellow;
    extern glm::vec4 pink;
    extern glm::vec4 red;
    extern glm::vec4 yellow;
    extern glm::vec4 white;
};

...

// ofApp.cpp

vec_color::blue       = glm::vec4( 0.f, 0.f, 255.f, 255.f );
vec_color::blueViolet = glm::vec4( 138.f, 43.f, 226.f, 255.f );
vec_color::darkRed    = glm::vec4( 200.f, 0.f, 0.f, 255.f );
vec_color::green      = glm::vec4( 0.f, 255.f, 0.f, 255.f );
vec_color::grey       = glm::vec4( 128.f, 128.f, 128.f, 255.f );
vec_color::lightGoldenRodYellow = glm::vec4( 250.f, 250.f, 210.f, 255.f );
vec_color::pink       = glm::vec4( 255.f, 192.f, 203.f, 255.f );
vec_color::red        = glm::vec4( 255.f, 0.f, 0.f, 255.f );
vec_color::yellow     = glm::vec4( 255.f, 255.f, 0.f, 255.f );
vec_color::white      = glm::vec4( 255.f, 255.f, 255.f, 255.f );

...

C++ requires a type specifier for all declarations

在 cpp 文件中的每一行变量声明。

如果有人有小费请告诉我。

弗兰克。

【问题讨论】:

  • 这不太可能是问题,但由于您使用__ofApp_hpp,您的程序的行为未定义:保留前导双下划线。真正的问题是按照你友好的编译器告诉你的去做来解决的。
  • 正如错误所说,您需要重复输入。请注意,您在 cpp 中如何将其链接为 bool nsTicTacToe::PlayerIsX;!也就是说,写glm::vec4 vec_color::blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );等就可以了。
  • 正如 Bathsheba 所说,不要使用包含双下划线或以单下划线开头的名称,既不要用于变量、类或函数,也不要用于类名。这些名称是为编译器/stdlib 实现保留的。
  • @eike:如果后跟一个大写字母,则开头只有一个下划线。
  • @Bathsheba 这取决于您的标识符所在的命名空间:eel.is/c++draft/lex.name#3.2 在全局命名空间中,即使是以单个下划线后跟小写字母开头的标识符也是保留的。

标签: c++ namespaces duplicate-symbol


【解决方案1】:

这种方式应该可行:

ofApp.hpp

#ifndef __ofApp_hpp
#define __ofApp_hpp

namespace vec_color {
    extern glm::vec4 blue;
    extern glm::vec4 blueViolet;
    extern glm::vec4 darkRed;
    extern glm::vec4 green;
    extern glm::vec4 grey;
    extern glm::vec4 lightGoldenRodYellow;
    extern glm::vec4 pink;
    extern glm::vec4 red;
    extern glm::vec4 yellow;
    extern glm::vec4 white;
};

...

#endif

ofApp.cpp

#include "ofApp.hpp"

namespace vec_color {
    glm::vec4 blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    glm::vec4 blueViolet = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    glm::vec4 darkRed = glm::vec4( 200.f, 0.f, 0.f, 255.f );
    glm::vec4 green = glm::vec4( 0.f, 255.f, 0.f, 255.f );
    glm::vec4 grey = glm::vec4( 128.f, 128.f, 128.f, 255.f );
    glm::vec4 lightGoldenRodYellow = glm::vec4( 250.f, 250.f, 210.f, 255.f );
    glm::vec4 pink = glm::vec4( 255.f, 192.f, 203.f, 255.f );
    glm::vec4 red = glm::vec4( 255.f, 0.f, 0.f, 255.f );
    glm::vec4 yellow = glm::vec4( 255.f, 255.f, 0.f, 255.f );
    glm::vec4 white = glm::vec4( 255.f, 255.f, 255.f, 255.f );
};

或者这样: ofApp.cpp

glm::vec4 vec_color::blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );
...

无论如何,以全局变量结束。不建议这样做。

【讨论】:

    【解决方案2】:
    vec_color::blue       = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    

    不起作用,因为您没有指定对象的类型。将其更改为

    glm::vec4 vec_color::blue       = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    

    对其他变量也进行类似的更改。


    正如在 cmets 中指出的那样,下面使用两个下划线

    #ifndef __ofApp_hpp
    #define __ofApp_hpp
    

    不能在应用程序代码中使用。它们仅供编译器编写者使用。请不要使用它们。最好将它们全部删除,使用大多数主流编译器都支持的#pragma once

    // ofApp.hpp
    #pragma once
    
    namespace vec_color {
        extern glm::vec4 blue;
        extern glm::vec4 blueViolet;
        extern glm::vec4 darkRed;
        extern glm::vec4 green;
        extern glm::vec4 grey;
        extern glm::vec4 lightGoldenRodYellow;
        extern glm::vec4 pink;
        extern glm::vec4 red;
        extern glm::vec4 yellow;
        extern glm::vec4 white;
    };
    

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多