Results 1 to 4 of 4

Thread: C++ table structure

  1. #1
    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[2])
    {
        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. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: C++ table structure

    Add this to your code:
    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. #3
    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[1]. position.x = 1;
    I put the following error:
    error: request for member 'position' in 'list_body. std::vector<_Tp, _Alloc>::operator[] [with _Tp = body*, _Alloc = std::allocator<body*>](1ul)', which is of non-class type 'body*'

  4. #4
    Join Date
    Nov 2008
    Posts
    1,022

    Re: C++ table structure

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

    list_body[1] -> 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

Similar Threads

  1. Replies: 5
    Last Post: 27-08-2011, 10:53 AM
  2. Link a Table to another Table to Drop Down In Main Table
    By himeshRES in forum Windows Software
    Replies: 6
    Last Post: 11-12-2010, 02:01 PM
  3. Replies: 4
    Last Post: 30-11-2010, 03:01 AM
  4. To convert a pivot table to a flattened table in MS Excel
    By zeemga in forum Windows Software
    Replies: 3
    Last Post: 27-11-2010, 06:48 AM
  5. Structure of Table in MySQL
    By Eleeazar in forum Software Development
    Replies: 4
    Last Post: 06-11-2009, 10:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,179,401.74062 seconds with 17 queries