2

This is going to make me nuts !

First of all I already tried solutions from this thread (C++) Linking with namespaces causes duplicate symbol error with no success (maybe I doing it wrong)

Here is what i am attempted to do:

// 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

And I then obtain

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

So I tried

// 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

on each lines variable declaration in cpp file.

I'm stuck if someone have a tip please tell me.

Franck.

S7th
  • 25
  • 4
  • Very very unlikely it's the issue but the behaviour of your program is undefined due to your using `__ofApp_hpp`: leading double underscores are reserved. The real issue is solved by doing what your friendly compiler is telling you to. – Bathsheba Nov 21 '19 at 17:25
  • As the error says, you need to repeat the type. Notice how in the question you linked it's **`bool`** `nsTicTacToe::PlayerIsX;` in the cpp! In other words, write `glm::vec4 vec_color::blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );` etc. and it will work. – Max Langhof Nov 21 '19 at 17:25
  • As Bathsheba said, do not use names containing double underscores or beginning with single underscores, neither for variables, classes or functions, nor for class names. These names are reserved for the compiler / stdlib implementation. – eike Nov 21 '19 at 17:31
  • @eike: Only single underscores at the start if followed by an upper case letter. – Bathsheba Nov 21 '19 at 17:35
  • Thanks every one for your quick answers. I take note for the double underscore notation. – S7th Nov 21 '19 at 17:38
  • 1
    @Bathsheba That depends on the namespace your identifier lives in: https://eel.is/c++draft/lex.name#3.2 In the global namespace, even identifiers beginning with a single underscore followed by a lowercase letter are reserved. – eike Nov 21 '19 at 17:38
  • @eike, that's a drink I owe you if you're ever in London. – Bathsheba Nov 21 '19 at 17:39
  • @Bathsheba I hope, I remember that until I am in London. I just recently learned that from a C++ weekly (https://youtu.be/0vJXQ2VOzEs) – eike Nov 21 '19 at 17:41

2 Answers2

0

This way should work:

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 );
};

or this way: ofApp.cpp

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

Anyway this way you end with global variables. This is not recommended.

Marek R
  • 32,568
  • 6
  • 55
  • 140
0
vec_color::blue       = glm::vec4( 0.f, 0.f, 255.f, 255.f );

does not work since you didn't specify the type of the object. Change that to

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

Make similar changes to the other variables also.


As has been pointed out in comments, use of two underscores below

#ifndef __ofApp_hpp
#define __ofApp_hpp

is not to be used in application code. They are reserved for use only by compiler writers. Please don't use them. It's best to remove them altogether and use #pragma once, which is supported by most mainstream compilers.

// 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;
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270