Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can I not use the namespace directive in c++ struct?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4362831/scoped-using-directive-within-a-struct-class-declaration">Scoped using-directive within a struct/class declaration?</a><br> <a href="https://stackoverflow.com/questions/6326805/why-using-namespace-x-is-not-allowed-inside-class-struct-level">Why &ldquo;using namespace X;&rdquo; is not allowed inside class/struct level?</a> </p> </blockquote> <p>I would like to introduce only std::string into structure. Why is the below considered illegal?</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; struct Father { using std::string; string sons[20]; string daughters[20]; }; </code></pre> <p>But oddly enough, i can do the following the in a function</p> <pre><code>int main() { using std::string; } </code></pre> <p>Update: C++ uses the same keyword with different semantics. </p> <p>c++ uses the keyword "using" to introduce a data member or function from the base class into the current class. Thus when i wrote using std::string inside a struct declaration, compiler is assuming that i am trying to introduce a member from a base class std. But std is not a base class, rather it is a namespace. Thus </p> <pre><code>struct A { int i; } struct B:A { using A::i; // legal using std::string// illegal, because ::std is not a class } </code></pre> <p>the same "using" keyword is also used to access a member of a particular namespace.</p> <p>So, i am guessing compiler decides the semantics of "using" based on where it is declared.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload