我正在尝试为我的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(Previous Question)一起使用的较早步骤,此后通过将结构定义移至readcommand.h文件的顶部来解决了该问题。现在,我陷入了这个错误,并且不确定下一步要去哪里。

这是文件的相关部分:

readcommand.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;
  }
}


readcommand.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);
}




我应该以其他方式初始化“链表列表”,还是只是要求对声明进行另一次重新安排?

提前感谢您的帮助。



更新:有了斯蒂芬·林提到的更改,现在我得到的错误是:

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);


readcommand.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);
}

最佳答案

删除行:

// Struct Definitions
struct node;
struct linked_list;


您正在使用声明为类readcommand本地的新类型来隐藏这些结构的全局类型定义

也:

typedef struct node node_t;
typedef struct linked_list linked_list_t;


可以作为:

typedef node node_t;
typedef linked_list linked_list_t;


在C ++中是首选,尽管前者也可以使用。

编辑:

另外,您的函数未正确定义,它们被定义为全局函数,而不是成员函数:

static void initialize (linked_list *list) {
    // ...
}


应该

static void readcommand::initialize (linked_list *list) {
    // ...
}


和其他定义类似。请注意,由于似乎所有函数都是static(除了一个?),并且没有成员变量,因此除了名称空间之外,您实际上并没有将readcommand类用于任何其他内容..您没有使用任何面向对象的功能。这是可以接受的,但在这种情况下,这并不是您的意图,因为您正在实例化类readcommand的对象,并使用点(.)运算符在其上调用静态函数,这是可能的,但不提供服务目的。

您可能打算不使用static,而是要使list成为readcommand的成员变量,但我不确定100%。否则,您可以完全跳过创建对象,并将所有内容都称为readcommand::initialize(...)等。

08-06 01:02