我有两个项目(称它们为 Test 和 Intrados)。在 Intrados 中,我有以下命名空间:
#include "Mapper.h"
#include "Director.h"
#include "Driver.h"
#include <iostream>
#include <string>
using namespace std;
namespace IntradosMediator {
void addVehicle(string);
}
void IntradosMediator::addVehicle(string vehicleName) {
Mapper* mapper = Mapper::getInstance();
mapper->addVehicle(vehicleName);
}
在 Intrados 项目中,调用“IntradosMediator::Mapper(addVehicle)”就可以了;然而,在项目测试中,以下代码会产生链接错误:
#include "IntradosMediator.cpp"
#include "Mapper.h"
using namespace IntradosMediator;
int main(){
IntradosMediator::addVehicle("Car X");
return 0;
}
错误是:
Test.obj : error LNK2019: unresolved external symbol "public: static class Mapper *
__cdecl Mapper::getInstance(void)" (?getInstance@Mapper@@SAPAV1@XZ) referenced in
function "void __cdecl IntradosMediator::addVehicle(class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >)"
(?addVehicle@IntradosMediator@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@
std@@@Z)
我确保将 Intrados 添加为 Test 的参考,并将其包含在 Include Directories 中。不知道在这里做什么,因为我是 C++ 新手。提前感谢您的任何建议。
编辑: 我在这里添加映射器代码:
//.h
#ifndef MAPPER_H
#define MAPPER_H
#include <string>
using std::string;
class Mapper {
public:
static Mapper* getInstance();
void addVehicle(string);
private:
//this is a singleton
Mapper(){};
};
#endif
//.cpp
#include "Mapper.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<string> vehicleList;
Mapper* Mapper::getInstance(){
static Mapper instance;
return &instance;
}
void
Mapper::addVehicle(string vehicleName) {
vehicleList.push_back(vehicleName);
}