实例化类时未定义的引用

时间:2013-05-14 13:14:22

标签: c++ eclipse qt oop class

这是我的 main.cpp 。这是我遇到问题的地方:

我收到两个错误:

第23行的'BankController :: BankController(TransactionRepository )*的未定义引用

第19行的`TransactionFileRepository :: TransactionFileRepository(std :: string)的未定义引用

对于他们两个,类型是C / C ++问题,资源是main.cpp

#include "bankgui.h"
#include "Controller/BankController.h"
#include "Repository/TransactionFileRepository.h"
#include "Repository/TransactionMemoryRepository.h"
#include "Repository/TransactionRepository.h"

#include <QtGui>
#include <QApplication>
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char *argv[]){

  string path = "DataStorage/Database.txt";

  //Instantiate the main data repository
  TransactionRepository* mainDatabase;
  mainDatabase = new TransactionFileRepository(path); // <-- Error here

  //Instantiate the main controller
  BankController* mainController;
  mainController = new BankController(mainDatabase); // <-- Same Error here

  //Starts the GUI
  QApplication app(argc, argv);
  BankGUI* mainWidget;
  mainWidget = new BankGUI(mainController);
  mainWidget->show();

  return app.exec();
}

我有3个班级:

  • 虚拟 TransactionRepository

  • 实现上述 TransactionMemoryRepository

  • 的一个类
  • TransactionMemoryRepository 继承到 TransactionFileRepository

  • 的一个类

3个类的头文件是:

TransactionRepository

#ifndef TRANSACTIONREPOSITORY_H_
#define TRANSACTIONREPOSITORY_H_

//---------------------------------------------------------------------------------------------------------------------

#include "../Domain/BankTransaction.h"
#include "../Utils/Vector.h"
#include "../Domain/exceptions.h"

//---------------------------------------------------------------------------------------------------------------------

class TransactionRepository {
public:

  /*
   *  Search and return the address of a transaction with the matching id
   */
  virtual BankTransaction* findById(int id) = 0;

  /*
   *  Returns the list of transactions
   */
  virtual Vector<BankTransaction*>findAll() = 0;

  /*
   *  Save a bank transaction in the list
   */
  virtual void save(BankTransaction) throw (RepositoryException) = 0;

  /*
   *  Update a transaction
   *
   */
  virtual void update(int id, int day, string type, float amount, string desc) = 0;

  /*
   *  Removes a transaction from our list
   */
  virtual void remove(int id) = 0;

  /*
   *  Return the total number of transactions from our list
   */
  virtual void getNr() = 0;

  /*
   *  Get a copy of the list
   */
  virtual void getCopy(Vector<BankTransaction> list) = 0;

  virtual ~TransactionRepository(){}

};

//---------------------------------------------------------------------------------------------------------------------

#endif /* TRANSACTIONREPOSITORY_H_ */

//---------------------------------------------------------------------------------------------------------------------

TransactionMemoryRepository

#ifndef TRANSACTIONMEMORYREPOSITORY_H_
#define TRANSACTIONMEMORYREPOSITORY_H_

#include "TransactionRepository.h"
#include "../Utils/Vector.h"

class TransactionMemoryRepository: public TransactionRepository {
public:
  TransactionMemoryRepository();
  virtual ~TransactionMemoryRepository();

  /*
   *  Search and return the address of a transaction with the matching id
   */
  BankTransaction* findById(int id);

  /*
   *  Returns the list of transactions
   */
  Vector<BankTransaction*>findAll();

  /*
   *  Save a bank transaction in the list
   */
  void save(BankTransaction) throw (RepositoryException);

  /*
   *  Update a transaction
   *
   */
  void update(int id, int day, string type, float amount, string desc);

  /*
   *  Removes a transaction from our list
   */
  void remove(int id);

  /*
   *  Return the total number of transactions from our list
   */
  void getNr();

  /*
   *  Get a copy of the list
   */
  void getCopy(Vector<BankTransaction> list);

protected:

  Vector<BankTransaction*> TransactionList;

};

#endif /* TRANSACTIONMEMORYREPOSITORY_H_ */

TransactionFileRepository

#ifndef TRANSACTIONFILEREPOSITORY_H_
#define TRANSACTIONFILEREPOSITORY_H_

#include <string>
#include "../Repository/TransactionMemoryRepository.h"
#include "../Utils/Vector.h"
#include "../Domain/BankTransaction.h"

using namespace std;

class TransactionFileRepository: public TransactionMemoryRepository {

private:
  string fileName;
  Vector<BankTransaction*> loadFromFile();
  void writeToFile();

public:
  TransactionFileRepository(string filepath);
  virtual ~TransactionFileRepository();
  virtual BankTransaction* findById(string id);
  virtual Vector<BankTransaction*> findAll();
  virtual void save(BankTransaction) throw  (RepositoryException);
  virtual void update(BankTransaction) throw (RepositoryException);
  virtual void remove(string studentId) throw (RepositoryException);
};

#endif /* TRANSACTIONFILEREPOSITORY_H_ */

最后两个类的.cpp文件(因为虚拟文件不需要.cpp文件)在其中没有太多实现。我添加的唯一内容是(.h)eader文件中的protptypes。

我一直在谷歌搜索两个小时的解决方案,但我尝试的一切都没有摆脱这些错误。

我应该提一下,这是我正在研究的C ++ QT项目。我不得不在项目属性中添加一些工作的包含路径。

包含所有文件。

0 个答案:

没有答案