多个定义错误:如何在头文件中声明引用?

时间:2012-09-24 07:52:43

标签: c++ qt reference header

qtGUI.o:(.bss+0x0): multiple definition of `R'
rWidgetPlots.o:(.bss+0x0): first defined here
qtGUI.o:(.rodata+0x0): multiple definition of `qtToR'
rWidgetPlots.o:(.rodata+0x0): first defined here
main.o:(.bss+0x0): multiple definition of `R'
rWidgetPlots.o:(.bss+0x0): first defined here
main.o:(.rodata+0x0): multiple definition of `qtToR'
rWidgetPlots.o:(.rodata+0x0): first defined here
moc_qtGUI.o:(.bss+0x0): multiple definition of `R'
rWidgetPlots.o:(.bss+0x0): first defined here
moc_qtGUI.o:(.rodata+0x40): multiple definition of `qtToR'
rWidgetPlots.o:(.rodata+0x0): first defined here

rWidgetPlots.h

#ifndef RWIDGETPLOTSH
#define RWIDGETPLOTSH

#include <iostream>
#include <RInside.h>
#include <Rcpp.h>
#include <vector>
#include <limits>
#include <QRect>
#include <fstream>
#include "zoomAndCornerDatabaseParser.h"

RInside R (0, NULL);
RInside & qtToR (R);

typedef struct
{
        double       latitude;
        double       longitude;
        unsigned int vehicleId;
} vehicle;

extern std :: vector <vehicle> vehicleInfo;

typedef struct
{
    float latitude;
    float longitude;
} coordinate;

extern std :: vector <coordinate> previousPoints;

class rdaTilesOnR
{
    public:
        static std :: string returnCenterPoint    (std :: string fileName);
        static std :: string findPanningDirection (float newLatitude, float newLongitude,
                        std :: string topLeftLat, std :: string topLeftLng, 
                        std :: string northEastLat, std :: string northEastLng, 
                    std :: string bottomRightLat, std :: string bottomRightLng, 
                    std :: string southWestLat, std :: string southWestLng);

        static void storeVehicleInfo  (float latitude, float longitude, unsigned int vehicleId);

        static void loadNewTileAndPlotPointsOnRWidget (unsigned short option, float newLongitude, float newLatitude);

        static std :: string doesPointLieInBoundaries  (unsigned short option, float newLatitude, float newLongitude, float currentCenterLatitude, float currentCenterLongitude);
};

#endif

qtGUI.h

#ifndef QTGUIH
#define QTGUIH

#include <QApplication>
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include <QPushButton>
#include <unistd.h>
#include <iostream>
#include <string.h>

#include "rWidgetPlots.h"

class controlRThroughQt : public QWidget
{
    Q_OBJECT  

    QTextEdit  *textEdit;

    public:
        void qtInterface ();

    public slots:
        void slotPanLeft     ();
        void slotPanRight   ();
        void slotPanTop      ();
        void slotPanBottom ();
        void slotZoomIn      ();
        void slotZoomOut    ();
        void slotRefresh      ();
        void findInfoByClicking ();
};

#endif

qtToR这是一个参考。它必须在那时和那里定义。如何解决此错误?使用extern无效。

1 个答案:

答案 0 :(得分:6)

  

必须在那时定义。

那么,它无法完成。如果您愿意将其留在标题中:

extern RInside R;
extern RInside & qtToR;

并将定义移动到单个实现文件中:

//somecpp.cpp
RInside R (0, NULL);
RInside & qtToR (R);

那就是它。