2016-04-26 13 views
0

私は時々前者をテストベッドとして使い、次にXCodeで実際のプロジェクトにコードを移動します。この場合私にとってはうまくいきません。次のコードは、Coliru(cat /Archive2/48/70c3935989bffb/main.cppを参照)ではコンパイルして実行しますが、XCodeでは実行しません。ここでなぜこのコードはColiruでコンパイルされますが、Xcodeではコンパイルされませんか?

#include <cassert> 

template <typename T, typename P> 
class Permutation 
{ 
public: 

    virtual bool operator==(const P& other) const; 
    // other member functions not listed here use the type T 
}; 

template <typename T> 
class SmallPermutation : public Permutation<T, SmallPermutation<T> > 
{  
public: 

    SmallPermutation(int i);  
    virtual bool operator==(const SmallPermutation& other) const; 
    int code; 
    // other member functions not listed here use the type T 
}; 


template <typename T> 
SmallPermutation<T>::SmallPermutation(int i) 
{ 
    code = i; 
} 

template <typename T> 
bool SmallPermutation<T>::operator==(const SmallPermutation& other) const 
{ 
    return code == other.code; 
} 

int main() 
{  
    SmallPermutation<int> a(4); 
    SmallPermutation<int> b(7); 
    SmallPermutation<int> c(4); 

    assert(a == c); 
    assert(!(a == b)); 

    return 0; 
} 

は(私は理解していない)XCodeのからのエラーメッセージの一部です:

Undefined symbols for architecture x86_64: 
"Permutation<int, SmallPermutation<int> >::operator==(SmallPermutation<int> const&) const", referenced from: 
    vtable for Permutation<int, SmallPermutation<int> > in permutationTest.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

は、コードに関する標準外の何かがありますか?私が調整する必要があるXCodeにビルド/コンパイル/環境設定がありますか?

背景:私は、同じインターフェースを持ついくつかの(テンプレート)クラスを持っており、すべてが抽象クラスから継承したいと考えています。テンプレートは、この作業を少し難しくします。私はCRTP(Curiously Recurring Template Pattern)というテクニックを使って(おそらく誤って)それを起こさせました。

答えて

1

Permutationクラスのoperator==ファンクションのような非純粋な仮想ファンクションを宣言する場合は、そのファンクションに関数本体を指定する必要があります。

解決策は、それが純粋にするのどちらかである:私のビルドを固定

virtual bool operator==(const P& other) const { return false; } 
+0

このシンプルな調整:

virtual bool operator==(const P& other) const = 0; 

またはダミー実装を提供するために!私はまだ2つのフレームワークにおける動作の違いがなぜ疑問です。 – sitiposit

関連する問題