簡體   English   中英

C ++模板鏈接列表鏈接器錯誤

[英]C++ Template Linked List Linker Error

我認為我使用模板的方式不正確,但是我無法弄清楚自己在做什么。 就像模板鏈表無法確定它需要使用我的Term類。

theList->插入(tempPolynomial); 是位於function.cpp末尾的代碼行,它導致鏈接器錯誤!

以下是Visual Studio 2012的確切錯誤:

  • 錯誤LNK2019:未解析的外部符號“布爾__cdecl運算符<(類Term,類LinkedList)”(?? M @ YA_NVTerm @@ V?$ LinkedList @ VTerm @@@@@@ Z)在函數“ public:void __thiscall LinkedList中引用: :insert(class Term)“(?insert @?$ LinkedList @ VTerm @@@@ QAEXVTerm @@@@ Z)C:\\ Users \\ Michael \\ Documents \\ Magic Briefcase \\ champlain \\ courseWork \\ dataStructures \\ pa2 \\ pa2 \\ functions。 OBJ

  • 錯誤LNK1120:1個未解決的外部C:\\ Users \\ Michael \\ Documents \\ Magic公文包\\ champlain \\ courseWork \\ dataStructures \\ pa2 \\ Debug \\ pa2.exe

header.h

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

#include "linkedList.h"
#include "term.h"

void loadPolynomial(string expression, LinkedList<Term> *theList);

functions.cpp

#include "header.h"


void loadPolynomial(string expression, LinkedList<Term> *theList)
{
    Term tempPolynomial;

    string varDelimiter = "x";
    string posDelimiter = "+";
    string negDelimiter = "-";
    string token = "";

    double coefficient;
    double exponent;

    bool isNeg;

    while(expression.length() > 0)
    {
        isNeg = false;

        if(expression.substr(0, 1) == "+")
        {
            expression.erase(0, 1);
        }

        else if(expression.substr(0, 1) == "-")
        {
            isNeg = true;
            expression.erase(0, 1);
        }

        //Get the coefficient
        token = expression.substr(0, expression.find(varDelimiter));
        //Remove the coefficient and variable from the string leaving only the exponent
        expression.erase(0, expression.find(varDelimiter) + varDelimiter.length());
        //Convert and put token's coeficient into a double
        coefficient = atof(token.c_str());

        if(isNeg = true)
        {
            coefficient = coefficient * -1;
        }

        //Put the coefficient value into the tempPolynomial
        tempPolynomial.setCoefficient(coefficient);

        //If posDelimiter has a smaller length then it is the beginning of the next expression
        if(expression.find(posDelimiter) < expression.find(negDelimiter))
        {
            //Get the exponent
            token = expression.substr(0, expression.find(posDelimiter));
            //Remove the exponent but leave the + 
            expression.erase(0, expression.find(varDelimiter));
            //Convert and put token's coeficient into a double
            exponent = atof(token.c_str());
        }

        else
        {
            //Get the exponent
            token = expression.substr(0, expression.find(posDelimiter));
            //Remove the exponent but leave the +
            expression.erase(0, expression.find(varDelimiter));
            //Convert and put token's coeficient into a double
            exponent = atof(token.c_str());
        }

        //Put the exponent value into the tempPolynomial
        tempPolynomial.setExponent(exponent);

        //Intert the first term into the linked list
        theList->insert(tempPolynomial);
    }
}

linkedList.h

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#include <iostream>
#include <fstream>
using namespace std;

template <class T>
class LinkedList
{
private:
    T mData;
    LinkedList<T> *mNext;

public:
    LinkedList();
    LinkedList(T data);
    ~LinkedList();

    T getData();
    LinkedList<T> *getNext();

    void setData(T data);

    void display();
    void insert(T data);
    bool isExist(T data);
    void remove(T data);

    friend ostream& operator<<(ostream &output, LinkedList<T> obj);

    bool operator==(T right);
    friend bool operator==(T left, LinkedList<T> right);

    bool operator!=(T right);
    friend bool operator!=(T left, LinkedList<T> right);

    bool operator>(T right);
    friend bool operator>(T left, LinkedList<T> right);

    bool operator<(T right);
    friend bool operator<(T left, LinkedList<T> right);
};



template <class T>
LinkedList<T>::LinkedList()
{
    mNext = NULL;
    mData = T();
}



template <class T>
LinkedList<T>::LinkedList(T data)
{
    mNext = NULL;
    mData = data;
}



template <class T>
LinkedList<T>::~LinkedList()
{
    LinkedList<T> *tempNode;

    tempNode = mNext;

    while(tempNode != NULL)
    {
        mNext = tempNode->mNext;
        tempNode->mNext = NULL;

        delete tempNode;

        tempNode = mNext;
    }
}



template <class T>
T LinkedList<T>::getData()
{
    return mData;
}



