C++ 无法输出链接列表中的对象
我创建了一个链表,在每个链表中都包含一个节点,其中包含一个CarPart对象。我相信我的一切都正常,除了输出cout。我得到以下错误C++ 无法输出链接列表中的对象,c++,linked-list,operator-overloading,C++,Linked List,Operator Overloading,我创建了一个链表,在每个链表中都包含一个节点,其中包含一个CarPart对象。我相信我的一切都正常,除了输出cout。我得到以下错误 “CarPart::getPartNumber”:非标准语法;使用“&”创建指向成员的指针(carpart.cpp第34行) “CarPart::getDescription”:非标准语法;使用“&”创建指向成员的指针(carpart.cpp第35行) “CarPart::getPrice”:非标准语法;使用“&”创建指向成员的指针(carpart.cpp第36行
“CarPart::getPartNumber”:非标准语法;使用“&”创建指向成员的指针(carpart.cpp第34行)
“CarPart::getDescription”:非标准语法;使用“&”创建指向成员的指针(carpart.cpp第35行)
“CarPart::getPrice”:非标准语法;使用“&”创建指向成员的指针(carpart.cpp第36行)
我曾尝试更换osstream运营商,但未能解决问题
Main.cpp
#include "stdafx.h"
#include "List.h"
int main()
{
List partsList;
partsList.push_front(new CarPart("FL2016", "Oil Filter", 18.95));
partsList.push_front(new CarPart("RS12YC", "Spark Plug", 4.15));
partsList.push_front(new CarPart("D5941", "Digital Tire Guage", 12.15));
partsList.push_back(new CarPart("G19216", "Car Wash Solution", 8.15));
partsList.display();
cout << "now we are going to remove the first item in the list" << endl;
system("PAUSE");
partsList.pop_front();
partsList.display();
system("PAUSE");
cout << "now we are going to remove the LAST item from the list" << endl;
partsList.pop_back();
partsList.display();
system("PAUSE");
return 0;
}
List.cpp
#include "stdafx.h"
#include "List.h"
List::List()
{
}
void List::push_front(CarPart* dat)
{
if (listSize == 0) {
n = new Node;
n->setData(dat);
listSize++;
temp = n;
head = n;
tail = n;
}
else {
n = new Node;
n->setData(dat);
listSize++;
temp = head;
head = n;
n->setNext(temp);
n->setPrevious(nullptr);
temp->setPrevious(n);
temp = n;
}
}
void List::push_back(CarPart* dat)
{
if (listSize == 0) {
n = new Node;
n->setData(dat);
listSize++;
temp = n;
head = n;
tail = n;
}
else {
n = new Node;
n->setData(dat);
listSize++;
temp = tail;
temp->setNext(n);
n->setPrevious(temp);
// SET NEXT TO NULL
temp = n;
tail = temp;
}
}
void List::pop_front()
{
temp = head->getNext();
delete head;
head = temp;
listSize--;
}
void List::pop_back()
{
temp = tail->getPrevious();
delete tail;
tail = temp;
tail->setNext(nullptr);
listSize--;
}
void List::display()
{
Node* test = head;
for (int i = 0; i < listSize; i++) {
cout << test;
}
}
List::~List()
{
}
Node.cpp
#include "stdafx.h"
#include "Node.h"
Node::Node()
{
}
CarPart* Node::getData()
{
return data;
}
void Node::setData(CarPart* dat)
{
data = dat;
}
void Node::setNext(Node* nextNode)
{
next = nextNode;
}
void Node::setPrevious(Node* prev)
{
previous = prev;
}
Node * Node::getPrevious()
{
return previous;
}
Node * Node::getNext()
{
return next;
}
void Node::display()
{
cout << data;
}
Node::~Node()
{
}
#包括“stdafx.h”
#包括“Node.h”
Node::Node()
{
}
CarPart*节点::getData()
{
返回数据;
}
void节点::setData(CarPart*dat)
{
数据=dat;
}
void节点::setNext(节点*nextNode)
{
next=nextNode;
}
void节点::setPrevious(节点*prev)
{
上一个=上一个;
}
Node*Node::getPrevious()
{
返回上一个;
}
Node*Node::getNext()
{
下一步返回;
}
void节点::display()
{
可能您调用的get函数不正确。您使用的是dt->getPartNumber;
而不是dt->getPartNumber();
来调用方法/函数,即使没有参数(dt->getPartNumber()
),您也总是使用括号。这一行
os << dt->getPartNumber()
os getPartNumber()
表示“将调用getPartNumber
返回的值传递给操作系统”
否则,它将dt->getPartNumber
解释为指向getPartNumber
函数的指针,这是一个非常不同的beast
注意:对于以后的问题,试着只发布导致错误的最小代码,而不是整个程序,并尝试标记出现错误的行。Google For SSCCE。LOL我曾经发布我认为相关的内容,每个人都会要求提供所有代码……尽管它可能遗漏了我应该包含的内容。我更新到ave(),而不仅仅是输出数字…显然不是一直到CarPart项。输出是008207880808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808是一个没有依赖项的文件中的编译错误,唯一需要的文件是CarPart.cpp
和.h
。如果您有依赖项,请尝试剥离它们并查看是否有错误。尝试增量调试。也要执行os,不要更改问题。您提出的问题已解决,请将其中一个答案标记为co更正。将进一步的问题作为新问题发布(在您充分研究并编写一个全面的问题之后)。这不是代码编写/调试服务。我更新为ave(),而不是它只是输出数字…显然不是一直到CarPart项。输出是00820788080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808
#include "stdafx.h"
#include "Node.h"
Node::Node()
{
}
CarPart* Node::getData()
{
return data;
}
void Node::setData(CarPart* dat)
{
data = dat;
}
void Node::setNext(Node* nextNode)
{
next = nextNode;
}
void Node::setPrevious(Node* prev)
{
previous = prev;
}
Node * Node::getPrevious()
{
return previous;
}
Node * Node::getNext()
{
return next;
}
void Node::display()
{
cout << data;
}
Node::~Node()
{
}
#pragma once
#include <iostream>
using namespace std;
class CarPart
{
private:
string partNumber;
string description;
double price;
public:
CarPart();
CarPart(string, string, double);
string getPartNumber();
string getDescription();
double getPrice();
~CarPart();
friend ostream& operator<<(ostream& os, CarPart* dt);
};
#include "stdafx.h"
#include "CarPart.h"
CarPart::CarPart()
{
}
CarPart::CarPart(string n, string d, double p)
{
partNumber = n;
description = d;
price = p;
}
string CarPart::getPartNumber()
{
return partNumber;
}
string CarPart::getDescription()
{
return description;
}
double CarPart::getPrice()
{
return price;
}
ostream& operator<<(ostream& os, CarPart* dt)
{
os << dt->getPartNumber;
os << dt->getDescription;
os << dt->getPrice;
return os;
}
CarPart::~CarPart()
{
}
os << dt->getPartNumber()