-2

Something interesting and wrong about my code.

#include <iostream>
void func(){
    using namespace std;
}
main(){
    func(); //Here the function will introduce the (using namespace std declaration) in the code
    cout << "Hello World!";
    return (0);
}

When compiled the error message is shown:

atizva@atizva:~/Documents/C++/Programs$ g++ -o func function_call.cpp
function_call.cpp: In function ‘int main()’:
function_call.cpp:7:2: error: ‘cout’ was not declared in this scope
  cout << "Hello World!";
  ^~~~
function_call.cpp:7:2: note: suggested alternative:
In file included from function_call.cpp:1:0:
/usr/include/c++/7/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^~~~

I don't understand why the function 'func()' doesn't call the tag: 'using namespace std' appropriately.

basgys
  • 4,320
  • 28
  • 39
  • 5
    You really need to learn about scope. Might want to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Rakete1111 May 17 '18 at 16:19
  • 3
    Nobody can compile that picture. Nobody can edit or format that picture. Nobody can google terms in that picture. Nobody can screen read or high contrast or large print that picture. – Drew Dormann May 17 '18 at 16:19
  • Use MACRO instead of function to forget scope (whereas we need to suggest the opposite most of the time) ;-) – Jarod42 May 17 '18 at 16:22
  • "How can I call a namespace ..." - One does not *call* namespaces. – Jesper Juhl May 17 '18 at 16:24
  • Like all other declarations, the effects of a `using namespace ...` end at the corresponding `}`. – Caleth May 17 '18 at 16:30
  • Why is this question being treated unfairly and marked as "off-topic"? Yes the OP posted images rather than text, however he/she tried something and are seeking advice why what they tried is not working. This question purpose is exactly the same as another question where the OP doesn't understand why something is not working, and the other questions received upvotes! – Kakalokia May 17 '18 at 16:30
  • @AliAlamiri read the specific close reason. Editing the question to not be pictures of code is necessary and sufficient to re-open – Caleth May 17 '18 at 16:31
  • @Caleth It seems to be rectified now – Tim Johns May 17 '18 at 16:34
  • 1
    @Caleth ... I have read the close reason, and to me it contradicts itself. It says the question must include the desired behaviour, and clearly the OP desired behaviour is to use std, but he/she has confusion around scope. The question is perfectly fine, the only problem people seem to be having with it is the images, which a simple edit by a moderator fixes. It almost feels people on SO are in a hurry to close questions! – Kakalokia May 17 '18 at 16:36
  • Sorry, first time in the forum. Thank's tho, for letting me know about the 'precautions' to take before posting. – Atizva Delequarc May 17 '18 at 16:37
  • @Tim Johns and there are 3 re-open votes to reflect that, including mine. – Jesper Juhl May 17 '18 at 16:47
  • @AliAlamiri this question went through many revisions. The question, as originally posted, did not have _"the shortest code necessary to reproduce it in the question itself."_. There was no code at all in this question -- only imgur links, which made this question off-topic. Voting to reopen. – Drew Dormann May 17 '18 at 16:54
  • @AliAlamiri As I was writing that comment, hlt's edit was occuring. I am one of the re-open votes – Caleth May 17 '18 at 17:22

2 Answers2

2

To fix this, you would have to move using namespace std; outside of func(). The reason this fails in your current code is that using declaration only applies within the scope that it is called (in this case func()). So once you exit func(), you lose the effects of using namespace std;

Tim Johns
  • 540
  • 3
  • 13
0

You assume that namespaces are enabled at runtime, but namespaces are only meaningful at compile time. What you are doing is to limit the use of std to the scope of the function func(). That is, it allows you to type

cout

inside that function, but not elsewhere.

  • 1
    Your first sentence about runtime vs. compile time is correct, but I suspect that it would be very confusing to a novice. Given that this question is related to not understanding a fundamental concept like scope, it seems likely that your first sentence would be more confusing than helpful. – Tim Johns May 17 '18 at 16:30