Pella86
Pella86

Reputation: 500

Qt namespace duplicate id error

I hope is not a question too specific... But I will be really thankfull if you could help!

when I build my application I get this error, do you know why?:

ld: duplicate symbol colors::white      in mainwindow.o and main.o
collect2: ld returned 1 exit status

Here is the definition of the main:

#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

and here is the definition of main window:

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    glwidget = new MyGLBox(ui->centralWidget);
    startTimer(11);

//test frame
    BasicShapes shapes;

    Polygon3 square = shapes.create_square(V3(0.,0.,0.),V3(1.0,1.0,1.),colors::cyan);
    Polygon3 cube = shapes.create_cube(V3(-1.,-1.,-1.),V3(1.,1.,1.),colors::purple);
    Polygon3 triangle = shapes.create_triangle(V3(0,1.,0),V3(1.,1.,1.),5.,colors::green);
    //other stuff...

    ui->vLayoutGLview->addWidget(glwidget); //add the glwidget to the ui framework 
}

colors is a namespace defined in the file colors.h:

namespace colors{
Color white(1.,1.,1.,0.);
Color black(0.,0.,0.,0.);
Color red(1.,0.,0.,0.);
Color green(0.,1.,0.,0.);
Color blue(0.,0.,1.,0.);
Color yellow(1.,1.,0.,0.);
Color purple(1.,0.,1.,0.);
Color cyan(0.,1.,1.,0.);
}

here is my file list:

colors.h

#include <string>
#include <iostream>
#include <sstream>

mainwindow.h

#include <QMainWindow>
#include "myglbox.h"
#include "sceneobject.h"

mathvector.h

#include <vector>
#include <cmath>
#include <string>
#include <iostream>
#include <sstream>    
#include "colors.h"

myglbox.h

#include <QGLWidget>
#include <QtOpenGL>
#include <vector>
#include "mathvector.h"

sceneobject.h

#include "mathvector.h"

I'm pretty sure I didn't included two time the same file(but maybe I missed it), and anyway the header are protected by the header guards.

do you have any idea why I get a duplicate id error? without the namespace it compiled fine, but a namespace with the standard color will be really useful...

Upvotes: 0

Views: 529

Answers (1)

Chad
Chad

Reputation: 19052

Objects defined in a header file will be duplicated in every compilation unit that includes that header, leading to duplicate definition errors. These should either be extern (and defined elsewhere), or (my preference) made into free functions:

Colors.h

namespace color
{
    const Color& white();
    const Color& black();
    // etc...
}

Colors.cpp

#include "colors.h"

namespace color
{
    const Color& white()
    {
       static Color w(1.,1.,1.,0.);
       return w;
    }

    const Color& black()
    {
       static Color b(0., 0., 0., 0.);
       return b;
    }
}

Then you can use them easily as:

Color white_copy = colors::white();
const Color& black = colors::black();

Upvotes: 5

Related Questions