-7

I currently define a string ID inside a class Return_Class for this to work I need to use "namespace." (I'm working in Qt.)

When I put using namespace std; my code functions perfectly, however when I remove using namespace std; and edit my class to...

class Return_Class : public QString
{
public:
    static std::QString ID;
};

...my code get the error message "QString in namespace "std" does not name a type"

I know that using std:: is considered better practice, but I'm confused as to why it doesn't work it my case? Is my syntax for the string wrong? Thanks

Mike Vine
  • 9,468
  • 25
  • 44
Jon CO
  • 25
  • 2
  • 10

2 Answers2

1

You are using QString, and QString is not from std namespace. When you directly type std::QString you will get an error, because QString is not defined in std namespace, but when you type using namespace std, You can use everything from std namespace without directly typing std (what is defined in that namespace), but not QString because definition of QString is not there.

Elvis Oric
  • 1,289
  • 8
  • 22
0

Qstring is not a part of the namespace, hence you are getting the error.

BTW , are you sure you wanted to use static std::QString ID; or is it static std::string ID;

And regarding difference between using "::std" or "using namespace std" It is always better to use "::std", because what if you are using two different namespaces, and they both have a function named func(). It will be ambiguous.

one more thing to note is that you should never put

using namespace std

In a header file, as it can propagate to all files that include that header file, even if they don't want to use that namespace.

Nishant
  • 1,635
  • 1
  • 11
  • 24