1. 程式人生 > >C++在另一個檔案建立名稱空間並呼叫

C++在另一個檔案建立名稱空間並呼叫

head.h

#ifndef KW_H
#define KW_H

namespace kw
{
    extern std::string name;
}

#endif // KW_H

head.cpp

#include <string>
#include "head.h"

namespace kw
{
    std::string name = "kwansumyuen";
}

main.cpp

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    cout << kw::name << endl;
    return 0;
}