undefined reference to ,vtable for...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chaturap
    New Member
    • Feb 2009
    • 2

    #1

    undefined reference to ,vtable for...

    Hi,

    I'm trying to implement factory method pattern using C++ on Ubuntu 8.04 using gcc 4.2.

    There are 3 major classes DBConnectionFac tory, DBConnector and OracleConnector (which is the derived class of DBConnector).

    source in separate header and cpp files as below.


    /* DBConnectionFac tory.h */

    #ifndef _DBCONNECTIONFA CTORY_H
    #define _DBCONNECTIONFA CTORY_H
    #include <iostream>
    #include "DBConnecto r.h"
    using namespace std;

    class DBConnectionFac tory
    {
    public:
    static DBConnector* getConnector(st ring&);
    };

    #endif /* _DBCONNECTIONFA CTORY_H */

    //=============== =============== =============== ===============

    /* DBConnectionFac tory.cpp */

    #include "DBConnectionFa ctory.h"
    #include "DBConnecto r.h"
    #include "OracleConnecto r.h"

    DBConnector* DBConnectionFac tory::getConnec tor(string& dbName)
    {
    DBConnector* factory;

    if(dbName=="ora cle"){
    factory = new OracleConnector ();
    }
    else{
    exit(1);
    }
    if(factory==0){
    exit(1);
    }
    else{
    return factory;
    }
    }

    //=============== =============== =============== ===============

    /* DBConnector.cpp */

    #ifndef _DBCONNECTOR_H
    #define _DBCONNECTOR_H
    #include <iostream>
    using namespace std;

    class DBConnector
    {
    public:
    // Pure virtual methods. Derived class(es) should implement these methods
    virtual void init() = 0;
    virtual void connect() = 0;
    virtual void shutdown() = 0;

    virtual ~DBConnector()
    {

    }
    };

    #endif /* _DBCONNECTOR_H */


    //=============== =============== =============== ===============

    /* OracleConnector .h */

    #ifndef _ORACLECONNECTO R_H
    #define _ORACLECONNECTO R_H
    #include <iostream>
    #include "DBConnecto r.h"
    #include <occi.h>

    using namespace std;
    using namespace oracle::occi;

    class OracleConnector : public DBConnector
    {
    private:
    // commented...

    public:
    void init();
    void connect();
    void shutdown();
    };

    #endif /* _ORACLECONNECTO R_H */

    //=============== =============== =============== ===============

    /* OracleConnector .cpp */

    #include <iostream>
    #include "OracleConnecto r.h"

    using namespace std;

    void OracleConnector ::init(){
    // code...
    }

    void OracleConnector ::connect(){
    // code...
    }

    void OracleConnector ::shutdown(){
    // code...
    }

    int main(){} // TODO: Remove this main() added for testing

    When I try to compile DBConnectionFac tory.cpp it gives folowing error(s).

    Command: g++ DBConnectionFac tory.cpp -I/opt/oracle/include -L/opt/oracle/lib -locci -lclntsh

    Error:

    /tmp/cc15eWsy.o: In function `OracleConnecto r::OracleConnec tor()':
    DBConnectionFac tory.cpp:(.text ._ZN15OracleCon nectorC1Ev[OracleConnector ::OracleConnect or()]+0x16): undefined reference to `vtable for OracleConnector '
    collect2: ld returned 1 exit status

    I went through GCC FAQs but could not figure out the reason. Highly appreciate your feedback.

    Thanks,

    Chatura
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This compiles fine with Visual Studio.NET 2008.

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      You don't link OracleConnector .o with your DBConnectionFac tory, but reference OracleConnector in it when create it
      factory = new OracleConnector ();

      It compiles ok if you merge all this stuff in one cpp file.

      Comment

      • chaturap
        New Member
        • Feb 2009
        • 2

        #4
        Thanks for replying... I will try

        Comment

        Working...