1

I have two projects (call them Test and Intrados). Inside Intrados, I have the following 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);
}

From within the Intrados project, calling "IntradosMediator::Mapper(addVehicle)" works just fine; yet, in project Test, the following code produces a link error:

#include "IntradosMediator.cpp"
#include "Mapper.h"
using namespace IntradosMediator;

int main(){
    IntradosMediator::addVehicle("Car X");
    return 0;
}

The error is:

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)

I've made sure to add Intrados as a reference for Test, and also included it in the Include Directories. Not sure what to do here, since I'm new to C++. Thanks in advance for any advice.

Edit: I'm adding the Mapper code here:

//.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

The error says the linker can't find Mapper::getInstance (it seems to find your addVehicle function just fine). Might you be failing to include the library that implements "Mapper" in your link?

于 2013-03-21T00:24:27.697 に答える
0

Could you paste your code for class Mapper? It seems like you are missing addVehicle function in that class, which is what the compiler is complaining about.

于 2013-03-21T00:31:44.563 に答える