我有两个项目(分别称为Test和Intrados)。在Intrados内部,我具有以下 namespace :
#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)”就可以了。但是,在项目Test中,以下代码会产生链接错误:
#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++的新手,所以不确定在这里做什么。在此先感谢您的任何建议。
编辑:
我在这里添加Mapper代码:
//.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);
}
最佳答案
该错误表明链接器找不到Mapper::getInstance
(似乎找到了addVehicle
函数就好了)。您是否可能未在链接中包含实现“Mapper”的库?
关于c++ - C++ “unresolved external symbol”使用来自不同项目的 namespace ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15537255/