English 中文(简体)
盖特名称空间重复错误
原标题:Qt namespace duplicate id error
  • 时间:2011-10-20 20:03:13
  •  标签:
  • c++
  • qt

我希望这不是一个太具体的问题。 但是,如果你能够帮助,我将真的感谢!

当我提出我的申请时,我会发现这一错误?

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

主要定义如下:

#include <QtGui/QApplication>
#include "主要窗口。"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

这里是主要窗口的定义:

#include "ui_主要窗口。"

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 
}

彩色是文档颜色中界定的一个名称空间:

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

我的档案清单如下:

颜色。

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

主要窗口。

#include <QMainWindow>
#include "Igl Box.h"
#include "景象"

数学博士

#include <vector>
#include <cmath>
#include <string>
#include <iostream>
#include <sstream>    
#include "颜色。"

Igl Box.h

#include <QGLWidget>
#include <QtOpenGL>
#include <vector>
#include "数学博士"

景象

#include "数学博士"

我确知,我只字不提同一档案(但我可能错过)的两倍,任何航程都由警卫保护。

你们有什么想法,为什么我出现重复错误? 如果没有它所汇编的字面空间,那么一个具有标准颜色的名称空间将真正有用。

最佳回答

每个汇编单位,包括该负责人,将重复在目录中定义的物体,从而造成定义错误。 这些规定要么是extern(并在别处界定),要么是(我的偏好)赋予自由职能:

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

然后,你可以轻而易举地使用:

Color white_copy = colors::white();
const Color& black = colors::black();
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签