未定义的符号:vtable

时间:2013-06-06 14:47:37

标签: c++ gcc c++11 vtable

所以这是其中一个编译器错误,我无法找出问题的根源。

这是课程LocalQualityMeasure,标题

#include <unordered_set>

#include "../clustering/Clustering.h"
#include "../graph/Graph.h"

namespace Project {

/**
 * Abstract base class for all local clustering quality measures.
 */
class LocalQualityMeasure {


public:

    LocalQualityMeasure();

    virtual ~LocalQualityMeasure();

    virtual double getQuality(std::unordered_set<node>& C, Graph& G) = 0;
};

} /* namespace Project */

和来源:

#include "LocalQualityMeasure.h"

namespace Project {

LocalQualityMeasure::LocalQualityMeasure() {
}

LocalQualityMeasure::~LocalQualityMeasure() {
    // TODO Auto-generated destructor stub
}


} /* namespace Project */

这是派生类Conductance,标题

#include <algorithm>

#include "LocalQualityMeasure.h"

namespace Project {

class Conductance: public Project::LocalQualityMeasure {
public:
    Conductance();
    virtual ~Conductance();

    virtual double getQuality(std::unordered_set<node>& C, Graph& G);
};

} /* namespace Project */

和来源

#include "Conductance.h"

namespace Project {

Conductance::Conductance() : LocalQualityMeasure() {
}

Conductance::~Conductance() {
    // TODO Auto-generated destructor stub
}

double getQuality(std::unordered_set<node>& C, Graph& G) {

    double volume = 0;
    double boundary = 0;
    double all = 0;

    for (auto it = C.begin(); it != C.end(); ++it) {
        volume = volume + G.degree(*it);
        G.forNeighborsOf(*it, [&](node v){
            if (C.find(v) == C.end()) boundary++;
        });
    }

    G.forNodes([&](node v){
        all = all + G.degree(v);
    });

    if (volume == 0 || all-volume == 0)
        return 1;
    return boundary / std::min(volume, all-volume);
}

} /* namespace Project */

链接器抱怨缺少vtable:

Undefined symbols for architecture x86_64:
  "Project::Conductance::getQuality(std::unordered_set<long long, std::hash<long long>, std::equal_to<long long>, std::allocator<long long> >&, Project::Graph&)", referenced from:
      vtable for Project::Conductance in Conductance.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [Project-CommunityDetection-DPar] Error 1

我将GCC 4.7与--std=c++11一起使用。

2 个答案:

答案 0 :(得分:7)

您忘记了getQuality的实施中的课程范围:

double Conductance::getQuality(std::unordered_set<node>& C, Graph& G) 
{
  .....
}

答案 1 :(得分:2)

您需要在实现文件中添加类范围:

double Conductance::getQuality(std::unordered_set<node>& C, Graph& G) {...

否则链接器无法找到标头中定义的方法的实现。