当我尝试创建一个dgraph对象时,我得到一个编译错误,但是当我省略创建dgraph对象的行时,我可以完美地链接和编译文件。请帮忙!感谢。
这是main.cpp:
#include<iostream>
#include"dgraph.h"
using namespace std;
int main()
{
string test;
test = "Hello, world!";
cout << test << endl;
slist L1;
L1.displayAll();
dgraph L2; //offending line
return 0;
}
这是我的dgraph.h:
#include <iostream>
#include <string>
#include "slist.h"
using namespace std;
const int SIZE = 20;
struct Gvertex
{
char vertexName; //the vertex Name
int outDegree; //the Out degree
slist adjacentOnes; //the adjecent vertices in an slist
int visit; // This will hold the visit number in HW7
};
class dgraph
{
private:
Gvertex Gtable[SIZE]; // a table representing a dgraph
int countUsed; // how many slots of the Gtable are actually used
public:
class BadVertex {}; // thrown when the vertex is not in the graph
dgraph();
~dgraph();
void fillTable();
void displayGraph();
int findOutDegree(char);
slist findAdjacency(char);
};
dgraph.cpp:
#include<iostream>
#include<fstream>
#include"dgraph.h"
void dgraph::fillTable()
{
ifstream fin;
fin.open("table.txt");
while(fin >> Gtable[countUsed].vertexName) //if you can read the name
{
char x; //temp var
fin >> Gtable[countUsed].outDegree;
for (int i = 0; i < Gtable[countUsed].outDegree; ++i)
{
fin >> x;
Gtable[countUsed].adjacentOnes.addRear(x);
}
countUsed++;
}
fin.close();
}
我正在使用g ++ -c编译,然后使用g ++ -o将所有文件链接在一起。 这是我编译时得到的确切错误:
g++ -o run hw6Client.o dgraph.o llist.o slist.o
Undefined symbols for architecture x86_64:
"dgraph::~dgraph()", referenced from:
_main in hw6Client.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
答案 0 :(得分:0)
标头声明了dgraph对象,但没有定义实现。只要您不实际创建dgraph对象,这就没问题了。当您包含实际声明dgraph对象的违规行时,问题就出现了。执行此操作时,链接器将在某处查找实际实现,但无法找到它。您需要包含
的实现void dgraph::filltable()
void dgraph::displayGraph()
int dgraph::findOutDegree(char)
slist dgraph::findAdjacency(char)
dgraph::dgraph()
dgraph::~dgraph()
链接器会找到它的地方。您需要显式定义构造函数和析构函数,因为您已明确声明它们。