我试图理解为什么当我使用 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)