Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6728779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:12:38+00:00 2026-05-26T10:12:38+00:00

I hope is not a question too specific… But I will be really thankfull

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:12:39+00:00Added an answer on May 26, 2026 at 10:12 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope this question is not considered too basic for this forum, but we'll
I hope this question is not too silly, but what is the most basic
I hope the question is not too generic, but I would need an advice:
i hope this question is not too simple, but i have no idea :(
I hope this question is not a duplicate but I didn't find anything equivalent
It's not quite a programming question, but I hope its related closely enough. Do
I hope this question is not too off-topic, if it is this post can
I hope the question is not too unspecific. At the moment, I am trying
I hope I am not asking a too obvious question here. For my current
I hope I'm not being too long winded in this question, I just want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.