program exit, while trying to initialize a class

Discussion in 'C++' started by dvir, Jun 7, 2006.

  1. dvir

    dvir Guest

    Hi

    I have a runing error while trying to initialize a class.
    this is my code:

    main.C
    -------------
    #include "tdagent.H"
    #include "simple_env.H"
    #include <vector>

    int main()
    {
    simple_env *env;
    env = new simple_env();
    env->create();
    tdagent *agent;
    agent = new tdagent(env);
    ....
    }


    tdagent.H
    ------------------

    #ifndef TDGAENT_H
    #define TDAGENT_H

    #include "critic.H"
    #include "actor.H"
    #include "simple_env.H"

    class tdagent
    {

    private:

    ublas::vector<float> get_state();
    ublas::vector<float> get_previous_state();
    void set_previous_state(ublas::vector<float> curr_state);
    float get_reward();
    simple_env* env;
    critic* critic1;
    actor* actor1;
    float d;
    state x;



    public:
    tdagent(simple_env* environment);
    ~tdagent();
    std::vector<float> find_next_action();


    };

    #endif

    tdagent.C
    ------------------
    #include "tdagent.H"

    tdagent::tdagent(simple_env* env)
    : critic1(new critic(env->get_cov(),env->get_state())),
    actor1(new actor(env->get_cov(),env->get_state(),
    env->get_actions_num(),
    env->get_min_reward(),env->get_max_reward()))


    {
    x.prev=get_state();
    }

    .....

    The code compiled fine.
    But when I tried to run it I got:

    "Aborted (core dumped)"

    I tried to debug it using ddd, and I saw that the program exits at:
    "agent = new tdagent(env);"
    at main.C
    when trying to step into this function I get to
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/iostream

    I'm using Cygwin g++ 3.4.4

    I don't understand where is my mistake.
    Hope you can help me find it.

    Thanks for your help.
    Dvir.
    dvir, Jun 7, 2006
    #1
    1. Advertising

  2. In message <>, dvir
    <> writes
    >Hi
    >
    >I have a runing error while trying to initialize a class.
    >this is my code:
    >

    [snip incomplete code]

    >I don't understand where is my mistake.
    >Hope you can help me find it.


    That's unlikely, since you haven't shown any of the member functions of
    simple_env, critic or actor.

    Post a minimal _complete_ program that illustrates the problem.

    --
    Richard Herring
    Richard Herring, Jun 7, 2006
    #2
    1. Advertising

  3. dvir

    dvir Guest

    Ok here is a more complete version of the code.
    hope its more clear now.

    main.C
    ------------
    #include "tdagent.H"
    #include "simple_env.H"
    #include <vector>

    int main()
    {
    simple_env *env;
    env = new simple_env();
    env->create();
    tdagent *agent;
    agent = new tdagent(env);
    ....
    }

    simple_env.H
    ---------------------
    #ifndef SIMPLE_ENV_H
    #define SIMPLE_ENV_H

    #include <math.h>
    #include <boost/numeric/ublas/matrix.hpp>
    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>
    #include <boost/numeric/ublas/matrix_proxy.hpp>
    #include <boost/numeric/ublas/vector_proxy.hpp>

    #define PI 3.1415926535897932384626433832795



    class simple_env
    {
    public:
    simple_env();
    ~simple_env();
    void create();
    float preform(float u);
    float get_reward();

    boost::numeric::ublas::vector<float> get_state();
    boost::numeric::ublas::vector<float> get_previous_state();
    boost::numeric::ublas::vector<float> get_final_state();
    boost::numeric::ublas::vector<float> get_cov();
    int get_actions_num();
    float get_min_reward();
    float get_max_reward();

    private:
    float sigmoid(float u);
    float reward;
    boost::numeric::ublas::vector<float> state;
    boost::numeric::ublas::vector<float> prev_state;
    boost::numeric::ublas::vector<float> final_state;
    boost::numeric::ublas::vector<float> cov;


    };

    #endif


    simple_env.C
    ---------------------
    #include "simple_env.H"

    namespace ublas = boost::numeric::ublas;
    using namespace boost::numeric::ublas;

    simple_env::simple_env()
    {

    reward=0;
    }

    simple_env::~simple_env()
    {

    }

    void simple_env::create()
    {


    cov.resize(3);
    for (unsigned int i = 0; i < 3; ++ i)
    cov (i) = 1;
    vector<float> initial_state(3);
    for (unsigned int i = 0; i < initial_state.size (); ++ i)
    initial_state (i) = 0;

    final_state.resize(3);
    for (unsigned int i = 0; i < final_state.size (); ++ i)
    final_state (i) = 5;

    state=initial_state;

    return;
    }

    ublas::vector<float> simple_env::get_cov()
    {
    return cov;
    }


    float simple_env::preform(float u)
    {

    prev_state=state;
    for (unsigned int i = 0; i < state.size (); ++ i)
    state (i) += sigmoid(u);
    reward=norm_2(state);

    return reward;

    }

    float simple_env::get_reward()
    {
    return reward;
    }

    ublas::vector<float> simple_env::get_state()
    {
    return state;
    }

    ublas::vector<float> simple_env::get_final_state()
    {
    return final_state;
    }
    ublas::vector<float> simple_env::get_previous_state()
    {
    return prev_state;
    }

    float simple_env::sigmoid(float u)
    {
    float s;
    s=PI/2*atanf(2/PI*u);
    return s;
    }

    int simple_env::get_actions_num()
    {
    return 1;
    }

    float simple_env::get_min_reward()
    {
    return 0;
    }

    float simple_env::get_max_reward()
    {
    return 8.66;
    }

    tdagent.H
    ----------------
    #ifndef TDGAENT_H
    #define TDAGENT_H

    #include "critic.H"
    #include "actor.H"
    #include "simple_env.H"

    class tdagent
    {

    private:

    ublas::vector<float> get_state();
    ublas::vector<float> get_previous_state();
    void set_previous_state(ublas::vector<float> curr_state);
    float get_reward();
    simple_env* env;
    critic* critic1;
    actor* actor1;
    float d;
    state x;



    public:
    tdagent(simple_env* environment);
    ~tdagent();
    std::vector<float> find_next_action();


    };

    #endif

    tdagent.C
    -----------------
    #include "tdagent.H"

    tdagent::tdagent(simple_env* env)
    : critic1(new critic(env->get_cov(),env->get_state())),
    actor1(new actor(env->get_cov(),env->get_state(),
    env->get_actions_num(),
    env->get_min_reward(),env->get_max_reward()))


    {
    x.prev=get_state();
    }

    ....



    Thanks.
    Dvir
    dvir, Jun 7, 2006
    #3
  4. dvir

    Paul Guest

    "dvir" <> wrote in message
    news:...
    >
    > Ok here is a more complete version of the code.
    > hope its more clear now.
    >
    > main.C
    > ------------
    > #include "tdagent.H"
    > #include "simple_env.H"
    > #include <vector>
    >
    > int main()
    > {
    > simple_env *env;
    > env = new simple_env();
    > env->create();
    > tdagent *agent;
    > agent = new tdagent(env);
    > ...
    > }


    1) Why are you dynamically creating these instances? Does that play into a
    role in duplicating the problem?

    And please remove the "..." as explained in the FAQ:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8


    #include "tdagent.H"
    #include "simple_env.H"
    #include <vector>

    int main()
    {
    simple_env env;
    env.create();
    tdagent agent(&env);
    }

    Is the above enough to duplicate the problem (that is if the rest of the
    code is OK, which it isn't -- see next item).

    2)
    > vector<float> initial_state(3);
    > for (unsigned int i = 0; i < initial_state.size (); ++ i)
    > initial_state (i) = 0; // This should have never compiled


    There is no user-defined operator() for vector, but you're calling one.
    What does this line do?

    initial_state (i) = 0;

    Shouldn't this be:

    initial_state = 0;

    ?

    > The code compiled fine.


    It did? That's shocking, seeing the error you made in your program
    concerning using ( ) instead of [ ] when accessing elements of the vector.

    Here it is to you, as a reference to what I'm speaking of:

    #include <vector>
    int main()
    {
    std::vector<float> initial_state(3);
    for (unsigned int i = 0; i < initial_state.size(); ++i )
    initial_state(i) = 0;
    }

    ----------------------------------------------------------------------------
    ----------------------------------
    Your Comeau C/C++ test results are as follows:
    Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "ComeauTest.c", line 7: error: call of an object of a class type without
    appropriate operator() or conversion functions to
    pointer-to-function type
    initial_state(i) = 0;
    ^

    1 error detected in the compilation of "ComeauTest.c".
    ----------------------------------------------------------------------------
    ------

    If the compiler did compile the code you posted, no wonder it core dumped.

    You also make the same mistake in other parts of the program, and that is
    using ( ) instead of [ ] when accessing or setting elements of the vector.

    Paul
    Paul, Jun 7, 2006
    #4
  5. dvir

    dvir Guest

    Paul wrote:
    > 2)
    > > vector<float> initial_state(3);
    > > for (unsigned int i = 0; i < initial_state.size (); ++ i)
    > > initial_state (i) = 0; // This should have never compiled

    >
    > There is no user-defined operator() for vector, but you're calling one.
    > What does this line do?
    >
    > initial_state (i) = 0;
    >
    > Shouldn't this be:
    >
    > initial_state = 0;
    >


    I used boost::numeric::ublas::vector from the boost library
    (www.boost.org)
    Because I'm using linear algebra functions.
    With ublas::vector It's right to write:
    vec(i)=0;
    Therefore, it did compile fine.
    My runing error was while trying to do:
    agent = new tdagent(env);
    I still don't understand what is wrong with that line.

    Thanks,
    Dvir
    dvir, Jun 8, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,892
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,828
    sandeep1976
    Nov 8, 2006
  3. Replies:
    2
    Views:
    450
    Jeff Epler
    May 31, 2005
  4. QQ
    Replies:
    5
    Views:
    516
    Jonathan Adams
    May 10, 2005
  5. chad
    Replies:
    14
    Views:
    576
    Arnaud Delobelle
    Oct 13, 2010
Loading...

Share This Page