4

I returned to one of my old C++ school assignments which implemented a binary tree. I have a file (Tree.cpp) that contains functions to insert, lookup, remove, etc nodes. At the top, I have "using namespace std;". The warnings I am getting are caused by another file, SymTab.hpp, that looks like this:

#ifndef SYMTAB_H
#define SYMTAB_H

#include <iostream>
#include "Tree.hpp"
using namespace std;

template <class Whatever>
class SymTab : private Tree<Whatever> {
public:
        Tree<Whatever> :: Insert;
        Tree<Whatever> :: Lookup;
        Tree<Whatever> :: Remove;
        Tree<Whatever> :: Write;
        Tree<Whatever> :: Set_Debug_On;
        Tree<Whatever> :: Set_Debug_Off;
};

#endif

Each of the lines after public: give a warning like:

"SymTab.hpp:11:9: warning: access declarations are deprecated in favour of using-declarations; suggestion: add the ‘using’ keyword [-Wdeprecated] Tree :: Insert;", where "Insert is replaced with each respective function name.

Any advice on namespaces and how to get rid of these warnings?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3750325
  • 1,502
  • 1
  • 18
  • 37
  • 1
    Add the using keyword as it suggests? – doctorlove Jun 18 '14 at 15:53
  • 3
    Never ever ever put a using directive in a header. That means anything that includes that header gets the whole of `std`. If there's a conflict in there and they want to use your header, your header will probably lose. – ghostofstandardspast Jun 18 '14 at 15:54

1 Answers1

7

There's two separate issues. The one the compiler is talking about are the "access declarations" in SymTab. Simply change it to this:

template <class Whatever>
class SymTab : private Tree<Whatever> {
public:
        using Tree<Whatever> :: Insert;
        using Tree<Whatever> :: Lookup;
        using Tree<Whatever> :: Remove;
        using Tree<Whatever> :: Write;
        using Tree<Whatever> :: Set_Debug_On;
        using Tree<Whatever> :: Set_Debug_Off;
};

The other, totally unrelated issue is using namespace std; in a header file. That's not an error per se, but a Bad IdeaTM. It causes the entire std namespace to be dumped into the global namespace for everyone who includes that header and there's nothing they can do about it. And it can lead to wonderful name conflicts with some common names like transform, list or sort which are defined in the std namespace. Just remove the using directive.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455