未解决的外部符号刚刚出现。构造函数麻烦

时间:2015-04-07 17:28:57

标签: c++ visual-studio-2013

我得到了这三个相关的(我认为)错误。 任何帮助,将不胜感激。这是一个更大的项目的一部分,但我很久以前为了一个不同的项目完成了大部分代码,它确定了。我已经开始尝试使用InsertAsFirst,但却无处可去。

错误2错误LNK2019:未解析的外部符号" public:__ thishisall Node :: Node(double,class Node *)" (?? 0Node @@ QAE @ NPAV0 @@ Z)在函数&#34中公开; public:void __thiscall List :: insertAsFirst(double)" (?insertAsFirst @ List @@ QAEXN @ Z)

错误1错误LNK2019:未解析的外部符号" public:__ thishisall Node :: Node(double)" (?? 0Node @@ QAE @ N @ Z)在函数&#34中引用; private:static class Node * __cdecl List :: clone(class Node *)" (?clone @ List @@ CAPAVNode @@ PAV2 @@ Z)

错误3错误LNK1120:2未解析的外部

#include "LinkedList.h"
#include "Stack.h"
#include <iostream>
#include <utility>

using std::ostream;


        List::List()
    : _first(nullptr)
{
}


           List::List(const List & other)
    : _first(clone(other._first))
{
}


List::~List()
{
    while (!empty())
    {
        removeFirst();
    }
}


const List & List::operator=(const List & other)
{
    // check for list = list
    if (&other != this)
    {
        // clear the current contents of this List
        this -> ~List();
        // and get a copy of other
        _first = clone(other._first);
    }

    return *this;
}


bool List::empty() const
{
    return _first == nullptr;
}


void List::insertAsFirst(double x)
{
    if (empty())
        _first = new Node(x,nullptr);
    else{
        Node * ptr = new Node(x,nullptr);
        ptr->_next = _first;
        _first = ptr;
    }
}






double List::removeFirst(){

    double item = _first->_entry;
    _first = _first->_next;
    return item;
}

double List::removeLast() {

    //returns zero if the list is empty
    double toReturn = 0;

    if (!empty()){
        Node * ptr = _first;

        //this is used for a list with 1 number
        if (ptr->_next == nullptr){
            toReturn = ptr->_entry;
            ptr = nullptr;
            return toReturn;
            //std::cout << "im heree in if statemen" << std::endl;
        }

        //used for a list with exactly 2 numbers
        if (ptr->_next->_next == nullptr){
            //  std::cout << " i made it in  the special if statment" << std::endl;
            toReturn = ptr->_next->_entry;
            ptr->_next = nullptr;

            return toReturn;
        }

        //used for a list with  more than 2 numbers
        //  std::cout << "before while loop" << std::endl;
        while (ptr->_next->_next != nullptr){
            //  std::cout << "while loop" << std::endl;
            ptr = ptr->_next;
            //std::cout << "while loop past next ptr" << std::endl;
        }
        toReturn = ptr->_next->_entry;
        ptr->_next = nullptr;
    }
    return toReturn;
}



double List::size(){
    double count = 0;
    if (_first == nullptr)
        return 0;

    else if(_first != nullptr){

        count = 1;
        Node * ptr = _first->_next;
        while (ptr != nullptr){
            count++;
            ptr = ptr->_next;
        }
        return count;
    }
    return count;
}

double List::sum(){

    double thesum = 0;
    if (!empty()){

        thesum = _first->_entry;
        Node * ptr = _first->_next;
        while (ptr != nullptr){

            thesum += ptr->_entry;
            ptr = ptr->_next;
        }
    }
    return thesum;
}


void List::insertAsLast(double x){

    if (empty()){
        insertAsFirst(x);
    }
    else{

        Node * ptr = _first;
        while (ptr->_next != nullptr){
            ptr = ptr->_next;
        }
        ptr->_next = new Node(x, nullptr);
    }
}

     double List::returnFirst(){
    return _first->_entry;
}



      void List::print(ostream & outfile) const
     {
               outfile << "[ ";
         if (!empty())
    {
        // The first entry is printed separately because no comma
        // is needed.
        outfile << _first->_entry;
        Node * ptr = _first->_next;
        while (ptr != nullptr)
        {
            outfile << ", " << ptr->_entry;
            ptr = ptr->_next;
        }
    }
    outfile << " ]";
}


     // Iterative version of clone.
     // This version walks down the linked structure making a
     //   new Node for each double in the structure.
     Node * List::clone(Node * ptr)
{
        if (ptr == nullptr)
    {
        return nullptr;
    }
    Node * first = new Node(ptr->_entry);
    Node * last = first;
    ptr = ptr->_next;
    while (ptr != nullptr)
    {
        last->_next = new Node(ptr->_entry);
        last = last->_next;
        ptr = ptr->_next;
    }
    return first;
}


//// Recursive version of clone.
//// This version handles two cases, a linked structure with
////   no Nodes, and a linked structure with one or more
////   Nodes.
//Node * List::clone( Node * ptr )
//{
//   if( ptr == NULL )
//   {
//      return NULL;
//   }
//   return new Node( ptr->_entry, clone( ptr->_next ) );
//}


     ostream & operator<<(ostream & outfile, const List & list)
{
    list.print(outfile);
    return outfile;
}









     #ifndef _NODE_H_
#define _NODE_H_

#include <iostream>


class Node
{
public:
    double _entry;
    Node * _next;

public:
    // Constructors

    // post: this Node contains entry and a NULL pointer
    explicit Node(double entry);

    // post: this Node contains entry and next
    Node(double entry, Node * next);

    // Destructor

    ~Node();

     private:
    // Inaccessible standard functions
    Node();
    Node(const Node &);
    const Node & operator=(const Node &);
    Node(Node &&);
    const Node & operator=(Node &&);
};

#endif

1 个答案:

答案 0 :(得分:0)

您声明了构造函数,但从未为该构造函数提供定义。

Node::Node(double entry);

这一行试图调用该构造函数

last->_next = new Node(ptr->_entry);