[英]namespaces in headers in c++
我有一個名稱空間問題。 它說“ phys1 :: x的多個定義”,為什么? 看一下我的代碼:
main.cpp
#include <cstdlib>
#include <iostream>
#include "abcd.h"
using namespace phys1;
using namespace std;
int main(){
cout << "SD " << tits() << endl;
system("pause");
return 0;
}
abcd.h
#ifndef _ABCD_H_
#define _ABCD_H_
namespace phys1
{
double xx = 9.36;
}
double tits();
#endif
abcd.cpp
#include "abcd.h"
double tits(){
return phys1::xx;
}
double xx = 9.36;
是一個定義,您不能在多個翻譯單元中定義相同的符號。
您可以使用const
,它提供變量內部鏈接,也可以使用static
:
//can't modify the value
const double xx = 9.36;
//can modify the value
//each translation unit has its own copy, so not a global
static double xx = 9.36;
或對於真正的全球外部人:
extern double xx;
//cpp file
double xx = 9.36;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.