我有两组文件,一组使用'namespace',另一组使用'using namespace',当我使用后者时,我得到一个未定义的引用错误,如下所示:
tsunamidata.cpp:(.text+0x1a0): undefined reference to `NS_TSUNAMI::ReadTsunamiData(IntVector2D, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >)'
collect2: ld returned 1 exit status
这是源文件:
#include "tsunamidata.h"
using namespace std;
using namespace NS_TSUNAMI; //if I replace this with 'namespace NS_TSUNAMI' it works!!
vector< vector<double> > ReadTsunamiGrid(IntVector2D pos) {
ifstream infile;
infile.open("H11_V33Grid_wB_grdcor_inundated.grd");
vector< Vector2D> range;
infile>>range[0].x;
infile>>range[0].y;
infile>>range[1].x;
infile>>range[1].y;
IntVector2D dxdy,size;
infile>>dxdy.i; infile>>dxdy.j;
infile>>size.i; infile>>size.j;
for (int j = size.j-1; j>-1; j--)
for(int i=0; i < size.i; i++)
infile>>tsunami_grid[j][i];
return tsunami_grid;
}
bool Agent_drowned(IntVector2D agnt_pos) {
ReadTsunamiData(agnt_pos, tsunami_grid );
return true;
}
double ReadTsunamiData(IntVector2D pos, vector<vector<double> > tsunami_depth) //Function which causes the error!!
{
return tsunami_depth[pos.j][pos.i];
}
头文件:
#ifndef TSUNAMIDATA_H
#define TSUNAMIDATA_H
#include "../../Basic_headers.h"
namespace NS_TSUNAMI {
vector< vector <double> > tsunami_grid;
bool Agent_drowned(IntVector2D agnt_pos);
vector<vector<double> > ReadTsunamiGrid(IntVector2D pos);
double ReadTsunamiData(IntVector2D pos, vector<vector<double> > tsunami_depth);
}
#endif
我不明白为什么会发生错误,因为它只发生在那个功能上?