2016-05-20 8 views
1

私は、クラス内で関数へのポインタを渡したいが間違いを示して、率直に言って、私は理由を知らない。チュートリアルに従っている。彼らは私がしている間は問題がありません。誰かがいくつかのヒントを与えることができますか?私は比較的新しいC++です。前もって感謝します。関数へのポインタC++

ここで私は辞書のプロトタイプを作っています。ここでは、単語の入力ファイルのペアから読み込み、リンクされたリストに保存する必要があります。私はCode :: Blocksプログラムを使用しています。それが書いている

#include <iostream> 
#include <fstream> 
#include <cmath> 
using namespace std; 
//------------------------------- 
struct node 
{ 
    string Language; 
    string English; 
    node* next; 
}; 
//------------------------------- 
class Dictionary //class where all functions will go 
{ 
public: 
    void readFirstElement(node *head, node *tail); 
    void readElements(node* head, node* tail); 
    // void insertElements(); 
    // void deleteElements(node* &head, node* &tail); 
//------------------------------- 
    void readFirstElement(node *head, node *tail) 
    { 
     string word1, word2; 
     node* temp; 
     ifstream input; 
     input.open("input.txt"); 
     input >> word1 >> word2; 
     temp=new node; 
     temp->Language=word1; 
     temp->English=word2; 
     temp->next=NULL; 
     cout << temp->Language <<" ir "<< temp->English << endl; 
     head=temp; 
     tail=temp; 

     input.close(); 
    } 
//------------------------------- 
    void readElements(node* &head, node* &tail) 
    { 
     string word1, word2; 
     node* temp; 
     ifstream input; 
     input.open("input.txt"); 
     while (!input.eof()) 
     { 
      input >> word1 >> word2; 
      temp=new node; 
      temp->Language = word1; 
      temp->English=word2; 
      tail->next=temp; 
      tail=temp; 
     } 
     tail->next=NULL; 
     input.close(); 
    } 
int main() 
{ 
    node* head = NULL; 
    node* tail = NULL; 
    Dictionary ob; 
    ob.readFirstElement(&head, &tail); 
    ob.readElements(&head, &tail); 
    node* temp = head; 
    return 0; 
} 

主な間違いは、エラーです: '無効辞書:: readFirstElement(ノード*、ノード*)' は、オーバーロードすることはできません。 .cpp実装ファイル内

+0

メソッド定義を実装ファイルに入れる必要があります。この場合、 'Dictionary'クラスに同じシグネチャを持つ2組のメソッドが宣言されていますが、これは無効です。 – ArchbishopOfBanterbury

答えて

0

入れメソッドの定義:あなたはDictionaryクラスで同じシグネチャを持つメソッド、二対のを宣言した時点で

Dictionary.h

#ifndef DICTIONARY_H 
#define DICTIONARY_H 

class Dictionary { 
    public: 
     void readFirstElement(node* head, node* tail) const; 
     void readElements(node* head, node* tail) const; 
     //... 
}; 

#endif 

Dictionary.cpp

#include "Dictionary.h" 

void Dictionary::readFirstElement(node* head, node* tail) const { 
    //... 
} 

void Dictionary::readElements(node* head, node* tail) const { 
    //... 
} 

ました無効です。

+0

ありがとうございます。私はまだこの方法に精通していないが、私はそれを試してみます –

1

これは、ポインタを渡すこととは特に関係ありません。あなたは議論に関係なく、議論が全くなくてもこの問題を抱えています。

メンバー関数を定義するには2通りの方法があります。

class X { 
    void foo() { 
     do_something(); 
    } 
}; 

また、宣言をクラス本体に配置してから、定義を外に置くこともできます。

class X { 
    void foo(); 
}; 

void X::foo() { 
    do_something(); 
} 

あなたのやったことは、クラス本体で関数を宣言してから、クラス本体でも別々に定義したことです。コンパイラはこれを2つの異なる関数と見なします。しかし、彼らは同じ名前と引数型を持っているので、それらを呼び出す試みはあいまいなので、許されません。上記のオプションの1つを選択します。

+0

ありがとう!今ははっきりしています。私はそれを修正し、プログラムが実行されていることを除いて、exeが動作を停止したことを除いて。関数readFirstElementsが機能していることは分かっていますが、ReadElementsで重大な間違いがあるのでしょうか?それとも私はそれらを組み合わせるべきですか? –

+1

@A.Black:別の質問がある場合は、別の投稿を作成してください。 –

関連する問題