2013-09-23 69 views
1

我正在使用鏈表來實現一致性程序。我正在試圖編譯使用g ++程序時,以下編譯器錯誤:C++程序編譯器錯誤:無匹配函數

concordancetest.cpp:在功能 '無效build_list(性病:: ifstream的&,炭*)':
concordancetest.cpp:65:錯誤:沒有匹配函數調用'Concordance :: insert(char * &,int &)'
concordance.h:22:note:candidates are:void Concordance :: insert(char(&)[9],int & )

以下是我寫的代碼:

頭文件:

#ifndef CONCORDANCE_H 
#define CONCORDANCE_H 

#include <iostream> 
#include <cstdlib> 

const int MAX = 8; 

class Concordance 
{ 
    public: 
     //typedef 
     typedef char Word[MAX+1]; 

     //constructor 
     Concordance(); 

     //destructor 
     ~Concordance(); 

     //modification member functions 
     void insert(Word& word, int& n); 
     void remove(Word& word); 
     int get_count(Word& word); 

     //constant member functions 
     int length() const; 

     //friend member functions 
     friend std::ostream& operator << (std::ostream& out_s, Concordance& c); 

    private: 
     struct Node 
     { 
      Word wd; 
      int count; 
      Node *next; 
     }; 
     Node *first;  

     Node* get_node(Word& word, int& count, Node* link); 
}; 

#endif 

實現代碼:

//class definition 
#include "concordance.h" 
#include <iostream> 
#include <cstring> 
#include <iomanip> 
using namespace std; 

Concordance::Concordance() 
{ 
    first = NULL; 
} 

Concordance::~Concordance() 
{ 
    Node *temp; 
    while(first != NULL) 
    { 
     temp = first; 
     first = first -> next; 
     delete temp; 
    } 
} 

void Concordance::insert(Word& word, int& n) 
{ 
    Node *prev; 

    if(first == NULL || strcmp(first -> wd, word) > 0) 
     first = get_node(word, n, first); 
    else 
    { 
     prev = first; 

     while(prev -> next != NULL && strcmp(prev -> next -> wd, word) > 0) 
      prev = prev -> next; 
     prev -> next = get_node(word, n, prev -> next); 
    } 
} 

void Concordance::remove(Word& word) 
{ 
    Node *prev, *temp; 
    prev = temp; 
    if(prev -> wd == word) 
    { 
     first = first -> next; 
     delete prev; 
    } 
    else 
    { 
     while(strcmp(prev -> next -> wd, word) > 0) 
      prev = prev -> next; 
     temp = prev -> next; 
     prev -> next = temp -> next; 
     delete temp; 
    } 
} 

int Concordance::get_count(Word& word) 
{ 
    while(strcmp(first -> wd, word) != 0) 
     first = first -> next; 

    return first -> count; 
} 

int Concordance::length() const 
{ 
    Node *cursor; 
    int len; 

    len = 0; 
    for(cursor = first; cursor != NULL; cursor = cursor -> next) 
     len++; 
    return len; 
} 

Concordance::Node* Concordance::get_node (Word& word, int& count, Node* link) 
{ 
    Node *temp; 

    temp = new Node; 
    strcpy(temp-> wd, word); 
    temp-> next = link; 
    temp -> count = count+1; 
    return temp; 
} 

ostream& operator << (ostream& out_s, Concordance& c) 
{ 
    Concordance::Node *cursor; 

    out_s << "Word" << setw(10) << " " << "Count" << endl; 
    out_s << "--------------------" << endl; 

    for(cursor = c.first; cursor != NULL && cursor->next != NULL; cursor = cursor-> next) 
     out_s << cursor-> wd << setw(10) << " " << cursor -> count << endl; 

    if(cursor != NULL) 
     out_s << cursor-> wd << setw(10) << " " << cursor -> count << endl; 
    out_s << "--------------------" << endl; 

    return out_s; 
} 

測試程序:

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <cstring> 
#include "concordance.h" 
using namespace std; 

void read_word(ifstream& in_file, char array[]); 
void build_list(ifstream& in_file, char array[]); 

int main() 
{ 
    char file_name[100]; 
    ifstream in_file; 
    char array[MAX+1]; 

    cout << "Enter a file name: "; 
    cin >> file_name; 

    in_file.open(file_name); 

    build_list(in_file, array); 

    in_file.close(); 

    return EXIT_SUCCESS; 
} 

void read_word(ifstream& in_file, char array[]) 
{ 
    char ch; 
    int i = 0; 

    in_file.get(ch); 

    while(isalpha(ch) && !isspace(ch)) 
    { 
     if(i > MAX-1) 
     { 
      while(!isspace(ch)) 
      in_file.get(ch); 
      break; 
     } 

     ch = tolower(ch); 
     array[i] = ch; 
     i++; 
     in_file.get(ch); 
    } 

    for(int j = 0; j < i; j++) 
     cout << array[j]; 
    cout << endl; 

} 

void build_list(ifstream& in_file, char array[]) 
{ 
    Concordance c; 
    int count = 0; 

    while(!in_file.eof()) 
    { 
     read_word(in_file, array); 
     c.insert(array, count); 
    } 

    cout << c; 
} 
+0

你的代碼試圖傳遞一個指向數組引用的指針;你不能那樣做。 – Kal

+1

參考[只使用標準庫](http://coliru.stacked-crooked.com/a/369bdd3c5a5de272)建議 – sehe

回答

1

類型的char array[]char *,所以當它查找匹配功能的,沒有一個找到。您可以通過使用typedef char* Word;並在需要它的函數內強制實現最大長度來解決此問題。

+0

更改'void insert(Word&word,int & n);'to'void insert(Word word,int '通過引用傳遞數組毫無意義 – jodag