namespace problem

L

lixiaoyao

hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"

I attached my code, anyone can help me run this code smoothly? I really
appreciate it.
Thanks a lot
B
using namespace std;
#include <iostream>
#include <fstream>
#include <cmath>

int main(){
int Nstep=500;//must be even
int Nstepoutput=50;
ofstream input[Nstepoutput];
char buf[256];

for(int i=0;i<Nstepoutput;i++)
{
sprintf(buf,"output%d",i);
input.open(buf);
}

double delta_t=1.0/Nstep;
double delta_x=1.0/Nstep;
double vel[Nstep][Nstep];
double density[Nstep][Nstep];
double denvel[Nstep][Nstep];
double energy[Nstep][Nstep];
double x[Nstep];
double time[Nstep];
ofstream haha("hehe");

for(int i=1;i<Nstep+1;i++)
{
x[i-1]=double((i-1))/Nstep;
time[i-1]=double((i-1))/Nstep;
vel[0][i-1]=0; // intial velocity at time=0;
vel[i-1][0]=0;
vel[i-1][Nstep-1]=0;
density[i-1][0]=1;
density[i-1][Nstep-1]=0;
energy[i-1][0]=2.5;
energy[i-1][Nstep-1]=2;
denvel[0][i-1]=0;
denvel[i-1][0]=0;
denvel[i-1][Nstep-1]=0;
}
for(int i=1;i<Nstep/2+1;i++)
{
density[0][i-1]=1;
energy[0][i-1]=2.5;
input[0] << x[i-1] << '\t' << density[0][i-1] << '\t' <<
energy[0][i-1] << endl;
}
for(int i=Nstep/2+1;i<Nstep+1;i++)
{
density[0][i-1]=0.125;
energy[0][i-1]=2;
input[0] << x[i-1] << '\t' << density[0][i-1] << '\t' <<
energy[0][i-1] << endl;
}

for(int i=0;i<Nstep-2;i++){

for(int j=1;j<Nstep-2;j++)
{

density[i+1][j]=-(density[j+1]*vel[j+1]-density[j-1]*vel[j-1])/2.0/delta_x*delta_t+(density[j+1]+density[j-1])/2.0;
//lax method for first different equation

denvel[i+1][j]=-(denvel[j+1]*vel[j+1]+0.4*density[j+1]*energy[j+1]-denvel[j-1]*vel[j-1]-0.4*density[j-1]*energy[j-1])/2.0/delta_x*delta_t+0.5*(

denvel[j+1]+denvel[j-1]);

energy[i+1][j]=(-(vel[j+1]*(1.4*density[j+1]*energy[j+1]+0.5*density[j+1]*vel[j+1]*vel[j+1])-vel[j-1]*(1.4*density[j-1]*energy[j-1]+0.5*density[j-1]*vel[j-1]*vel[j-1]))/2.0/delta_x*delta_t+0.5*(density[j+1]*(energy[j+1]+0.5*vel[j+1]*vel[j+1])+density[j-1]*(energy[j-1]+0.5*vel[j-1]*vel[j-1])))/density[i+1][j]-0.5*vel[i+1][j]*vel[i+1][j];

// density are not zero at all,but how to protect program from
potential zero bug

}
cout << i << "\t" << i/5 << "\t" << i/5.0 << endl;
getchar();

if(i/5==i/5.0){
for(int jj=0;jj<Nstep;jj++){
input[i/5] <<'\t' << i/100.0 << "\t" << x[i-1] << '\t' <<
density[jj-1] << endl;
}
}
}

for (int i=0;i<Nstepoutput;i++){
input.close();
}

return 0;
}
 
I

Ian Collins

lixiaoyao said:
hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"

I attached my code, anyone can help me run this code smoothly? I really
appreciate it.

I don't know about running it, it a bit of a mess, but it should compile
if you fix one gccism:
Thanks a lot
B
using namespace std;
#include <iostream>
#include <fstream>
#include <cmath>

int main(){
int Nstep=500;//must be even
int Nstepoutput=50;
ofstream input[Nstepoutput];

Nstepoutput should be const int, C++ doesn't (yet?) support VLAs.
 
J

joosteto

Ian said:
lixiaoyao said:
hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"

I attached my code, anyone can help me run this code smoothly? I really
appreciate it.

I don't know about running it, it a bit of a mess, but it should compile
if you fix one gccism:

And maybe adding "using namespace std;" at the beginning of the
program.
BTW, seems the g++ being used is rather old, as any recent g++ will
need it too.
 
I

Ian Collins

Ian said:
lixiaoyao said:
hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"

I attached my code, anyone can help me run this code smoothly? I really
appreciate it.

I don't know about running it, it a bit of a mess, but it should compile
if you fix one gccism:


And maybe adding "using namespace std;" at the beginning of the
program.
BTW, seems the g++ being used is rather old, as any recent g++ will
need it too.
The OP did, right at the top. I let that go this time...
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

lixiaoyao said:
hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"

I attached my code, anyone can help me run this code smoothly? I really
appreciate it.
Thanks a lot
B

Put his line...
using namespace std;
#include <iostream>
#include <fstream>
#include <cmath>
....here.

I.e.:

#include <iostream>
#include <fstream>
int main(){
[10m. lines deleted...]
}


Stefan
 
R

Ron Natalie

lixiaoyao said:
hey all
I wrote the following code which run well in g++, but when I try to
compile it in C++ builder, it report a bunch of problems, the most
frustrating one is
"[C++ Error] Unit1.cpp(1): E2282 Namespace name expected"

I attached my code, anyone can help me run this code smoothly? I really
appreciate it.
Thanks a lot
B
using namespace std;

At this point there is no namespace called std. You have to include
some standard library header (or otherwise define it). One of the
basic tenets of standard C and C++ is that your program is pretty
much an empty slate at the beginning of a translation unit. Until
you start including declarations they don't exist. There's nothing
special about std versus any other namespace other than it's reserved
for the standard library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top