使用模板链接列表和通过指针传递的C ++未解析外部符号

时间:2013-09-16 05:42:13

标签: c++ templates linked-list lnk2019 unresolved-external

请阅读以下评论

以下是具体错误:

  • 错误2错误LNK2019:未解析的外部符号“void __cdecl totalPolynomial(class LinkedList *,class LinkedList *,class LinkedList *)”(?totalPolynomial @@ YAXPAV?$ LinkedList @ VTerm @@@@ 00 @ Z )在函数_main

  • 中引用
  • 警告1警告C4805:'==':'unsigned int'类型的不安全混合,并在操作38中输入'bool'

我认为问题存在于functions.cpp / .h中的sumPolynomial中。特别是它的声明/实现。

header.h

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

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

void loadPolynomial(string expression, LinkedList<Term> *theList);
void totalPolynomial(LinkedList<Term> *x, LinkedList<Term> *y, LinkedList<Term> *sum);

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;*/
    bool removedCoefficient = false;

    while(expression.length() > 0)
    {
        /*isNeg = false;*/
        coefficient = 1.0;
        exponent = 1.0;
        removedCoefficient = false;

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

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

        if( expression.length() > 1 && expression.find(varDelimiter) == true)
        {
            //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);
            removedCoefficient = true;
        }

        if(expression.find(posDelimiter) > 0 && expression.length() > 0 && removedCoefficient == true)
        {
            //Get the exponent
            token = expression.substr(0, expression.find(posDelimiter));
            //Remove the exponent but leave the + 
            expression.erase(0, expression.find(posDelimiter) + posDelimiter.length());
            //Convert and put token's coeficient into a double
            exponent = atof(token.c_str());

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

        if(removedCoefficient == false)
        {
            //Get the trailing number
            token = expression.substr(0);
            coefficient = atof(token.c_str());
            //Set it as the coefficient
            tempPolynomial.setCoefficient(coefficient);
            //Set the exponent as 0 so we know it doesn't include a variable
            tempPolynomial.setExponent(0.0);
            //Remove it from the string
            expression.erase(0, token.length());
        }

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



void sumPolynomial(LinkedList<Term> *x, LinkedList<Term> *y, LinkedList<Term> *sum)
{
    LinkedList<Term> *tempNodeX;
    LinkedList<Term> *tempNodeY;
    Term tempTermX;
    Term tempTermY;
    Term newTerm;

    tempNodeX = x->getNext();
    tempNodeY = y->getNext();


    while(tempNodeX != NULL)
    {
        tempTermX = tempNodeX->getData();

        while(tempNodeY !=NULL)
        {
            tempTermY = tempNodeY->getData();

            if(tempTermX.getExponent() == tempTermY.getExponent())
            {
                newTerm.setCoefficient(tempTermX.getCoefficient() + tempTermY.getCoefficient());
                newTerm.setExponent(tempTermY.getExponent());

                sum->insert(newTerm);
            }

            tempNodeX = tempNodeY->getNext();
        }

        tempNodeX = tempNodeX->getNext();
    }
}

term.h

#ifndef TERM_H
#define TERM_H

#include<iostream>
using namespace std;

class Term
{
private:
    double mCoefficient;
    double mExponent; 

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

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

    friend ostream& operator<<(ostream &output, Term object);

    bool operator==(Term right);
    //friend bool operator==(Term left, Term right);
    bool operator!=(Term right);
    //friend bool operator!=(Term left, Term right);
    bool operator>(Term right);
    //friend bool operator>(Term left, Term right);
    bool operator<(Term right);
    //friend bool operator<(Term left, Term right);
};

#endif

term.cpp

#include "term.h"

Term::Term()
{
    mCoefficient = 1;
    mExponent = 1;
}


//
//Term::Term(double coefficient)
//{
//  mCoefficient = coefficient;
//  mExponent = 1.0;
//}
//
//
//Term::Term(double exponent)
//{
//  mCoefficient = 1.0;
//  mExponent = exponent;
//}



Term::Term(double coefficient, double exponent)
{
    mCoefficient = coefficient;
    mExponent = exponent;
}



Term::~Term(){}



double Term::getCoefficient()
{
    return mCoefficient;
}



double Term::getExponent()
{
    return mExponent;
}



void Term::setCoefficient(double coefficient)
{
    mCoefficient = coefficient;
}



void Term::setExponent(double exponent)
{
    mExponent = exponent;
}



ostream& operator<<(ostream &output, Term object)
{
    if(object.mExponent > 0)
    {
        output << object.mCoefficient << "x" << object.mExponent;
    }

    else
    {
        output << object.mCoefficient;
    }
    return output;
}


bool Term::operator==(Term right)
{
    if(mExponent == right.mExponent && mCoefficient == right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }
}



bool Term::operator!=(Term right)
{
    if(mExponent != right.mExponent)
    {
        return true;
    }

    else if(mCoefficient != right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }
}


bool Term::operator>(Term right)
{
    if(mExponent > right.mExponent)
    {
        return true;
    }

    else if(mExponent == right.mExponent && mCoefficient > right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }

}



bool Term::operator<(Term right)
{
    if(mExponent < right.mExponent)
    {
        return true;
    }

    else if(mExponent == right.mExponent && mCoefficient < right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }
}

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> object);

    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> object)
{
    output << object.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

1 个答案:

答案 0 :(得分:0)

以下是现在有效的修订循环:

    while(tempNodeX != NULL)
    {
        tempTermX = tempNodeX->getData();

        while(tempNodeY != NULL)
        {
            tempTermY = tempNodeY->getData();

            if(tempTermX.getExponent() == tempTermY.getExponent())
            {
                newTerm.setCoefficient(tempTermX.getCoefficient() + tempTermY.getCoefficient());
                newTerm.setExponent(tempTermY.getExponent());

                //cout << tempTermX << " " << tempTermY << " " << newTerm << endl;
                sum->insert(newTerm);

                tempNodeY = y->getNext();
                tempNodeY->remove(tempTermY);
                break;
            }

            tempNodeY = tempNodeY->getNext();
        }

        tempNodeX = tempNodeX->getNext();
        tempNodeY = y->getNext();
    }