template <class T>
LinkedList<T> *LinkedList<T>::getNext()
{
    return mNext;
}



template <class T>
void LinkedList<T>::setData(T data)
{
    mData = data;
}



template <class T>
void LinkedList<T>::display()
{
    LinkedList<T> *tempNode;

    tempNode = mNext;

    while(tempNode != NULL)
    {
        cout << tempNode->mData << endl;

        tempNode = tempNode->mNext;
    }
}



template <class T>
void LinkedList<T>::insert(T data)
{
    LinkedList<T> *previousNode;
    LinkedList<T> *tempNode;
    LinkedList<T> *newNode;

    newNode = new LinkedList(data);

    if(mNext == NULL)
    {
        mNext = newNode;
    }

    else
    {
        previousNode = mNext;
        tempNode = mNext;

        while(tempNode != NULL && tempNode->mData < data)
        {
            previousNode = tempNode;
            tempNode = tempNode->mNext;
        }

        if(tempNode == mNext)
        {
            newNode->mNext = mNext;
            mNext = newNode;
        }

        else
        {
            previousNode->mNext = newNode;
            newNode->mNext = tempNode;
        }
    }
}



template <class T>
bool LinkedList<T>::isExist(T data)
{
    LinkedList<T> *tempNode;
    bool exist = false;

    tempNode = mNext;

    while(tempNode != NULL)
    {
        if(tempNode->mData == data)
        {
            exist = true;

            break;
        }

        tempNode = tempNode->mNext;
    }

    return exist;
}



template <class T>
void LinkedList<T>::remove(T data)
{
    LinkedList<T> *tempNode;
    LinkedList<T> *previousNode;

    if(isExist(data) == false)
    {
        return;
    }

    tempNode = mNext;
    previousNode = mNext;

    while(tempNode->mData != data)
    {
        previousNode = tempNode;
        tempNode = tempNode->mNext;
    }

    if(tempNode == mNext)
    {
        mNext = tempNode->mNext;
        tempNode->mNext = NULL;
    }

    else
    {
        if(tempNode->mNext == NULL)
        {
            previousNode->mNext = NULL;
        }

        else
        {
            previousNode->mNext = tempNode->mNext;
            tempNode->mNext = NULL;
        }
    }

    delete tempNode;
}



template <class T>
ostream& operator<<(ostream &output, LinkedList<T> obj)
{
    output << obj.mData;

    return output;
}



template <class T>
bool LinkedList<T>::operator==(T right)
{
    return mData == right;
}



template <class T>
bool operator==(T left, LinkedList<T> right)
{
    return left == right.mData;
}



template <class T>
bool LinkedList<T>::operator!=(T right)
{
    return mData != right;
}



template <class T>
bool operator!=(T left, LinkedList<T> right)
{
    return left != right.mData;
}



template <class T>
bool LinkedList<T>::operator>(T right)
{
    return mData > right;
}



template <class T>
bool operator>(T left, LinkedList<T> right)
{
    return left > right.mData;
}



template <class T>
bool LinkedList<T>::operator<(T right)
{
    return mData < right;
}



template <class T>
bool operator<(T left, LinkedList<T> right)
{
    return left < right.mData;
}

#endif

term.h

#ifndef TERM_H
#define TERM_H

class Term
{
private:
    double mCoefficient;
    double mExponent; 

public:
    Term();
    Term(double coefficient, double exponent);
    ~Term();

    double getCoefficient();
    double getExponent();
    void setCoefficient(double coefficient);
    void setExponent(double exponent);
};

#endif

您的學期班需要一個較小的比較器。 這些都可以做:

在術語類成員中:

bool operator <(const Term&) const; 

或免費的運算符功能:

bool operator <(const Term& left, const Term& right);

為什么? 因為LinkedList<T>::insert(T val)調用以下內容:

while(tempNode != NULL && tempNode->mData < data)

模板擴展后, tempNode->mDatadata都為Term類型。 但是沒有operator < (成員函數或自由函數)將兩個Term對象比較為“較小”。

我不確定您要如何對它們進行排序(我可能會先按指數對它們進行排序,然后指數的系數相同)。 我讓您自己決定,但是無論如何您都需要操作員。

示例 (在Term類中作為成員)

bool operator <(const Term& rhs) const
{
    return (mCoefficient < rhs.mCoefficient ||
           (!(rhs.mCoefficient < mCoefficient) == && mExponent < rhs.mExponent));
}

示例II (免費運營商;不是Term的成員)

bool operator <(const Term& lhs, const Term& rhs)
{
    return (lhs.getCoefficient() < rhs.getCoefficient() ||
           (!(rhs.getCoefficient() < lhs.getCoefficient()) == && 
             lhs.getExponent() < rhs.getExponent()));
}

注意:如果對Term友好,則可以直接訪問成員,而無需通過其getter函數(順便說一句,應將其聲明為const因為它們不對要調用其的Term對象進行修改)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM