我认为我的模板使用不当,但我不知道我做错了什么。这就像模板链表无法确定它需要使用我的术语类一样。
名单-
以下是Visual Studio 2012的确切错误:
>
错误LNK1120:1未解决的外部C:\用户\迈克尔\文档\魔术公文包\尚普兰\课程工作\数据结构\pa2\调试\pa2.exe
标题。H
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
#include "linkedList.h"
#include "term.h"
void loadPolynomial(string expression, LinkedList<Term> *theList);
功能。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);
}
}
链接列表。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
术语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
Term类需要一个较小的比较器。这两种方法都可以:
在作为成员的术语类中:
bool operator <(const Term&) const;
或自由运算符函数:
bool operator <(const Term& left, const Term& right);
为什么啊?因为LinkedList
while(tempNode != NULL && tempNode->mData < data)
都
tempNode-
我不确定你们想如何对它们进行排序(我可能会先按指数排序,然后指数的系数是相同的)。我让你来决定,但你无论如何都需要接线员。
示例(在
术语
类中作为成员)
bool operator <(const Term& rhs) const
{
return (mCoefficient < rhs.mCoefficient ||
(!(rhs.mCoefficient < mCoefficient) == && mExponent < rhs.mExponent));
}
示例二(自由操作员;不是
术语的成员)
bool operator <(const Term& lhs, const Term& rhs)
{
return (lhs.getCoefficient() < rhs.getCoefficient() ||
(!(rhs.getCoefficient() < lhs.getCoefficient()) == &&
lhs.getExponent() < rhs.getExponent()));
}
注意:如果这是好友到
术语
,你可以直接访问成员,而不是通过他们的getter函数(顺便说一下,应该声明const
,因为他们不修改术语
对象他们被称为)。
我对C模板非常陌生。我目前正在做一个项目,我需要使用模板实现双重链接列表。以下是我目前所拥有的: 然而,在我的析构函数中,为什么我不能访问节点元素?该方法中的代码现在已编译,但不会抛出错误。但是如果我尝试使用- 此外,我如何在函数头中初始化list==NULL,而不是在类之外进行初始化?
英文原文: http://emberjs.com/guides/templates/links/ 链接 ({{link-to}}助手) 你可以使用如下的方式创建一个指向一个路由的链接: 1 2 3 4 5 App.Router.map(function() { this.resource("photos", function(){ this.route("edit", { path:
我正在练习在链表类的实现中使用节点结构实现一个模板类链表。在createNode()成员函数中,当我初始化指向节点结构的指针变量时,会出现编译器错误:“未初始化的局部变量'newNode'已使用” 如果我将初始化更改为: 它工作得很好。如果我可以初始化基本数据类型,比如int,我有点困惑为什么这很重要: 为什么我不能对structs做同样的操作??我的代码如下:
我是c语言新手,我有以下问题: 文件:-main。cpp-实用程序。h-实用程序.cpp 当我在做: g-c-std=c 11实用程序。cpp(编译)g-c-std=c11main。cpp(编译) 当我尝试链接时: g -o main.o utils.o /usr/lib/gcc/i686 redhat-linux/4.8.3/../../../crt1.o:在函数main'utils的引用。o:
为什么这段代码会给我一个链接器错误以及如何修复它? 体系结构x86_64的未定义符号:" operator==(foo const
我跟随YouTube教程用C构建FPS游戏,但遇到了一个无法解决的错误;链接器错误LNK2019: 错误1错误LNK2019:未解析的外部符号"public:__thiscallVector3d::~Vector3d(val)"(??1Vector3d@@QAE@XZ)引用在函数"class std::basic_ostream 我需要做什么来解决这个问题? 这是我在Vector3d中声明的类。h