我仔細查看了我的代碼,但不明白爲什麼會出現此錯誤。C++錯誤:神祕的衝突聲明錯誤
錯誤消息是以下各項:
main.cc: In function ‘int main()’:
main.cc:12: error: conflicting declaration ‘traj dim’
main.cc:11: error: ‘dim’ has a previous declaration as ‘unsigned int dim’
和一個可以用下面的命令重現它
g++ -o a.out realvector.cc traj.cc main.cc
我main.cc是
#include "realvector.h"
#include "traj.h"
using namespace std;
int main() {
unsigned int dim=1000;
traj TRAJ(dim);
return 1;
}
TRAJ中定義traj.h as
#ifndef TRAJ
#define TRAJ
#include "realvector.h"
class traj{
public:
traj(unsigned int);
~traj();
void next(double &);
private:
unsigned int it,nt; // index, total array size
double dt; // step time
RealVector r,v,a;
};
#endif
構造在traj.cc
#include "realvector.h"
#include "traj.h"
traj::traj(unsigned int dim) : nt(dim) {
RealVector r(nt),v(nt),a(nt);
it=0;
}
traj::~traj(){
r.~RealVector();
}
定義的任何想法,爲什麼這個錯誤出來?另外,定義r,v是否正確的方法是? RealVector是家庭自定義的類它的構造函數定義如下
#include "realvector.h"
using namespace std;
RealVector::RealVector() {}
RealVector::RealVector(unsigned int n)
: dim(n) {
data = new double[dim];
for (int i=0; i<dim; i++)
data[i]=0;
}
RealVector::~RealVector(){
delete[] data;
}
與realvector.h爲
#ifndef REAL_VECTOR_H
#define REAL_VECTOR_H
#include <iostream>
class RealVector {
public:
RealVector();
RealVector(unsigned int n);
~RealVector();
int dim;
double* data;
};
#endif
當然你已經在main.cpp中包含了'traj.h'? –
未發現問題http://ideone.com/PNPsY2 –
@bsmile:你用'traj :: traj(unsigned int dim)初始化r,v,a:nt(dim),r(nt),v(nt) ,(nt){...'。但是,在這之前你應該可以使'traj :: traj(unsigned int dim,unsigned int nt)'。 – PrestonH