CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2010
    Posts
    5

    c++ no virtual function found on table

    Hello

    i'm trying to create a vector list using the STL lib.
    the first method works but the second crashes cause: virtual function _Build () not found on table.
    What's wrong?
    Compiled with gcc.

    code:

    //// Main
    #include <iostream>
    #include "TOcc.h"
    #include "TColl.h"

    #include <vector>

    using namespace std;

    int main()
    {

    DBE dbe;

    cout << endl << " Method 1. it work!" << endl;

    vector<_TOccV<DBE> *> list;

    _TOccV<DBE> *p;

    TOcc1 s1;
    p = &s1;
    list.push_back(p);

    TOcc2 s2;
    p = &s2;
    list.push_back(p);

    list[0]->Build ();
    list[1]->Build ();

    cout << endl << " Method 2. Don't work" << endl;

    TColl list2;

    list2.BuildAll ();

    return 0;
    }



    codice:

    ///// TOcc.h
    #ifndef TOCC_H_INCLUDED
    #define TOCC_H_INCLUDED

    #include<assert.h>
    #include<vector>

    using namespace std;

    class DBE {

    vector<int> someData;

    public:

    DBE () {};

    };



    template <typename S>
    class _TOccV {

    vector< vector<int> > *_data;
    int _dataYSize;

    virtual void _Build (void) { cout << endl << "_TOccV::_Build" << endl; };

    public:

    _TOccV () : _dataYSize(0) { };
    _TOccV (int dataYSize) : _dataYSize(dataYSize)
    { _data = new vector< vector<int> >(dataYSize); };
    ~_TOccV ()
    { delete _data; _data = NULL; };

    void Push (vector<int> *v, int where) { _data[where].push_back(*v); };
    vector<int> Pop (int what) { assert(what < _data->size()); return (*_data)[what]; };
    void Build (void) { _Build (); };

    };


    class TOcc1 : public _TOccV<DBE> {

    void _Build (void) { cout << endl << "TOcc1::_Build" << endl; };

    public:

    TOcc1 () : _TOccV<DBE> (1) {};

    };


    class TOcc2 : public _TOccV<DBE> {

    void _Build (void) { cout << endl << "TOcc2::_Build" << endl; };

    public:

    TOcc2 () : _TOccV<DBE> (1) {};

    };

    #endif // TOCC_H_INCLUDED


    /////// TColl.h
    #ifndef TCOLL_H_INCLUDED
    #define TCOLL_H_INCLUDED

    #include <vector>
    #include "TOcc.h"

    using namespace std;

    class TColl {

    vector<_TOccV<DBE> *> _list;

    public :

    TColl ();

    void BuildAll (void);

    };



    TColl::TColl ()
    {

    _TOccV<DBE> *p;

    TOcc1 s1;
    p = &s1;
    _list.push_back(p);

    TOcc2 s2;
    p = &s2;
    _list.push_back(p);

    };


    void TColl::BuildAll (void)
    {
    unsigned int idx;
    _TOccV<DBE> *p;

    for (idx = 0; idx < _list.size(); idx++)
    {
    p = _list[idx];
    p->Build ();
    }

    }


    #endif // TCOLL_H_INCLUDED

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: c++ no virtual function found on table

    TColl::TColl ()
    {

    _TOccV<DBE> *p;

    TOcc1 s1;
    p = &s1;
    _list.push_back(p);

    TOcc2 s2;
    p = &s2;
    _list.push_back(p);

    };

    Ask yourself what happens to s1 and s2 when the constructor is complete.
    your humble savant

  3. #3
    Join Date
    Nov 2010
    Posts
    5

    Re: c++ no virtual function found on table

    maybe s1 and s2 will be destroyed?

  4. #4
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: c++ no virtual function found on table

    Yup
    If they get destroyed, then anything pointing to them will now be invalid.
    your humble savant

  5. #5
    Join Date
    Nov 2010
    Posts
    5

    Re: c++ no virtual function found on table

    ok, i understand...

    i just resolve it declaring s1 and s2 static in a new init() function:

    void TColl::init (void)
    {

    _TOccV<DBE> *p;

    static TOcc1 s1;
    p = &s1;
    _list.push_back(p);

    static TOcc2 s2;
    p = &s2;
    _list.push_back(p);

    };

    now works but i'm not sure is the right way.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: c++ no virtual function found on table

    It probably is not the right way. I suggest that you use a vector<std::tr1::shared_ptr<_TOccV<DBE> > > or a boost:tr_vector<_TOccV<DBE> >, with _list.push_back(new TOcc1<DBE>()).

    By the way, names that begin with an underscore followed by an uppercase letter, or that contain consecutive underscores, are reserved to the implementation for any use. Therefore, rename _TOccV (its name is rather cryptic anyway).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Nov 2010
    Posts
    5

    Re: c++ no virtual function found on table

    i think the syntax is:

    vector<std::tr1::shared_ptr<_TOccV<DBE> > > _list2; // list declaration

    _list2.push_back(std::tr1::shared_ptr<_TOccV<DBE> > (new TOcc1)); // insertion

    _list2[what].Build () // access to function member

    right?

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: c++ no virtual function found on table

    Quote Originally Posted by OninO View Post
    i think the syntax is:

    vector<std::tr1::shared_ptr<_TOccV<DBE> > > _list2; // list declaration

    _list2.push_back(std::tr1::shared_ptr<_TOccV<DBE> > (new TOcc1)); // insertion

    _list2[what]->Build () // access to function member

    right?
    Pretty much, the objects in your vector are pointers, so use "->" not "."

    I recommend you use typdefs:
    Code:
    typedef _TOccV<DBE> toc_dbe_t;
    typedef std::tr1::shared_ptr<toc_dbe_t> smart_toc_dbe_t;
    
    vector<smart_toc_dbe_t> myContainer;
    myContainer.push_back(smart_toc_dbe_t(new toc_dbe_t));
    myContainer->Build();
    I have no idea what you are doing, so my names are not the best, but you get the point.

    By the way, _list2 is a terrible name. For starters, a list is a very precise object in C++, of which _list2 is not a type (it is a vector). Also, avoid numbering your variables. The name should contain a good description of the variable. "2" is not very descriptive.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Join Date
    Nov 2010
    Posts
    5

    Re: c++ no virtual function found on table

    Quote Originally Posted by monarch_dodra View Post
    Pretty much, the objects in your vector are pointers, so use "->" not "."
    right. i did it!

    I recommend you use typdefs:
    Code:
    typedef _TOccV<DBE> toc_dbe_t;
    typedef std::tr1::shared_ptr<toc_dbe_t> smart_toc_dbe_t;
    
    vector<smart_toc_dbe_t> myContainer;
    myContainer.push_back(smart_toc_dbe_t(new toc_dbe_t));
    myContainer->Build();
    thanks! much readable than my code

    I have no idea what you are doing, so my names are not the best, but you get the point.

    By the way, _list2 is a terrible name. For starters, a list is a very precise object in C++, of which _list2 is not a type (it is a vector). Also, avoid numbering your variables. The name should contain a good description of the variable. "2" is not very descriptive.
    i always use underscore for the private members of my classes. maybe this is not a good way... i will try to dont do it
    i used _list cause before using vector i tried with a list. :-) 2 because _list was commented (the first and wrong version of _list2) :-)

    thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured