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

标签 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: 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 *)) 函数的类似错误( header 包含在 mysh.cpp 中


几天前我遇到了一个问题,涉及让我的头文件与 mysh.cpp ( Previous Question ) 一起工作的早期步骤,并且已经通过将结构定义移动到 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;
  }
}

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

我应该以不同的方式初始化“linked_list list”,还是仅仅需要重新安排声明?

提前感谢您的帮助。


更新:随着 Stephen Lin 提到的更改,我现在得到的错误是:

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

最佳答案

删除行:

// 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(...)

关于c++ - 需要结构包含/实现帮助(第 2 部分)C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170155/

相关文章:

c++ - Doxygen 详细描述渗透到简短描述中

c - 使用 union 强制 C 位域对齐

c - 取消引用数组中结构中字符串的 C 指针?

c++ - 如何将鼠标点击和拖动发送到后台的另一个程序?

c++ - 将 Child 对象数组传递给接受 Parent* 的函数

c++ - "no appropriate default constructor available"用于 C++ 派生类

c++ - 为什么头文件 Head1.h 不能包含包含 Head1.h 的头文件 Head2.h?

c++ - 包含中的递归(文件 1 包含包含文件 1 的文件 2)

c - 在c中包含自定义头文件

c++ - 结构对象析构函数