由于符号,C ++链接器失败

时间:2015-11-07 01:54:12

标签: c++ linker-errors

我正在编写一些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;
}

2 个答案:

答案 0 :(得分:1)

这意味着您没有实现Graph类的构造函数。要么不在标题中声明它(因此编译器将为您生成默认构造函数),或者如果您这样做,请实现它。

答案 1 :(得分:0)

Graph * graph = new Graph(); Graph已位于顶级命名空间。

您也忘了提供constructor