Problem learning Namespace in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Problem learning Namespace in C++

This is my C++ code:-\ #include<iostream> using namespace std; namespace new1 { void func { std::cout<<"new1"; } } namespace new2 { void func { std::cout<<"new2"; } } main() { std::cout<<"In main."; new1::func(); } And this is the error message:- ..\Playground\:5:7: error: variable or field 'func' declared void void func ^ ..\Playground\:7:20: error: expected '}' before ';' token std::cout<<"new1"; ^ ..\Playground\:9:1: error: expected declaration before '}' token } ^ Can anyone explain what is wrong here?

18th Jun 2017, 4:16 PM
Sayan Kundu
5 Answers
+ 3
Please link the code from the playground in the future. Posting code directly inside the thread is messy.
18th Jun 2017, 6:31 PM
Manual
Manual - avatar
+ 3
Do this for classes. Class new1 { public: // functions //edit };
18th Jun 2017, 6:34 PM
Manual
Manual - avatar
+ 3
Hm, where are sintaxes error in your code, you need to write void func() {} and it will be ok. Namespace is ok, and use certainly soo. https://code.sololearn.com/c8f4V61yovz3/?ref=app
18th Jun 2017, 6:55 PM
shaldem
shaldem - avatar
+ 3
I was wrong. @Shaldem I did not know namespace, could be used like that. Thank you for the info and sharing your code!
18th Jun 2017, 7:24 PM
Manual
Manual - avatar
+ 2
namespace new1; Does not exist. Delete it. You should have used Class there. namespace is not used like that.
18th Jun 2017, 6:30 PM
Manual
Manual - avatar