由于符号的 C++ 链接器失败
C++ Linker failure because of symbol
我正在编写一些 C++ 代码,请注意我是该语言的新手。我已经解决了使用 G++ 编译时的所有错误,但我似乎无法弄清楚为什么会出现链接失败。
我附上了我的代码和错误:
错误:
$ g++ -std=c++11 algorithm.cpp
Undefined symbols for architecture x86_64:
"Graph::Graph()", referenced from:
_main in algorithm-14102f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的档案(目前不完整):
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
class Graph {
struct node { // nodes that you read
string name;
int id; // index of the node in the nodes vector
vector<int> in; // parent(s) that can lead to this node
vector<int> out; // children you can go to
node(string key, int ind, vector<int> *in = new vector<int>(), vector<int> *out = new vector<int>()) :
name(key), id(ind) {}
};
vector<node> nodes; // all the nodes in arbitrary sequential order
map <string, int> dict; // map converting the names into ids
public:
Graph(); // class constructor
void addNode(string key, int id){
node *item = new node(key, id);
}
int getNodesLength(){
return nodes.size();
}
};
int main()
{
Graph * graph = new Graph();
std::string s;
std::string word;
while(std::getline(std::cin, s)) {
for(char c : s) {
if (c == ' '){ // right side words
graph->addNode(word, graph->getNodesLength());
word = "";
} else if (c == ':') { // left side word
graph->addNode(word, graph->getNodesLength());
word = "";
} else { // letter char
word += c; //
}
}
}
return 0;
}
Graph * graph = new Graph();
Graph
已经在顶级命名空间。
您还忘记提供 constructor
说明你没有实现Graph的构造函数class。要么不要在 header 中声明它(这样编译器将为您生成默认构造函数),或者如果您这样做,请实现它。
我正在编写一些 C++ 代码,请注意我是该语言的新手。我已经解决了使用 G++ 编译时的所有错误,但我似乎无法弄清楚为什么会出现链接失败。
我附上了我的代码和错误:
错误:
$ g++ -std=c++11 algorithm.cpp
Undefined symbols for architecture x86_64:
"Graph::Graph()", referenced from:
_main in algorithm-14102f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的档案(目前不完整):
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <map>
using namespace std;
class Graph {
struct node { // nodes that you read
string name;
int id; // index of the node in the nodes vector
vector<int> in; // parent(s) that can lead to this node
vector<int> out; // children you can go to
node(string key, int ind, vector<int> *in = new vector<int>(), vector<int> *out = new vector<int>()) :
name(key), id(ind) {}
};
vector<node> nodes; // all the nodes in arbitrary sequential order
map <string, int> dict; // map converting the names into ids
public:
Graph(); // class constructor
void addNode(string key, int id){
node *item = new node(key, id);
}
int getNodesLength(){
return nodes.size();
}
};
int main()
{
Graph * graph = new Graph();
std::string s;
std::string word;
while(std::getline(std::cin, s)) {
for(char c : s) {
if (c == ' '){ // right side words
graph->addNode(word, graph->getNodesLength());
word = "";
} else if (c == ':') { // left side word
graph->addNode(word, graph->getNodesLength());
word = "";
} else { // letter char
word += c; //
}
}
}
return 0;
}
Graph * graph = new Graph();
Graph
已经在顶级命名空间。
您还忘记提供 constructor
说明你没有实现Graph的构造函数class。要么不要在 header 中声明它(这样编译器将为您生成默认构造函数),或者如果您这样做,请实现它。