I am writing a new REST router for an application. It has some old C++ classes called Route
and so forth. I've created some new classes, namely (you guessed it) Route
and RouteManager
. There are literally 0 namespaces used in this entire application. So I figured by introducing my own namespace (WebRouter
) I could code while keeping the old code in the project.
Obviously this didn't work. My compiler (C++98) is complaining about things that are already defined (Route
and the cout overload). This is how I tried to accomplish my goal of keeping the old Route
class while namespacing a new Route
class.
Route.hpp
namespace WebRouter {
// complains this is defined, which it is for the old Route..
class Route {
private:
protected:
public:
string uri;
string method;
Route(string uri, string method);
~Route();
};
}
// complains this is already defined, which it is for the old Route..
ostream &operator<<(std::ostream &os, WebRouter::Route const &route) {
os << "--- Route ---" << endl;
os << "- URI: " << route.uri << endl;
os << "- Method: " << route.method << endl;
os << "-------------" << endl;
return os;
}
Route.cpp
#include "Route.hpp"
using namespace WebRouter;
Route::Route(string uri, string method){
this->uri = uri;
this->method = method;
}
Route::~Route(){}
RouteManager.hpp
namespace WebRouter {
class RouteManager {
private:
vector<Route> RouteVector;
protected:
public:
RouteManager();
~RouteManager();
Route* FindRoute(string uri, string method);
};
}
RouteManager.cpp
#include "RouterManager.hpp"
using namespace WebRouter;
RouteManager::RouteManager() {}
RouteManager::~RouteManager() {}
The specific error is
multiple definition of 'global constructors keyed to 2343_2__zoidfiosdiof**WebRouter**5**Route**E'
This happens whether I do using
or prefix with WebRouter::