My code is like below, when I compile it , I receive this error :
two.cpp:5: error: expected constructor, destructor, or type conversion before ‘=
‘ token
#include <iostream>
using namespace std;
namespace a1{
int a=3;
a=4;
}
int main(){
cout << a1::a<<endl;
}
I encountered this problem when I defined a namespace in two files, in the second file, I can’t assign a value to a variable which defined in the first file.
I am learning Beginning ANSI C++ , and can’t find any information about this in the book.
The purpose of namespaces is to avoid conflicting names. So surround your variable and class declarations with namespaces. But namespaces by themselves don’t provide the scaffolding for running code. What is going on with your a=4; statement ? Where is that supposed to execute ? You need to put it in a function or method, not in a namespace.