1

我有两个项目(称它们为 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);
}
4

2 回答 2

2

错误说链接器找不到Mapper::getInstance(它似乎找到你的addVehicle函数就好了)。您可能没有在链接中包含实现“映射器”的库吗?

于 2013-03-21T00:24:27.697 回答
0

你能粘贴你的类 Mapper 的代码吗?似乎您在该类中缺少 addVehicle 函数,这是编译器所抱怨的。

于 2013-03-21T00:31:44.563 回答