C++ 需要结构包含/实现帮助(第2部分)C++;

C++ 需要结构包含/实现帮助(第2部分)C++;,c++,struct,header-files,doubly-linked-list,C++,Struct,Header Files,Doubly Linked List,我正在尝试修改一些代码,以便在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

我正在尝试修改一些代码,以便在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*)和transverse(linked_list*,void(*callback)(char*)函数也有类似的错误(头包含在mysh.cpp中)


几天前,我遇到了一个问题,涉及到让头文件与mysh.cpp()一起工作的前一步,后来通过将结构定义移到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;
  }
}
#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);
}
read.traverse(& list, read.print);
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);
}
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);

};

更新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*))
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;
  }
}
#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);
}
read.traverse(& list, read.print);
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 node node_t;
typedef linked_list linked_list_t;

是C++的首选,虽然前者也适用。

编辑:

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

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

static void readcommand::initialize (linked_list *list) {
    // ...
}
对于其他定义也是如此。请注意,因为您的所有函数似乎都是静态的(除了一个?)你没有成员变量,除了名称空间之外,你没有真正使用类
readcommand
。也就是说,你没有使用任何面向对象的特性。这是可以接受的,但在这种情况下,这似乎不是你的意图,因为你正在实例化类
readcommand
的对象并调用static fun在其上使用点(
)运算符执行操作,这是可能的,但没有任何作用


您可能不想使用
静态
,而是想让
列表
成为
readcommand
的成员变量,但我不是100%确定。否则,您可以完全跳过创建对象,并将所有内容都称为
readcommand::initialize(…)
,等等。

谢谢,这确实减少了一些错误,但我仍然对“未定义的对X方法的引用”有问题,我将其添加到问题的末尾,并使用您建议的更改更新代码。这是什么原因?
静态无效初始化(链接列表*列表)
应该是
static void readcommand::initialize(linked\u list*list)
,等等。啊,已经很久了。再次感谢。我已经更新了每个函数(删除了static并添加了readcommand::),现在我尝试调用
traverse
,它接受一个链接的列表和一个回调函数时遇到了一个错误(在本例中,我正在传递它。
read.print
)。
mysh.cpp:114:错误:调用'readcommand::traverse(linked_list*,)'readcommand时没有匹配的函数。h:35:注意:候选函数是:void readcommand::traverse(linked_list*,void(*)(char*))
我将在问题中添加打印功能。应该是
&readcommand::print
readcommand::print
,如果可能也可以接受,但我更喜欢前者),但实际上在这种情况下
print
需要是
静态的
。谷歌表示“指向函数的指针”和“指向成员函数的指针”(虽然后者可能马上有点太多了…)是的,这肯定解决了它。我不知道我应该在头文件中将print函数声明为
static void print(char*);
,但是
void readcommand::print(char*word)在.CPP文件中,自从上次C++尝试以来,已经有相当一段时间了。现在所有的工作都很好,所以最后一次谢谢。