2013-03-02 31 views
0

我試圖適應位的代碼被包含在我mysh.cpp文件中使用的雙向鏈表,和我越來越結構包括/實施幫助需要(PT 2)C++

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文件中添加(linked_list *,char *)和遍歷(linked_list *,void(* callback)(char *))函數的類似錯誤(頭文件包含在mysh.cpp中


我前幾天遇到了一個問題,涉及到讓我的頭文件與mysh.cpp一起工作的早期步驟(Previous Question ),並且自此解決了將struct定義移動到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); 
} 

我應該以不同的方式來初始化「linked_list名單」,還是這僅僅需要聲明的另一個重新安排?

非常感謝您的幫助。


UPDATE:由斯蒂芬·林提到的變化,我得到的錯誤現在:

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

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

回答

1

刪除線:

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

+0

謝謝,這對一些錯誤肯定降不下來,但我仍然有「未定義參考X法」的問題,我已經添加到問題的最後,並用您提出的更改更新代碼。 這是什麼造成的? – LeMazing 2013-03-02 03:08:17

+0

'static void initialize(linked_list * list)'應該是'static void readcommand :: initialize(linked_list * list)'等。 – 2013-03-02 03:11:47

+0

啊,它已經這麼久了。再次感謝。 我已經更新了各個功能(去除靜態和補充readcommand::)現在我只是得到一個錯誤,當我嘗試調用'traverse',它接受一個linked_list和一個回調函數(在這種情況下,我'傳遞'read.print')。 'mysh.cpp:114:錯誤:沒有匹配函數調用'readcommand :: traverse(linked_list *,<未解析的重載函數類型>)' readcommand.h:35:注意:候選項是:void readcommand ::遍歷(linked_list *,void(*)(char *))' 我會將打印功能添加到問題中。 – LeMazing 2013-03-02 03:24:02