gpt4 book ai didi

c++ - 类的链接列表,如何在遍历时调用toString?

转载 作者:行者123 更新时间:2023-11-30 02:34:53 25 4
gpt4 key购买 nike

我想知道如何调用 toString()我的类链接列表中的方法 BoxClass . BoxClass有一个 double length , widthheight .

我的 BoxClass:

class BoxClass{
private:
double length;
double width;
double height;

public:
// Default constructor w/ no parameters
BoxClass(){
length = 0;
width = 0;
height = 0;
}

// Constructor with arguments
BoxClass(double boxLength, double boxWidth, double boxHeight){
length = boxLength;
width = boxWidth;
height = boxHeight;
}

// Setters and Getters
void setLength(double boxLength){
length = boxLength;
}
double getLength(){
return length;
}

void setWidth(double boxWidth){
width = boxWidth;
}
double getWidth(){
return width;
}

void setHeight(double boxHeight){
height = boxHeight;
}
double getHeight(){
return height;
}

// Returns the volume of the boxes
double Volume(){
return (length * width * height);
}

// toString method for boxes, returns "(length) x (width) x (height) string
string toString(){
return ("(" + to_string(length)+ "x" + to_string(width) + "x" + to_string(height) + ")");
}
}; // End of BoxClass() class

LinkNode.h

//Template ListNode class definition.
#ifndef LINKNODE_H
#define LINKNODE_H

template <typename T> class LinkList;

template <typename T> class LinkNode{
friend class LinkNode <T>;
public:
LinkNode(const T &);
T getData()const;
T data;
LinkNode <T> *nextPtr;
};

template <typename T> LinkNode <T>::LinkNode(const T &info):data(info), nextPtr(NULL){
// Empty body
}

template <typename T>T LinkNode<T>::getData()const{
return data;
}

#endif

Main(创建类,将其添加到链接列表

//  Create the Box class
BoxClass userBox(length, width, height);

// Add box class to Link List
Box.insertNode(userBox);

Box.print();

LinkList.h print() 方法

template<typename T>void LinkList<T>::print()const {
// To list off nodes
int counter = 1;

if (isEmpty()) {
cout << "No boxes in list!\n";
} else {
LinkNode<T>*currentPtr = firstPtr;

cout << "Your boxes in increasing order of volume is:\n";
// while (currentPtr) {
while (currentPtr != NULL) {

// Output as "#. (length x width x height)
cout << counter << ". " << currentPtr->data << endl;
printf(" %i. %.2f\n", counter, currentPtr->data);
currentPtr = currentPtr->nextPtr;
counter++;
}
}
}

LinkList.h

//Template LinkList class definition.
#ifndef LINKLIST_H
#define LINKLIST_H
#include <iostream>
#include "LinkNode.h"
using namespace std;

template<typename T> class LinkList {
public:
LinkList();
void addNode(const T &);
void insertNode(const T &);
bool isEmpty() const;
void print() const;

private:
LinkNode<T>*firstPtr;
LinkNode<T>*getNewNode(const T &);
};

template<typename T>LinkList<T>::LinkList() :firstPtr(NULL) {
// Empty body
}

template <typename T>void LinkList<T>::insertNode(const T &value) {
LinkNode<T>*newPtr = getNewNode(value);
bool inserted = false;

if (isEmpty() || (newPtr->data < firstPtr->data)) {
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
// cout << " " << newPtr->data << " inserted at front of list.\n";
printf(" %.2f inserted at front of list.\n", newPtr->data);
} else {
LinkNode<T>*currentPtr = firstPtr;
while (currentPtr->nextPtr && !inserted) {
if (newPtr->data < currentPtr->nextPtr->data) {
// cout << " " << newPtr->data << " inserted before " << currentPtr->nextPtr->data << ". " << endl;
printf(" %.2f inserted before %.2f.\n", newPtr->data, currentPtr->nextPtr->data);
newPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr = newPtr;
inserted = true;
} else {
currentPtr = currentPtr->nextPtr;
}
}
if (!inserted) {
currentPtr->nextPtr = newPtr;
printf(" %.2f inserted at the end of list.\n", newPtr->data);
}
}
}

template<typename T>bool LinkList<T>::isEmpty()const {
return firstPtr == NULL;
}

template<typename T>LinkNode<T>*LinkList<T>::getNewNode(const T &value) {
return new LinkNode<T>(value);
}

template<typename T>void LinkList<T>::print()const {
// To list off nodes
int counter = 1;

if (isEmpty()) {
cout << "No boxes in list!\n";
} else {
LinkNode<T>*currentPtr = firstPtr;

cout << "Your boxes in increasing order of volume is:\n";
// while (currentPtr) {
while (currentPtr != NULL) {

// Output as "#. (length x width x height)
cout << counter << ". " << currentPtr->data << endl;
printf(" %i. %.2f\n", counter, currentPtr->data);
currentPtr = currentPtr->nextPtr;
counter++;
}
}
}

#endif

再一次,我的问题是 - 如何遍历列表并调用 toString()盒子类方法?我尝试了 cout << data.toString() << endl; 中的所有内容但这是行不通的。我已经坚持了好几天了,有人可以帮助我吗?

编辑:添加 LinkList.h

最佳答案

当你写 template <typename T> class LinkNode{您明确指出您的节点类将没有关于它包含的节点类型的内置知识。

您没有向我们展示您的 LinkList<T>类,但显然,同样的事情也适用于它:因为它由 LinkNode<T> 组成它还必须接受类型为 <T> 的通用参数, 所以它不能内置关于 <T> 实际类型的知识要么。

因此,您不能突然引入具有此类知识的方法。它没有任何意义。 “它不计算”。

您需要做的是添加此 print()你的方法 elsewhere ,并让它接受 LinkList<BoxClass> .然后,它将能够查看LinkNode。作为 LinkNode<BoxClass> , 它将能够调用 linkNode.data.toString() .

关于c++ - 类的链接列表,如何在遍历时调用toString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34234990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com