#1
 RyanInt Member Join Date: Jun 2009 Posts: 3,960
C++ table structure

I started in C++ and I want to make a table structure. I do this:
body * table_of_body =new body[N];

With body is composed of structures coordinates (x, y) defines the acceleration, velocity and position of a point.
Code:
```#include <iostream>
#include <cmath>
#define G 6.67e-11
#define N 2
using namespace std;
struct Coordinates
{
float x;
float y;
};
struct body{
float mass;
Coordinates position;
Coordinates velocity;
Coordinates acceleration;
};
void calcul_acceleration(body * table_of_body =new body)
{
Coordinates * table_of_acc=new Coordinates[N];
for(int i=0; i<(N-1); i++)
{
table_of_acc[i].x=(table_of_body[i+1].mass)*( (table_of_body[i+1].(position)).x - (table_of_body[i].(position)).x )/abs(pow(norm(table_of_body[i].(position).x-table_of_body[i+1].(position).x, 3));
table_of_acc[i].y=(table_of_body[i+1].mass)*( (table_of_body[i+1].(position)).y - (table_of_body[i].(position)).y )/abs(pow(norm(table_of_body[i].(position).y-table_of_body[i+1].(position).y, 3));
cout <<table_of_acc[i]<<endl;
}
table_of_acc[i].y*=G
table_of_acc[i].x*=G
}
float norm(Coordinates vector)
{
float n=sqrt(pow(vector.x, 2)+pow(vector.y, 2));
return n;
}
int main()
{
body * table_of_body =new body[N];
calcul_acceleration(table_of_body[]);
return 0;
}```
My problem lies in the void calcul_acceleration. If for example in the first box and there is a body, I want to change any of the details of the acceleration, should I write:
Code:
`(table_of_body[i+1].(position)).x`
or
Code:
`(table_of_body[i+1]->(position)).x`
or something else?
#2
 CitricAcid Member Join Date: Nov 2008 Posts: 1,022
Re: C++ table structure

Code:
`using std:: vector`
The vector array is a part of standard namespace (std: which allows you to easily create a dynamic array of elements.

Because your code is missing the above statement, you are having all those difficulties.
#3
 RyanInt Member Join Date: Jun 2009 Posts: 3,960
Re: C++ table structure

Okay great! Almost everything works, I have no error in the formula, but something is blocking the main ():
Code:
```#include <vector>
#include <iostream>
#include <cmath>
#define G 6.67e-11
#define N 2
using namespace std;
struct Coordinates
{
float x;
float y;
};
struct body{
float mass;
Coordinates position;
Coordinates velocity;
Coordinates acceleration;
};
Coordinates operator-( Coordinates a, Coordinates b)
{
Coordinates res;
res.x=a.x-b.x;
res.y=a.y-b.y;
return res;
}
float norm(Coordinates vector)
{
float n=sqrt(pow(vector.x, 2)+pow(vector.y, 2));
return n;
}
void calcul_acceleraton(vector<body> table_of_body)
{
Coordinates * table_of_acc=new Coordinates[N];
int i=0;
for(i; i<(N-1); i++)
{
table_of_acc[i].x=(table_of_body[i+1].mass)*( table_of_body[i+1].position.x );
table_of_acc[i].x=(table_of_body[i+1].mass)*( table_of_body[i+1].position.x  - table_of_body[i].position.x )/abs(pow(norm(table_of_body[i].position-table_of_body[i+1].position), 3));
table_of_acc[i].y=(table_of_body[i+1].mass)*( table_of_body[i+1].position.y  - table_of_body[i].position.y )/abs(pow(norm(table_of_body[i].position-table_of_body[i+1].position), 3));
cout <<table_of_acc[i].x<<endl;

}
table_of_acc[i].y*=G;
table_of_acc[i].x*=G;
}
int main()
{
vector<body*> list_body;
calcul_acceleraton(vector list_body);   Missing template arguments before 'list_body'
return 0;
}```
Missing template arguments before 'list_body' on the line calling the function calcul_acceleration

I have tried to:
Code:
`list_body. position.x = 1;`
I put the following error:
Quote:
 error: request for member 'position' in 'list_body. std::vector<_Tp, _Alloc>::operator[] [with _Tp = body*, _Alloc = std::allocator](1ul)', which is of non-class type 'body*'
#4
 CitricAcid Member Join Date: Nov 2008 Posts: 1,022
Re: C++ table structure

Your vector is a vector of body*, therefore you must:

list_body -> position.x = 1;

and you do not need vector line 59:

calcul_acceleraton (list_body);

And why after you pass <body*> vector to a function that expects a vector <body>?

Incidentally that lack of const and references

 Tags:

 Thread Tools Search this Thread Show Printable Version Email this Page Search this Thread: Advanced Search Similar Threads for: "C++ table structure" Thread Thread Starter Forum Replies Last Post Unable to work with table columns in opera browser when the table has wide layout Janya Technology & Internet 5 27-08-2011 10:53 AM Link a Table to another Table to Drop Down In Main Table himeshRES Windows Software 6 11-12-2010 02:01 PM Colonizing a table of employees from a table of applicants in Microsoft Access laplapye Windows Software 4 30-11-2010 03:01 AM To convert a pivot table to a flattened table in MS Excel zeemga Windows Software 3 27-11-2010 06:48 AM Structure of Table in MySQL Eleeazar Software Development 4 06-11-2009 10:57 PM

All times are GMT +5.5. The time now is 04:53 AM. 