Question

First, this is hw and I'm supposed to change only one function. But I'm not here for help on that function, I'm here for an LNK error.

Here's the relevant output on the Developer Command Prompt after I type in cl shortest.cpp (with spaces between errors for easier reading):

shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::readFile(char *)" (?readFile@Graph@@QAEXPAD@Z) referenced in function _main

shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::dijkstra(int)" (?dijkstra@Graph@@QAEXH@Z) referenced in function _main

shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::printPath(int)" (?printPath@Graph@@QAEXH@Z) referenced in function _main

shortest.exe : fatal error LNK1120: 3 unresolved externals

I understand this is supposed to mean I haven't defined those symbols, but they should have been predefined for me in one of two files: graph.cpp or graph.h. Graph.h seems more likely, as it contains the declarations of each of these "unresolved" symbols.

graph.h:
#include <iostream>
#include <fstream>
#include <vector>
#include <limits.h>
#include <cstdlib>

#define INFINITY INT_MAX

using namespace std;

class Graph
{
  struct Edge
  {
    int dest;    // destination vertex number
    int cost;    // edge weight
    Edge *next;
  };

  struct Vertex
  {
    Edge *adj;
    bool known;
    int  dist;
    int  path;
  };

  vector<Vertex *> vertices;
  vector<int>      PQ;
  int              PQsize;

  void PQinsert (int v);
  int  PQdeleteMin ();

public:
  Graph () { PQsize=0; }
  int  numVertices ()  { return vertices.size(); }
  int  dist (int dest) { return vertices[dest]->dist; }
  void readFile (char *name);
  void dijkstra  (int start);
  void printPath (int dest);
};

I won't post graph.cpp or shortest.cpp (which only contains the main), unless requested, as it doesn't seem to me that the content of the related functions is an issue. If I do, I will only post the related methods to keep it shorter. But graph.cpp, graph.h, and shortest.cpp are all in the same folder. And shortest.cpp does include graph.h. I'm only supposed to change the dijkstra method, but if I can add something that will make this compile without changing way the methods or class themselves work (which it shouldn't have to) I don't see a problem.

Was it helpful?

Solution

You have to use make file to build with all the source files. Otherwise you can first compile the sources and then link manually. Search google for individual commands for compiling/linking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top