0

私は、コンパイラとしてLLVM clang ++(FreeBSDのバージョン3.1)を使用して、 WileyのTeach Yourself C ++ (7th Edition)のサンプルプログラムを使用しています。インチ。23、「クラス継承」、次のコードが提供されます。

Date.h-基本クラス:

...snip..
inline void Date::Display(std::ostream& os)
   { os << "Date: " << *this; }
...snip...

SpecialDate.h-Dateから派生し、Display()関数をオーバーロードするクラス:

#include "Date.h"
...snip...
void Display(std::ostream& os)
   { os << "SpecialDate: " << *this; }
...snip...

23-1.cpp-SpecialDateのオブジェクトをインスタンス化し、それを使用します。

#include <iostream>
#include "SpecialDate.h"
int main()
{
...snip... 
// da, mo, & yr are all ints and declared
SpecialDate dt(da, mo, yr);
...snip...
dt.Display();
...snip...

コードをコンパイルすると、次のエラーが発生します。

./23-1.cpp:14:14: error: too few arguments to function call, expected 1, have 0
            dt.Display();
            ~~~~~~~~~~ ^
./SpecialDate.h:15:2: note: 'Display' declared here
    void Display(std::ostream& os)
    ^
1 error generated.

std :: ostream型変数を参照によってDisplay()に渡す必要があることがわかりますが、続行する方法に困惑しています。FWIW:Date.hとSpecialDate.hの両方が<<演算子をオーバーロードします。私はもう試した:

dt.Display(std::cout); /* all sorts of weird compiler errors */
...
std::ostream os; /* error: calling a protected constructor of class */
dt.Display(os);
...
std::cout << dt.Display(); /* same "too few arguments" error */

このコードをコンパイルするにはどうすればよいですか?

編集:クリス、clang ++エラーdt.Display(std::cout);

/tmp/23-1-IecGtZ.o: In function `SpecialDate::Display(std::ostream&)':
./23-1.cpp:(.text._ZN11SpecialDate7DisplayERSo[_ZN11SpecialDate7DisplayERSo]+0x34):    undefined reference to `operator<<(std::ostream&, SpecialDate&)'
/tmp/23-1-IecGtZ.o: In function `Date::Date(int, int, int)':
./23-1.cpp:(.text._ZN4DateC2Eiii[_ZN4DateC2Eiii]+0x34): undefined reference to `Date::SetDate(int, int, int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

また、#include <iostream>Date.hとSpecialDate.hの両方にあります。

編集2:のエラーを読み直した後dt.Display(std::cout);、コンパイラがSpecialDateとDateのソースを認識しておらず、ヘッダーのみを認識していることに気付きました。だから私はそれらをコマンドラインに追加しました、そしてそれは最終的にエラーなしでコンパイルされました!

clang++ 23-1.cpp Date.cpp SpecialDate.cpp

残念ながら、動作は期待どおりではありません(日付は表示されていません)が、私の元の質問には回答されています。私はそれを突っ込み続けます。初心者の皆さんの忍耐に感謝します!

4

0 に答える 0