首先,这是硬件,我应该只更改一个功能。但是我在这里不是该功能的帮助,我在这里是关于LNK错误的。
这是我输入cl shortest.cpp后在Developer Command Prompt上的相关输出(错误之间有空格以便于阅读):
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
我知道这应该意味着我尚未定义这些符号,但是它们应该已经在两个文件之一graph.cpp或graph.h中为我预定义了。Graph.h似乎更有可能,因为它包含每个“未解析”符号的声明。
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);
};
除非有要求,否则我不会发布graph.cpp或shortest.cpp(仅包含main),因为在我看来相关功能的内容不是问题。如果这样做,我只会发布相关方法以使其更短。但是graph.cpp,graph.h和shortest.cpp都在同一文件夹中。而且shortest.cpp确实包含graph.h。我只应该更改dijkstra方法,但是如果我可以添加一些东西来进行编译而又不改变方法或类本身的工作方式(不必这样做),则不会出现问题。
您必须使用make文件来构建所有源文件。否则,您可以先编译源代码,然后手动链接。在google中搜索用于编译/链接的单个命令。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句