ERROR: expected constructor or ...

17: expected constructor, destructor, or type conversion before '=' token.

I get this error with this simple code:

...
double x[1001] = { 0.0 };
x[0] = 0.00000; <-- this is line 17
x[1] = 0.00001;
...

I checked the "arrays" page here and it's identical to the description. What am I not seeing??

Thanks in advance!

-LD
____________________________
my faith: http://www.angelfire.com/ny5/jbc33/
You are correct, your code is fine.

Are you sure the error isn't propagating from something wrong with the previous line(s), like mis-matched parentheses?
Well, here is the top of the program. I can't see what would cause the error on line 17,

// gTrajectory.c: calculate the path of a mass, m,
// near the surface of the Earth
//
// DATE: 05/26/08
// TIME: 11:07 AM

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std; // C++ adopts space in memory

#define pi 3.14159 // standard constant
#define dt 0.00001 // time interval 1/100,000 of a second

double x[1001] = { 0.0 }; // declaring the path array
x[0] = 0.0e+9; // Initial Condition for x(0)
x[1] = 1.0e-5; // Initial Condition for x(1)


-LD

____________________________
my faith: http://www.angelfire.com/ny5/jbc33/
errr.... Do you actually have that code in a function?

e.g.
1
2
3
4
5
int main() {
double x[1001] = { 0.0 }; // declaring the path array
x[0] = 0.0e+9; // Initial Condition for x(0)
x[1] = 1.0e-5; // Initial Condition for x(1)
}


It looks to me as if your are trying to initialise elements 0 and 1 of a global array x, in which case you can't do it like this.

If you want to initialise elements 0 and 1 then (using Zaita's post) line numbers 3 and 4 have to be part of a function or method.
You could just use
 
double x[1001] = {0.0e+9, 1.0e-5, 0.0};


That would initialize the first two elements to what you want and the rest to 0.0.
Thanks to all you guys... I am sorry but I should have done this to begin with. My ignorance of C++ (coughIcoughHATEcoughJAVAcough) is showing. Here is the whole program:

// gTrajectory.c: calculate the path of a mass, m,
// near the surface of the Earth
//
// DATE: 05/26/08
// TIME: 11:07 AM

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std; // C++ adopts space in memory

#define pi 3.14159 // standard constant
#define dt 0.00001 // time interval 1/100,000 of a second

double x[1001] = { 0.0 }; // declaring the path array
x[0] = 0.0e+9; // Initial Condition for x(0)
x[1] = 1.0e-5; // Initial Condition for x(1)


double m = 1.0; // mass of the moving object
double F = 9.8/m; // force of gravity

// cout << "\n t \t x(t) \n"; // the output header
int main()
{
ofstream gTraj; // create a file for output
gTraj.open ("gTrajectory.txt"); // open a file for output
gTraj << "\nt\tx(t)\n"; // the output header in the file

for (int i = 0 ; i <= 999 ; i++ ) // 1000 elements in x(t)
{

x[i+2] = -x[i] + 2*x[i+1] + (F/m)*dt*dt;

cout << i*dt << "\t" << x[i] << "\n"; // the output
gTraj << i*dt << "\t" << x[i] << "\n"; // the output
}

cout << "1000" << x[1001]; // the last one
gTraj.close();

return 0; // exit the program

}

PS: I just realized that maybe I should mention that I am using the free DJGPP compiler, which I installed. Also, thanks for the suggestion Duoas.

Thanks in advance!

-LD
____________________________
my faith: http://www.angelfire.com/ny5/jbc33/

Last edited on
I'm not familiar with that compiler but as long as it complies with usual constructs then the change suggested by Duoas should work from a code point of view. If it doesn't then you will have to follow my suggestion and put those lines in before your for loop.

I don't know about the maths though :)
Thanks Duoas - it finally works!!

I asked a friend about this and he said that the reason is that the statements needed to be in the _body_ of the main() program - not in the declaration part.

So Duoas' suggestion took the declaring statements out and made it just a definition of the 'x' array. I was using the declaration area (before main) to initialize the first two 'x' values of the difference equation (which go _in_ main().)

BTW, I know the maths work. (it's for projectile motion using differences to solve the differential equation) b/c I transferred them to an Excel file and got a parabola as expected.

Finally, now I can go on the the next step... plotting it! :)

Thanks to _all_ again - a quantum leap in help compared to the "help" at the Sun/Java forums.

-LD
____________________________
my faith: http://www.angelfire.com/ny5/jbc33/
Topic archived. No new replies allowed.