2009-08-18 67 views
0

以下是鏈表的頭文件:私人節點構造函數中LL麻煩

// list.h 
class list 
{ 
public: 
    list(void);    // constructor 
    virtual ~list(void); // destructor 
    void displayByName(ostream& out) const; 
    void displayByRating(ostream& out) const; 
    void insert(const winery& winery); 
    winery * const find(const char * const name) const; 
    bool remove(const char * const name); 

private: 
    struct node 
    { 
     node(const winery& winery);  // constructor 
     winery item; 
     node * nextByName; 
     node * nextByRating; 
    }; 

    node * headByName; 
    node * headByRating; 
}; 

酒廠構造函數有4 PARAM的如下:

// code in main.cpp 
// this statement is very important 
// I am trying to add the info to the list as a reference to the node ctor 
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95)); 

我執行代碼爲止。

我調試,它把我帶到ctor。我使用ctor初始化列表來初始化私人成員變量 。

//winery.cpp 
winery::winery(const char * const name, const char * const location, const int acres, const int rating) 
    : name(new char[strlen(name)+1]), location(new char[strlen(location)+1]), acres(0), rating(0) 
{ 
    // arcres, name, location, rating, are all private members of the winery class 

} 

然後我們去的LinkedList:

//list.cpp 
void list::insert(const winery& winery) 
{ 
    list *ListPtr = new list(); 
// here im trying to add all the info to the list: 
    node *NodePtr = new node(winery); 

} 

我得到一個鏈接錯誤:LNK2019:無法解析的外部符號 「公用:__thiscall目錄::節點::節點(級酒莊常量&)」 (?插入@列表@@ QAEXABVwinery @@@ Z)

(參見函數「public:void __thiscall list :: insert

因爲節點ctor是一個鏈接列表專用的結構? list.cpp?

+0

酒莊在哪裏申報?我可以看到聲明嗎? – jkeys 2009-08-18 00:47:02

+0

的#ifndef _WINERY_ 的#define _WINERY_ 的#include 級酒莊 { 市民: \t酒莊(爲const char * const的名字,爲const char * const的位置,const int的畝,const int的等級); \t虛擬酒莊(虛空); \t const char * const getName()const; \t const char * const getLocation()const; \t const int getAcres()const; \t const int getRating()const; \t //顯示標題爲酒莊的名單 \t靜態無效displayHeadings(STD :: ostream的& out); \t朋友的std :: ostream的和運營商<<(STD :: ostream的進出,釀酒* W); 私人: \t字符\t *名稱; \t字符\t *位置; \t INT 英畝; \t INT \t \t等級; }; #endif – user40120 2009-08-18 04:45:15

回答

2

你在哪裏以及如何爲node的構造函數提供實現?它不能找到它

1

你要麼忘記定義(除了聲明)構造爲list::node,或者你已經忘記了從.cpp文件生成的目標文件與構造函數的定義鏈接到你的應用程序中這:

node(const winery& winery); 

只是一個聲明,而不是一個定義(因爲它缺乏正文)。

3

你聲明此構造節點,但沒有其他的構造函數:

node(const winery& winery) 

後來的後來,你在爲實現酒廠構造函數,而不是節點(這是所示):

winery::winery(const char * const name, const char * const location, const int acres, const int rating) 

文件因聲明而編譯,但鏈接程序將失敗。

實際上實現了節點的構造函數,你沒有(在你展示的代碼中)。在某處,根據你的代碼,你需要聲明和實現一個以釀酒廠爲參數的構造函數。錯誤表示鏈接器找不到合適的構造函數。