有两个类:MainWindow,Widget。Mainwindow类中有一个Widget类的成员变量widget。还有一个数组v[4000],Mainwindow和Widget两个类都要访问这个数组,我就在mainwindow.h中定义了这个数组,在widget.cpp中把它声明为全局变量,出现错误,如下:
multiple definition of 'v'
collect2: ld returned 1 exit status
大体结构如下:
mainwindow.h中:
#include "widget.h"
...
private:
Widget widget;
...
widget.h中:
#include "ui_widget.h"
#include "mainwindow.h"
extern double t[2][4000];
extern double v[2][4000];
...
我分析了一下,在mainwindow.h中包含了widget.h(因为要声明Widget类成员变量),而widget.h有要包含mainwindow.h(因为要把v[4000]声明为全局变量),这样会出现反复包含导致上面的错误,可能是这样,不知道是不是的。
有没有什么解决办法?还有其他方式解决吗,比如不用成员变量,友元函数,不把Widget设为Mainwindow的成员变量等等...
[ 此帖被friday1203在2009-11-08 20:03重新编辑 ]