Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access declarations are deprecated in favor of using-declarations; suggestion: add the ‘using’ keyword

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?

like image 803
user3750325 Avatar asked Mar 20 '23 07:03

user3750325


1 Answers

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.

like image 71
Angew is no longer proud of SO Avatar answered Mar 22 '23 04:03

Angew is no longer proud of SO