8

このエラーが発生し、自分で解決できません

source.cpp:85:8: error: request for member ‘put_tag’ in ‘aux’, which is of non-class type ‘Keyword()’
source.cpp:86:8: error: request for member ‘put_site’ in ‘aux’, which is of non-class type ‘Keyword()’
make: *** [source.o] Error 1

私にこのエラーを与えるコードは

Keyword aux();
aux.put_tag(word);
aux.put_site(site);

単語とサイトはchar *タイプであることに言及しなければなりません

さて、私の Keyword クラスの定義は次のとおりです。

class Keyword{
 private:

std::string tag; 
Stack<std::string> weblist;

public:

    Keyword();
    ~Keyword();
    void put_tag(std::string word)
    {
        tag = word;
    }
    void put_site(std::string site)
    {
        weblist.push(site);
    }

};

どうもありがとうございました!

アップデート

改造することで

Keyword aux();
aux.put_tag(word);
aux.put_site(site);

Keyword aux;
aux.put_tag(word);
aux.put_site(site);

私はこのエラーを受け取りました:

source.o: In function `Algorithm::indexSite(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
source.cpp:(.text+0x2c6): undefined reference to `Keyword::Keyword()'
source.cpp:(.text+0x369): undefined reference to `Keyword::~Keyword()'
source.cpp:(.text+0x4a8): undefined reference to `Keyword::~Keyword()'
source.o: In function `Keyword::put_site(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
source.cpp:(.text._ZN7Keyword8put_siteESs[Keyword::put_site(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x2a): undefined reference to `Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make: *** [tema3] Error 1
4

3 に答える 3

20

この行はあなたが思うことをしません:

Keyword aux();

引数を取らず、 を返す関数を宣言しています。あなたはおそらく(括弧なしで)書くつもりでした:auxKeyword

Keyword aux;

typeのオブジェクトを宣言しますKeyword

アップデート:

あなたが得ている次のエラーに関しては、これは、クラスのコンストラクタとデストラクタの宣言があるが、定義がないためです。実際、あなたが得ているエラーは、コンパイラからではなく、リンカから来ています。

コンストラクタとデストラクタの簡単な定義を提供するには、次のように変更します。

Keyword();
~Keyword();

これに:

Keyword() { }
~Keyword() { }

または、これらのメンバー関数が何もしない限り、それらをまったく省略してください。コンパイラーがそれらを生成します (コンストラクターに関係する他のユーザー宣言コンストラクターを追加しない限り)。

于 2013-05-03T17:00:37.947 に答える
4

これではない

Keyword aux();
aux.put_tag(word);
aux.put_site(site);

でもこれは

Keyword aux;
aux.put_tag(word);
aux.put_site(site);

あなたのバージョンKeyword aux();では、変数宣言ではなく関数プロトタイプです。

于 2013-05-03T17:01:02.160 に答える
0

次のコードをメイン関数に入力すると、同じ問題が発生しました.List.hとList.cppファイルにListクラスが含まれています。

List<int,int> myList();
bool x=myList.isEmpty();

「非クラス型 'List()' である 'myList' のメンバー 'isEmpty' の要求」というエラーが表示されます。

エラーは、コンパイラが myList() を関数プロトタイプと見なすためです

コードを修正すると

List<int,int> myList;
bool x=myList.isEmpty();

「`List::List() への未定義の参照」というエラーと、デストラクタに関するいくつかの同様のエラーが発生しました。

このページのコードと回答をさらに調べたところ、List.cpp ファイルを main.cpp に含める必要があることがわかりましたが、List.h を List.cpp ファイルに含めましたが、この情報を伝える必要があるようですメインファイルも。このチュートリアルをさらに読むと、List.cpp を含めずにプロジェクトをコンパイルすると、List.h ファイルにプロトタイプがあるため正常にコンパイルされますが、リンカーがへの呼び出しを解決できないため、リンカー ステージで失敗する理由が説明されています。 List() を特定の関数に。

于 2016-12-02T20:48:11.233 に答える