gpt4 book ai didi

C++ 程序编译器错误 : No Matching Function

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:24 26 4
gpt4 key购买 nike

我正在使用链表来实现索引程序。尝试使用 g++ 编译程序时出现以下编译器错误:

concordancetest.cpp:在函数“void build_list(std::ifstream&, char*)”中:
concordancetest.cpp:65: 错误:没有匹配函数来调用‘Concordance::insert(char*&, int&)’
concordance.h:22: 注意:候选人是: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;
}

最佳答案

char array[] 的类型是char *,所以在查找匹配的函数时,没有找到。您可以通过使用 typedef char* Word; 并在需要它的函数中强制执行最大长度来解决此问题。

关于C++ 程序编译器错误 : No Matching Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com