看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Dev C++ 、 g++ 小弟最近跟哥哥要了學校講義自學C++, 裡面提到解決name collision的其中之一個方法是使用namespace, 不過不知道是自己哪裡做錯, 小弟試著跟著範例code做一遍卻一直卡關, 以下是我自己打的code: __________main.cpp__________ #include <iostream> #include "s1.h" using namespace std; float PI =3.14; int main(){ cout << "main" << endl; cout << "PI = " << PI << endl; PI5::func1(); return 0; } __________s1.h__________ void func1(); __________s1.cpp__________ #include <iostream> using namespace std; namespace PI5{ float PI = 5; void func1(){ cout << "func1" << endl; cout << "2PI = " << 2*PI; cout << endl; } } 依照講義中的說法, 理想中的ouput應該是: main PI = 3.14 func1 2PI = 10 但實際編譯卻會出現'PI5' has not been declared的錯誤, 小弟已經在此卡關許久..., 還請各位前輩幫小弟指點迷津! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.116.219.97 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1588535678.A.358.html
KaryuuIssen: s1.h改成 namespace PI5 { void func1(); } 05/04 04:05
ou9810: 宣告跟實作都要在同個namespace 05/05 10:34