C++ 名称空间&;未定义的引用
我有两组文件,一组使用'namespace',另一组使用'using namespace',当我使用后者时,我得到一个未定义的引用错误,如下所示:C++ 名称空间&;未定义的引用,c++,reference,namespaces,undefined,using,C++,Reference,Namespaces,Undefined,Using,我有两组文件,一组使用'namespace',另一组使用'using namespace',当我使用后者时,我得到一个未定义的引用错误,如下所示: tsunamidata.cpp:(.text+0x1a0): undefined reference to `NS_TSUNAMI::ReadTsunamiData(IntVector2D, std::vector<std::vector<double, std::allocator<double> >, std::al
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
tsunamidata.cpp:(.text+0x1a0):对“NS_Tsunamia::ReadTsunamiData(IntVector2D,std::vector)”的未定义引用
collect2:ld返回了1个退出状态
这是源文件:
#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];
}
#包括“tsunamidata.h”
使用名称空间std;
使用名称空间NS_TSUNAMI//如果我用“名称空间NS_TSUNAMI”替换它,它会工作!!
vectorReadTsunamiGrid(IntVector2D位置){
河流充填;
填充开放(“H11_V33网格_wB_grdcor_淹没。grd”);
向量范围;
填充>>范围[0].x;
填充>>范围[0].y;
填充>>范围[1].x;
填充>>范围[1].y;
IntVector2D dxdy,大小;
infle>>dxdy.i;infle>>dxdy.j;
infle>>size.i;infle>>size.j;
对于(int j=size.j-1;j>-1;j--)
for(int i=0;i>海啸网格[j][i];
返回网格;
}
布尔代理溺水(IntVector2D agnt位置){
ReadTsunamiData(agnt_位置,海啸网格);
返回true;
}
double ReadTsunamiData(IntVector2D pos,vector 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
\ifndef海啸
#定义海啸
#包括“./../Basic_headers.h”
海啸{
向量<向量>海啸网格;
bool Agent_溺水(IntVector2D agnt_pos);
矢量读取Tsunamigrid(IntVector2D位置);
双读海啸数据(IntVector2D位置,矢量海啸深度);
}
#恩迪夫
我不明白为什么会发生错误,因为它只发生在该函数中 在头文件中,该函数位于命名空间内。函数的定义必须类似地位于命名空间中,以便编译器知道namelookup。using namespace命令用于从名称空间调用函数以使用速记,即cout而不是std::cout。在头文件中,该函数位于名称空间内。函数的定义必须类似地位于命名空间中,以便编译器知道namelookup。using namespace命令用于从命名空间调用函数以使用速记,即cout而不是std::cout。名称不合格的函数定义同时是当前命名空间中相同函数的定义和声明
namespace A {
void foo(); // declares ::A::foo
}
void A::foo() {} // qualified, definition only, depends on previous
// declaration and provides A::foo
void foo() {} // unqualified, both declaration and definition of ::foo
using namespace NS_TSUNAMI;
bool Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
名称不合格的函数定义同时是当前命名空间中同一函数的定义和声明
namespace A {
void foo(); // declares ::A::foo
}
void A::foo() {} // qualified, definition only, depends on previous
// declaration and provides A::foo
void foo() {} // unqualified, both declaration and definition of ::foo
using namespace NS_TSUNAMI;
bool Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
这将在命名空间内声明一个函数
namespace A {
void foo(); // declares ::A::foo
}
void A::foo() {} // qualified, definition only, depends on previous
// declaration and provides A::foo
void foo() {} // unqualified, both declaration and definition of ::foo
using namespace NS_TSUNAMI;
bool Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
这将在全局命名空间中声明并定义不同的函数。using指令使名称空间中的名称在全局名称空间中可用,但不会更改声明的含义
要定义以前在命名空间中声明的函数,请将其放入命名空间中:
namespace NS_TSUNAMI {
bool Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
}
或限定名称:
bool NS_TSUNAMI::Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
这将在命名空间内声明一个函数
namespace A {
void foo(); // declares ::A::foo
}
void A::foo() {} // qualified, definition only, depends on previous
// declaration and provides A::foo
void foo() {} // unqualified, both declaration and definition of ::foo
using namespace NS_TSUNAMI;
bool Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
这将在全局命名空间中声明并定义不同的函数。using指令使名称空间中的名称在全局名称空间中可用,但不会更改声明的含义
要定义以前在命名空间中声明的函数,请将其放入命名空间中:
namespace NS_TSUNAMI {
bool Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
}
或限定名称:
bool NS_TSUNAMI::Agent_drowned(IntVector2D agnt_pos) {
// whatever
}
查看我的答案(不是选择的答案)。查看我的答案(不是选择的答案)。