I'm trying to understand why there is ambiguity in my functions when I use using namespace versus explicitly declaring a namespace enclosure.
Book.h header file:
#ifndef MYBOOK_BOOK_H
#define MYBOOK_BOOK_H
namespace mybook
{
void showTitle();
void showTableOfContents();
}
#endif
My implmenetation file which causes an ambiguity error: 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;
}
My implementation file which does not have an ambiguity error: Book.cpp
#include "Book.h"
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
namespace mybook {
void showTitle() {
cout << "The Happy Penguin" << endl;
cout << "By John Smith" << endl;
}
void showTableOfContents() {
cout << "Chapter 1" << endl;
cout << "Chapter 2" << endl;
}
}
I would think that the first scenario of Book.cpp should work because by declaring using namespace mybook at the beginning it is saying that I am now going to implement the functions I defined in the header file. However I get the errors of "error 'showTitle': ambiguous call to overload function could be 'void showTitle(void) or void mybook::showTitle(void)'" and same for my other function showTableOfContents. Why does using namespace mybook in the first scenario not work?