+ 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?
5 odpowiedzi
+ 3
Please link the code from the playground in the future.
Posting code directly inside the thread is messy.
+ 3
Do this for classes.
Class new1
{
public:
// functions
//edit
};
+ 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
+ 3
I was wrong.
@Shaldem
I did not know namespace, could be used like that.
Thank you for the info and sharing your code!
+ 2
namespace new1;
Does not exist. Delete it.
You should have used Class there.
namespace is not used like that.