- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如何调用 toString()
我的类链接列表中的方法 BoxClass
. BoxClass
有一个 double
length
, width
和 height
.
我的 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/
正在阅读 Underscore.js 以了解它的 is[String|Number|...] 方法是如何工作的,现在我很困惑。下划线: toString.call(obj) == ['object '
scala> Array(1, 2, 3).toString res1: String = [I@11cf437c scala> List(1, 2, 3).toString res2: String
我在将字符串从 stringbuilder 转换为字符串时遇到问题。问题类似于 this issue但略有不同: 这是我的简化代码: StringBuilder sb = new StringBuil
我正在尝试将从正在构建的搜索功能中名为 Part 的模型返回的 int id 转换为字符串,以便简化搜索。 这是我目前使用的 if 语句: if(part.getId().toString().ind
我需要从所选内容中提取文本并将其发送到 TTS 服务。 TTS 服务将返回一个流 URL 和每个单词的一组索引,指示它们的开始和结束位置(时间和文本)。 当用户播放流时,我想在读出每个单词时突出显示它
我想知道人们在 Java 的 toString() 方法中放入了什么。 我一直在向一些新类添加一些内容,并且想知道它是否应该包含类名。 在类ClassConfig中,我无法决定是否应该拥有 @Over
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 下面是我的主要方法,其中比较两个对象引用。覆盖toString()方法
我的问题是,JAVA中没有提供toString()方法的类是否可以打印出特定信息? 问题在于:我们为我们的应用程序提供了一个记录器(使用aspectJ),它打印出给出的特定参数。例如: public
基本上这就是我想要实现的目标。 classname@address(?)[original toString()], object's name, object's age @Override pub
据我所知,Scala 中的中缀运算符的使用应该等同于方法的调用。所以: scala> "a" + 3.toString res0: java.lang.String = a3 是相同的: scala>
这个问题已经有答案了: Why can't I access a property of an integer with a single dot? (5 个回答) 已关闭 7 年前。 functio
我正在进行测试,并且给出了很多单元(隐藏)测试,但是我的一段代码遇到了这个错误。大家能帮帮我吗? getString(comment) { const authorName = comment.get
return toString.call(obj) 和 return obj.toString() 有什么区别? 我通常会找到具有这些不同风格的代码 最佳答案 toString.call(obj) 返
例如,我必须在每个数字到字符串的转换中使用 .ToString(CultureInfo.CurrentCulture)。我能否以某种方式重写 .ToString(),这样我就不会在字符串转换中显式地收
var d = []; console.log(typeof d); // weird! console.log(d.toString()); //Prints nothing since there
当对象字面量调用toString()方法如{}.toString()会导致语法错误,但是当数组字面量调用toString()没关系。当我将对象文字分配给一个变量时,当它调用 toString() 方法
我在打印特殊数组时遇到问题: 我使用 System.out.println(Arrays.toString()); 打印多个对象的数组但现在数组中充满了对象,这些对象具有 char 值,我想打印分配给
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
> ~0..toString(2) -1 > ~1..toString(2) -2 > ~2..toString(2) -11 > ~3..toString(2) -12 > (~1).toStrin
这是我的问题,我的机器使用法语文化,因此默认情况下它以法语方式解析 (3,141592)。 如果机器文化不是美国,这里是重现我的问题的代码: float number = 4103.26808
我是一名优秀的程序员,十分优秀!