【问题标题】:Private Node Ctor in LL troubleLL 麻烦中的私有节点
【发布时间】:2010-11-20 10:41:41
【问题描述】:

以下是链表头文件:

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

酒厂 ctor 有 4 个参数,如下所示:

// 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 init 列表进行初始化 私有成员变量。

//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

}

然后我们去链表:

//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: unresolved external symbol "public: __thiscall list::node::node(class winery const &)" (??0node@list@@QAE@ABVwinery@@@Z) 引用函数“公共:void __thiscall list::insert(class winery const &)”(?insert@list@@QAEXABVwinery@@@Z)

因为节点ctor是链表私有的结构? list.cpp?

【问题讨论】:

  • 酒厂在哪里申报?我可以看看声明吗?
  • #ifndef WINERY #define WINERY #include class winery { public: winery(const char * const name, const char * const位置,常量 int 英亩,常量 int 等级);虚拟〜酒厂(无效); const char * const getName() const; const char * const getLocation() const;常量 int getAcres() 常量;常量 int getRating() 常量; // 显示酒厂列表的标题 static void displayHeadings(std::ostream& out);朋友 std::ostream& operator

标签: c++


【解决方案1】:

您正在为节点声明此构造函数,但没有其他构造函数:

node(const winery& winery)

然后,您正在为酒厂实现一个构造函数,但不是为节点(如图所示):

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

文件由于声明而编译,但链接器将失败。

你(在你展示的代码中)实际上没有在哪里实现 node.js 的构造函数。在某处,根据您的代码,您需要声明并实现一个将酒厂作为参数的构造函数。错误表示链接器找不到合适的构造函数。

【讨论】:

    【解决方案2】:

    您要么忘记定义(除了声明list::node 的构造函数,要么忘记链接从.cpp 生成的目标文件将带有该构造函数定义的文件添加到您的应用程序中。这个:

    node(const winery& winery);  
    

    只是一个声明,而不是一个定义(因为它没有正文)。

    【讨论】:

      【解决方案3】:

      您在哪里以及如何为node 的构造函数提供实现?它找不到它

      【讨论】:

        猜你喜欢
        • 2011-04-29
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多