請教下各位哈! 在.cpp的檔案寫了個命名空間,命名空間里面有一個函式,.h檔案要如何宣告這個命名空間呢,否則主函式無法呼叫這個命名空間
比如test.cpp檔案為
#include "test.h"
#include <iostream>
using namespace std;
namespace First
{
void print(void)
{
cout << "Hello,world!" << endl;
}
}
那么.h 檔案要如何宣告出這個First命名空間呢,目的是為了讓主函式可以呼叫First命名空間里面的print函式
uj5u.com熱心網友回復:
以下有個例子,你做參考:Point2D.h
namespace d2d { //<<<<
int i, j, k;
class Point2D {
public:
Point2D() {
_x = 0;
_y = 0;
}
Point2D(int x, int y) : _x(x), _y(y) {
}
int x() {return _x;}
int y() {return _y;}
private:
int _x;
int _y;
};
};
//--------------------------
main.cpp
#include <iostream>
#include "Point2D.h"
using namespace std; //<<<<
int main() {
d2d::Point2D p1(10, 10); //<<<<
cout << "(x , y) : ("
<< p1.x() << ", "
<< p1.y() << ")"
<< endl;
return 0;
}
uj5u.com熱心網友回復:
.h檔案:namespace First
{
void print(void);
}
.cpp檔案:
namespace First
{
void print(void)
{
cout << "Hello,world!" << endl;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130046.html
標籤:基礎類
下一篇:TS流的一些問題
