我正在尝试修改一些代码,以便在我的 mysh.cpp 文件中包含和使用双向链表,我得到了
mysh.cpp:88: error: no matching function for call to ‘readcommand::initialize(linked_list*)’
readcommand.h:32: note: candidates are: static void readcommand::initialize(readcommand::linked_list*)
mysh.cpp:100: error: no matching function for call to ‘readcommand::add(linked_list*, char*&)’
readcommand.h:34: note: candidates are: static void readcommand::add(readcommand::linked_list*, char*)
mysh.cpp:114: error: no matching function for call to ‘readcommand::traverse(linked_list*, void(char*))’
readcommand.h:38: note: candidates are: static void readcommand::traverse(readcommand::linked_list*, void (*)(char*))
以及我的 readcommand.cpp 文件中的 add(linked_list*, char*) 和 traverse(linked_list*, void (*callback) (char *)) 函数的类似错误(标题包含在 mysh.cpp
几天前我遇到了一个问题,涉及让我的头文件与 mysh.cpp (上一个问题)一起工作的较早步骤,并且通过将结构定义移动到 readcommand.h 文件的顶部解决了这个问题。现在我被这个错误困住了,不知道下一步该去哪里。
以下是文件的相关部分:
读取命令.cpp
static void initialize (linked_list *list) {
list->first = 0;
list->last = 0;
}
static void add (linked_list *list, char *word) {
node *nodeX;
nodeX = talloc();
if (! nodeX) {
fprintf (stderr, "allocation failure\n");
exit (EXIT_FAILURE);
}
nodeX->word = word;
if (list->last) {
list->last->next = nodeX;
nodeX->prev = list->last;
list->last = nodeX;
}
else {
list->first = nodeX;
list->last = nodeX;
}
}
读取命令.h
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
struct node {
const char *word;
node *prev;
node *next;
};
struct linked_list {
node *first;
node *last;
};
class readcommand {
public:
// Struct Definitions
typedef node node_t;
typedef linked_list linked_list_t;
// Creation
static void initialize (linked_list *list);
node *talloc ();
static void add (linked_list *list, char *word);
// Modification and Traversal
static void del_list (linked_list *list, node *nodeX);
static void traverse (linked_list *list, void (*callback) (char *));
static void reverse (linked_list *list, void (*callback) (char *));
static void traverse_delete (linked_list *list, int (*callback) (char *));
static void free (linked_list *list);
static int delete_all (char *word);
static void print (char *word);
};
mysh.cpp
#include "readcommand.h"
int main (int argc, char** argv) {
readcommand read;
linked_list list;
string input = "";
read.initialize (& list);
// Read input string here
getline (cin, input);
cout << endl;
// Parse words individually and add to linked list
int len = input.length();
char *str = (char *) input.c_str();
char *word = strtok (str, " ");
while (word != NULL) {
read.add (& list, word);
word = strtok (NULL, " ");
}
read.traverse(& list, read.print);
printf("\n");
return (0);
}
我应该以不同的方式初始化“linked_list list”,还是仅仅需要重新安排声明?
提前感谢您的帮助。
更新:随着斯蒂芬林提到的变化,我现在得到的错误是:
mysh.cpp:88: undefined reference to `readcommand::initialize(linked_list*)'
mysh.cpp:100: undefined reference to `readcommand::add(linked_list*, char*)'
mysh.cpp:114: undefined reference to `readcommand::print(char*)'
mysh.cpp:114: undefined reference to `readcommand::traverse(linked_list*, void (*)(char*))'
更新2:我的新错误:
mysh.cpp:114: error: no matching function for call to ‘readcommand::traverse(linked_list*, <unresolved overloaded function type>)’
readcommand.h:35: note: candidates are: void readcommand::traverse(linked_list*, void (*)(char*))
mysh.cpp
read.traverse(& list, read.print);
读取命令.cpp
void readcommand::traverse (linked_list *list, void (*callback) (char *)) {
node *nodeX;
for (nodeX = list->first; nodeX; nodeX = nodeX->next) {
callback ((char *) nodeX->word);
}
}
void readcommand::print (char *word) {
printf ("%s, ", (char *) word);
}