By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

namespace problem

P: n/a
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[i].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[i][j+1]*vel[i][j+1]-density[i][j-1]*vel[i][j-1])/2.0/delta_x*delta_t+(density[i][j+1]+density[i][j-1])/2.0;
//lax method for first different equation

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

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

energy[i+1][j]=(-(vel[i][j+1]*(1.4*density[i][j+1]*energy[i][j+1]+0.5*density[i][j+1]*vel[i][j+1]*vel[i][j+1])-vel[i][j-1]*(1.4*density[i][j-1]*energy[i][j-1]+0.5*density[i][j-1]*vel[i][j-1]*vel[i][j-1]))/2.0/delta_x*delta_t+0.5*(density[i][j+1]*(energy[i][j+1]+0.5*vel[i][j+1]*vel[i][j+1])+density[i][j-1]*(energy[i][j-1]+0.5*vel[i][j-1]*vel[i][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[i][jj-1] << endl;
}
}
}

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

return 0;
}

Jul 18 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
lixiaoyao wrote:
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.

--
Ian Collins.
Jul 18 '06 #2

P: n/a
Ian Collins wrote:
lixiaoyao wrote:
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.

Jul 18 '06 #3

P: n/a
jo******@gmail.com wrote:
Ian Collins wrote:
>>lixiaoyao wrote:
>> 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...

--
Ian Collins.
Jul 18 '06 #4

P: n/a
lixiaoyao schrieb:
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>
#include <cmath>
using namespace std;
int main(){
[10m. lines deleted...]
}

Stefan
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
Jul 18 '06 #5

P: n/a
lixiaoyao wrote:
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.
Jul 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.