这是我的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个类的头文件是:
事务存储库
#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_ */ //---------------------------------------------------------------------------------------------------------------------
事务内存库
#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_ */
事务文件存储库
#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)header 文件中的 protptypes。
我一直在谷歌上搜索两个小时的解决方案,但我尝试的一切都没有摆脱这些错误。
我应该提到这是我正在研究的一个 C++ QT 项目。我必须将包含路径添加到项目属性中才能使某些事情起作用。
包含的所有文件都存在。