0

I have a class router.cpp with implementations of dijkstras algorithm called getShortestPath. I would like to call this method from another .cpp file called interface.cpp.

Router.h

public:
    Router(vector<double>);
    void getShortestPath(...):

I defined starter.h as:

#include "Router.h"
class starter {
public:
     static Router r;

     static std::string func(std::vector<double> bbox) {
        std::string a = "DONE";
        return a;
    }
};
//EDIT: definition of a static variable must be given outside class declaration
 Router r = starter::r;

and interface.cpp where I am trying to call getShortestPath:

starter starter;
std::vector<double> bbox;
//starter::router =   
starter::r.getShortestPath(); // ERROR HERE "undefined reference to 'starter::r'

How should I define router appropriately?

How can I overcome this issue? Any suggestions would be appreciated.


EDIT:

After the helpful comments, kindly find the updated definition of the static variable above.

This leaves me with the same error as before:

undefined reference to starter::r at starter::r.getShortestPath();

  • 1
    static members can be declared inside the class definition, but they must often be defined exactly once outside the class definition. Add `Router starter::r;` to a cpp file. router.cpp seems the mos logical place to put it. – user4581301 Sep 14 '19 at 16:24
  • **Very** similar issue recently addressed here: https://stackoverflow.com/questions/57937147/unresolved-external-symbol-in-c-error-lnk1120-and-lnk2001/57937200#57937200 – Adrian Mole Sep 14 '19 at 16:25
  • Side note: When you find yourself making classes entirely of static members, that's usually a sign you want a namespace instead. – user4581301 Sep 14 '19 at 16:29
  • Skip down to answer two in the duplicate, specifically **static data members must be defined outside the class in a single translation unit** – user4581301 Sep 14 '19 at 16:35
  • @user4581301 should it be `Router starter::r;` or `Router starter::r { the vector I need for the constructor } ;` ? – xqedtaghmel Sep 14 '19 at 17:17
  • If you have arguments for the constructor, that's the place to use them. – user4581301 Sep 14 '19 at 17:47

0 Answers0