LNK 2005& Visual Studio 2015中的LNK 1169错误

时间:2017-09-07 04:12:35

标签: c++ visual-c++ runtime-error

我的代码中出现了LNK 2005和LKN 1169错误(只有后者中的一个有七个前者)。就我而言,一切看起来都是正确的。我真的很感谢任何能够指引我朝着正确的方向寻求解决方案的人。我正在处理的代码是堆栈的链表实现。代码和错误列表如下所示。

LinkedList.h

#include <iostream>
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

using namespace std;

struct Node
{
    int data;
    Node* next;
};

class LinkedList
{
public:
    LinkedList(); // Constuctor
    ~LinkedList(); // Destructor
    void push(int number); // Pushes an element with the value number into the stack
    int pop(); // Pops an element from the stack
    int isEmpty(); // Returns 1 if the stack is empty, 0 otherwise
    int numOfElements(); // Returns the number of elements in the stack
    void printElements(); // Print out the current stack to the console

private:
    Node* head;
    Node* tail;
    int maxSize;
};

#endif

LinkedList.cpp

#include "LinkedList.h"

LinkedList::LinkedList()
{
    head = NULL;
    tail = NULL;
    maxSize = 20;
}

LinkedList::~LinkedList()
{
    Node* temp = head;

    while (temp != NULL)
    {
        temp = temp->next;
        delete head;
        head = temp;
    }
}

void LinkedList::push(int number)
{
    if (numOfElements() == maxSize)
    {
        cout << endl << endl << "The stack is full. Cannot push.";
    }

    else if (head == NULL)
    {
        Node* n = new Node;
        n->data = number;
        n->next = NULL;

        head = n;
        tail = n;

        cout << endl << endl << number << " was successfully pushed into the stack!";
    }

    else
    {
        Node* n = new Node;
        n->data = number;
        n->next = head;
        head = n;

        cout << endl << endl << number << " was successfully pushed into the stack!";
    }
}

int LinkedList::pop()
{
    if (head == NULL)
    {
        cout << endl << endl << "The stack is empty. Cannot pop.";

        return NULL;
    }

    // Case with only one element
    else if (head == tail)
    {
        Node* temp;
        temp = head;
        delete head;
        head = NULL;
        tail = NULL;

        return temp->data;
    }

    else
    {
        Node* temp;
        temp = head;
        head = head->next;

        return temp->data;
    }
}

int LinkedList::isEmpty()
{
    if (head == NULL)
    {
        return 1;
    }

    else
    {
        return 0;
    }
}

int LinkedList::numOfElements()
{
    if (head == NULL && tail == NULL)
    {
        return 0;
    }

    Node* temp = head;
    int counter = 0;

    while (temp != NULL)
    {
        counter++;
        temp = temp->next;
    }

    return counter;
}

void LinkedList::printElements()
{
    if (isEmpty() == 0)
    {
        Node* temp = head;

        while (temp != NULL)
        {
            cout << temp->data << endl;
            temp = temp->next;
        }
    }
}

的main.cpp

#include "LinkedList.cpp"

using namespace std;

// Prototypes
void showMenu();

int main()
{
    LinkedList stack; // Demonstrates usage of constructor
    int choice;

    cout << "Welcome to the Linked List Implementation of a Stack!" << endl << endl;    
    showMenu();

    cout << "Please Enter Your Choice: ";
    cin >> choice;

    while (choice < 1 || choice > 6)
    {
        cout << endl << endl << "I am sorry, that is not a valid choice. Please try again." << endl;
        showMenu();

        cout << "Please Enter Your Choice: ";
        cin >> choice;
    }

    if (choice == 1)
    {
        int value;

        cout << endl << endl << "Please enter a positive number that you wish to push into the stack: ";
        cin >> value;

        stack.push(value);
    }

    if (choice == 2)
    {
        int poppedData;

        poppedData = stack.pop();

        cout << endl << endl << poppedData << " was successfully popped from the stack!";
    }

    if (choice == 3)
    {
        int value;

        value = stack.isEmpty();

        if (value == 1)
        {
            cout << endl << endl << "The stack is empty!";
        }

        if (value == 0)
        {
            cout << endl << endl << "The stack is not empty!";
        }
    }

    if (choice == 4)
    {
        int elementsInStack;

        elementsInStack = stack.numOfElements();

        cout << endl << endl << "The number of elements in the stack is " << elementsInStack << ".";
    }

    if (choice == 5)
    {
        cout << endl << endl << "The elements in the stack are as follows:" << endl;

        stack.printElements();
    }

    if (choice == 6)
    {
        return 0;
    }
}

void showMenu()
{
    cout << "1. Push" << endl;
    cout << "2. Pop" << endl;
    cout << "3. isEmpty" << endl;
    cout << "4. numOfElements" << endl;
    cout << "5. printElements" << endl;
    cout << "6. Quit" << endl << endl;
}

List of Errors from the Code

严重级代码描述项目文件行抑制状态 错误LNK2005“public:__thiscall LinkedList :: LinkedList(void)”(?? 0LinkedList @@ QAE @ XZ)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2005“public:__thiscall LinkedList ::〜LinkedList(void)”(?? 1LinkedList @@ QAE @ XZ)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2005“public:int __thiscall LinkedList :: isEmpty(void)”(?isEmpty @ LinkedList @@ QAEHXZ)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2005“public:int __thiscall LinkedList :: numOfElements(void)”(?numOfElements @ LinkedList @@ QAEHXZ)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2005“public:int __thiscall LinkedList :: pop(void)”(?pop @ LinkedList @@ QAEHXZ)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2005“public:void __thiscall LinkedList :: printElements(void)”(?printElements @ LinkedList @@ QAEXXZ)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2005“public:void __thiscall LinkedList :: push(int)”(?push @ LinkedList @@ QAEXH @ Z)已经在LinkedList.obj中定义了Project1 c:\ Users \ Chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Project1 \ main.obj 1

严重级代码描述项目文件行抑制状态 错误LNK1169找到一个或多个多重定义的符号Project1 c:\ users \ chase \ documents \ visual studio 2015 \ Projects \ Project1 \ Debug \ Project1.exe 1

0 个答案:

没有答案