私はスタンドアロンベースクラス(A)を実装しようとしているプロジェクトで作業しています後のクラス(BやCなど)で拡張することができます。私はそれらをすべて独立して使用できるようにしたいので、純粋な仮想関数はありません。派生クラスの各クラスのコピーを1つだけ必要とします。 Dのようないくつかのクラスは、以前に拡張されたすべてのクラスを結合し、それらのすべての機能を保持しながら追加のクラスを実装する必要があります。C++リンク時のベースクラス関数への未定義の参照(--not_vtables、コンストラクタ、テンプレート、または純粋仮想関数)
私はC++ 03、gcc 4.4.6、make 3.81を使用しています。
すべてがうまくコンパイルされますが、リンク時に「未定義のリファレンス」が「ベースクラスの関数」になっていますが、派生クラスの基本クラス関数を使用しようとします。
純粋な仮想関数を正しく実装していない、コンストラクタを使ってサルを試している、クラスのすべてのメンバーを実装できませんでした(vtableエラー)。私は(故意に)これらのことをやっているわけではない。
これらがテンプレートとして実装されていれば素晴らしいかもしれませんが、残念なことに複雑さのために可能ではありません。私は、実際のコードを投稿することはできませんが、私は基本的な構造と動作に似た例アップ嘲笑しました:クラスを宣言
ヘッダファイル:Aを実施し
#ifndef CODE_H
#define CODE_H
class A
{
protected:
int a_;
int b_;
int c_;
public:
explicit A() :
a_(0), b_(0), c_(0) {}
A(const int X, const int Y, const int Z) :
a_(X), b_(Y), c_(Z) {}
A(const A& X) :
a_(X.A()), b_(X.B()), c_(X.C()) {}
int doA1(const A& X) const;
int doA2() const;
A getA() const;
A& operator=(const A& X);
const int& A() const {return a_;}
const int& B() const {return b_;}
const int& C() const {return c_;}
//One for each operator but not shown for brevity
A operator+(const A& X) const;
void operator+=(const A& X);
A operator+(const int X) const;
void operator+=(const int X);
};
class B : public virtual A
{
protected:
int d_;
public:
explicit B() :
A(), d_(0) {}
B(const int D, A X) :
A(X), d_(D) {}
B(const int D) :
d_(D) {}
int doB1(const B& X) const;
B getB() const;
B& operator=(const B& X);
const int& D() const {return d_;}
//One for each operator but not shown for brevity.
//int d_ doesn't need to be modified by the operators, so it's ignored in their implementation.
B operator+(const B& X) const {return B(d_, (A::operator+(X.getA())));}
void operator+=(const B& X) {A::operator+=(X.getA());}
B operator+(const int X) const; {return B(d_, (A::operator+(X)));}
void operator+=(const int X) {A::operator+(X);}
};
class C : public virtual A
{
protected:
int e_;
int f_;
int g_;
public:
explicit C() :
A(), e_(0), f_(0), g_(0) {}
C(const int U, const int V, const int W,
const int X, const int Y, const int Z) :
A(U, V, W), e_(X), f_(Y), g_(Z) {}
C(const A& W,
const int X, const int Y, const int Z) :
A(W), e_(X), f_(Y), g_(Z) {}
C(const C& X) :
C(X.A(), X.B(), X.C()), e_(X.E()), f_(X.F()), g_(X.G()) {}
int doC1(const C& X) const;
C getC() const;
C& operator=(const C& X);
const int& E() const {return e_;}
const int& F() const {return f_;}
const int& G() const {return g_;}
//One for each operator but not shown for brevity
C operator+(const C& X) const;
void operator+=(const C& X);
C operator+(const int X) const;
void operator+=(const int X);
};
class D : public virtual B, public virtual C
{
public:
explicit D() :
B(), C() {}
D(const int D, C Y) :
B(D), C(Y) {}
int doD1(const D& X) const;
int doD2() const;
D getD() const;
D& operator=(const D& X);
//One for each operator but not shown for brevity.
D operator+(const D& X) const {return D(d_, (C::operator+(X.getC())));}
void operator+=(const D& X) {C::operator+=(X.getC());}
D operator+(const int X) const; {return D(d_, (C::operator+(X)));}
void operator+=(const int X) {C::operator+(X);}
};
#endif
ファイル:
#include header.h
int A::doA1(const A& X) const
{
int doStuff;
//does stuff.
return doStuff;
}
int A::doA2() const
{
int doStuff2;
//does stuff.
return doStuff2;
}
A A::getA() const
{
A out(a_, b_, c_);
return out;
}
A& A::operator=(const A& X)
{
if (this != &X)
{
a_ = X.a_;
b_ = X.b_;
c_ = X.c_;
}
return *this;
}
//One for each operator but not shown for brevity
A A::operator+(const A& X) const
{
return A(a_ + X.a_, b_ + X.b_, c_ + X.c_);
}
void A::operator+=(const A& X)
{
a_ += X.a_;
b_ += X.b_;
c_ += X.c_;
}
A A::operator+(const int X) const
{
return A(a_ + X, b_ + X, c_ + X);
}
void A::operator+=(const int X)
{
a_ += X;
b_ += X;
c_ += X;
}
をBを実装
ファイル:
#include header.h
int B::doB1(const B& X) const
{
//Linker complains on both doA1 and getA, even if I qualify them or remove all the qualifiers
return (A::doA1(X.getA()));
}
B B::getB() const
{
//Also complains here, with or without qualifiers.
B out(d_, A::getA());
return out;
}
B& B::operator=(const B& X)
{
if (this != &X)
{
//Also here, for presumably the same reason.
A::operator=(D.getA());
d_ = X.d_;
}
return *this;
}
Iは、しかし、同様にCとDのためのモックアップを提供することができます彼らはAとBと同じ基本パターンに従います。
これまでに見た回答に基づいて、偶然の名前の隠れを避けるために、基本クラス修飾子を関数に追加するだけですみます。それは動作していないようです。また、不思議なことに、インスタンス化されたオブジェクトは(doB1のように)パブリック関数を見つけることができないようです。私はちょうど何か明白なものを欠いている(私はCの背景から来て驚かないだろう)?
本当にあなたの「モックアップ」にメンバー変数と複数のメンバー関数を含める必要はありますか?ノイズを減らしてください。 [mcve]を投稿してください。 – StoryTeller
変更をロールバックしました。それらのタイプミスはエラーであり、大きなものです。答えにある修正を適用するのではなく、[mcve]で作業する時間を取ってください。 – StoryTeller
@StoryTellerあなたはその答えの下のコメントを読んだことがありますか?なぜそれが修正されたのか、それは完全に合法的だと説明しています。それで、コンパイルして問題のエラーを表示するまで、コードを投稿しないでください。 –