c++ - C++中的工厂模式链接器错误 undefined reference

标签 c++ oop factory-pattern

我想使用抽象工厂模式。但是在 Factory 的构造函数中有一个错误,它说

    `[Linker error] undefined reference to `DbDatabaseFactory::CreateConnection()'`    
     [Linker error] undefined reference to `DbDatabaseFactory::CreateCommand()' 
    `[Linker error] undefined reference to `DbDatabaseFactory::CreateConnection()'`    
     [Linker error] undefined reference to `DbDatabaseFactory::CreateCommand()' 

这是我的代码:

class IConnection{

      public:
              bool Connect();
              bool Disonnect();
              bool connectionState;
      };

class ICommand{
      public:
              void  Execute(string);
      };

class SqlDbConnection: public IConnection{

      public:
             bool connectionState;
             bool Connect(){
                   connectionState = true;
                   cout << "MS Sql bağlantısı açılacak";
                    return true;
                    };
             bool Disonnect(){
                     cout << "MS Sql bağlantısı kapatılacak";
                     return true;
                     };
      };

class MySqlDbConnection:public IConnection{

      public:
             bool connectionState;
             bool Connect(){
                  connectionState = true;
                  cout << "MYSql bağlantısı açılacak";
                  return true;
                  };
             bool Disonnect(){
                 cout << "MYSql bağlantısı kapatılacak";
                  return true;
                  };
      };

class SqlDbCommand: public ICommand{
      public:
             void Execute(string command){
                   cout << "Sql Command çalıştırılıyor";
                   };

      };

class MySqlDbCommand: public ICommand{
      public:
             void Execute(string command){
                   cout << "MySql Command çalıştırılıyor";
                   };
            };

class DbDatabaseFactory{
      public:
             IConnection CreateConnection();
             ICommand CreateCommand();
      };

class MsSqlDbFactory: public DbDatabaseFactory{
      public:
             IConnection CreateConnection(){
              SqlDbConnection sqlConnection;
              return sqlConnection ;
             }

             ICommand CreateCommand(){
              SqlDbCommand sqlCommand;
              return sqlCommand ;
             }
      };


class MySqlDbFactory: public DbDatabaseFactory{
      public:
             IConnection CreateConnection(){
              MySqlDbConnection mySqlConnection;
              return mySqlConnection ;
             }

             ICommand CreateCommand(){
              MySqlDbCommand mySqlCommand;
              return mySqlCommand ;
             }
      };

class Factory{
              DbDatabaseFactory _databaseFactory;
              IConnection _connection;
              ICommand _command;
      public:
              Factory(DbDatabaseFactory);

           void Start(){
               _connection.Connect();
                if(_connection.connectionState == true){
                _command.Execute("SELECT ...");
                }
          };


      };

Factory::Factory(DbDatabaseFactory dbFactory)
{
    _databaseFactory = dbFactory;   
    _connection = dbFactory.CreateConnection();
    _command = dbFactory.CreateCommand();
}

你有什么建议吗?

最佳答案

DbDatabaseFactory 的声明应该是,

  class DbDatabaseFactory{
  public:
         virtual IConnection* CreateConnection() = 0;
         virtual ICommand* CreateCommand() = 0;
  };

而且在 Factory 类中,您应该保留对 DbDatabaseFactory、IConnection 和 ICommand 的指针或引用,以正确使用运行时多态性

请找到工厂类的以下更改,

 class Factory{
          DbDatabaseFactory* _databaseFactory;
          IConnection* _connection;
          ICommand* _command;
  public:
          Factory(DbDatabaseFactory*);

       void Start(){
           _connection->Connect();
            if(_connection->connectionState == true){
            _command->Execute("SELECT ...");
            }
      };


  };

  Factory::Factory(DbDatabaseFactory* dbFactory)
  {
      _databaseFactory = dbFactory;   
      _connection = dbFactory->CreateConnection();
     _command = dbFactory->CreateCommand();
  } 

这些更改应该可以解决您的问题。

同时修改您的 MsSqlDbFactory 如下

class MsSqlDbFactory: public DbDatabaseFactory{
  public:
         IConnection* CreateConnection(){
          return new SqlDbConnection();
         }

         ICommand* CreateCommand(){
          return new SqlDbCommand(); 
         }
  };

关于c++ - C++中的工厂模式链接器错误 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21352232/

相关文章:

.net - 关于依赖注入(inject)和工厂的疑惑

c++ - 工厂方法中的输出参数

C++11 main() 返回时终止线程?

c++ - 如何使用 Alchemy 将 C++ 移植到 swf?

python - 计算两个日期之间有多少个星期一

c++ - 每个派生类的静态变量

c# - 带对象的全局静态类

php - 是否有任何 PHP OO 文件 API?

c++ - 长整数输出错误

c# - 在 ASP.NET 中使用依赖注入(inject)和工厂模式传递服务