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
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
Comment