小编S. *_*ahl的帖子

使用命名空间与使用命名空间闭包的范围

我试图理解为什么当我使用 using 命名空间而不是显式声明命名空间外壳时,我的函数中存在歧义。

Book.h 头文件:

#ifndef MYBOOK_BOOK_H
#define MYBOOK_BOOK_H 

namespace mybook
{
    void showTitle();
    void showTableOfContents();
}

#endif
Run Code Online (Sandbox Code Playgroud)

我的实现文件导致歧义错误:Book.cpp

#include "Book.h"
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;
using namespace mybook;

void showTitle() {
    cout << "The Happy Penguin" << endl;
    cout << "By John Smith" << endl;
}

void showTableOfContents() {
     cout << "Chapter 1" << endl;
     cout << "Chapter 2" << endl;
}

Run Code Online (Sandbox Code Playgroud)

我的实现文件没有歧义错误:Book.cpp

#include "Book.h"
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

namespace …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces declaration definition name-lookup

1
推荐指数
1
解决办法
384
查看次数

标签 统计

c++ ×1

declaration ×1

definition ×1

name-lookup ×1

namespaces ×1