Hi,
I'm trying to implement factory method pattern using C++ on Ubuntu 8.04 using gcc 4.2.
There are 3 major classes DBConnectionFactory, DBConnector and OracleConnector (which is the derived class of DBConnector).
source in separate header and cpp files as below.
/* DBConnectionFactory.h */
#ifndef _DBCONNECTIONFACTORY_H
#define _DBCONNECTIONFACTORY_H
#include <iostream>
#include "DBConnector.h"
using namespace std;
class DBConnectionFactory
{
public:
static DBConnector* getConnector(string&);
};
#endif /* _DBCONNECTIONFACTORY_H */
//================================================== ==========
/* DBConnectionFactory.cpp */
#include "DBConnectionFactory.h"
#include "DBConnector.h"
#include "OracleConnector.h"
DBConnector* DBConnectionFactory::getConnector(string& dbName)
{
DBConnector* factory;
if(dbName=="oracle"){
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 _ORACLECONNECTOR_H
#define _ORACLECONNECTOR_H
#include <iostream>
#include "DBConnector.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 /* _ORACLECONNECTOR_H */
//================================================== ==========
/* OracleConnector.cpp */
#include <iostream>
#include "OracleConnector.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 DBConnectionFactory.cpp it gives folowing error(s).
Command: g++ DBConnectionFactory.cpp -I/opt/oracle/include -L/opt/oracle/lib -locci -lclntsh
Error:
/tmp/cc15eWsy.o: In function `OracleConnector::OracleConnector()':
DBConnectionFactory.cpp:(.text._ZN15OracleConnecto rC1Ev[OracleConnector::OracleConnector()]+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