【问题标题】:Not able to output object in linked list无法在链表中输出对象
【发布时间】:2015-12-13 00:38:41
【问题描述】:

我创建了一个链表,每个链表都包含一个包含 CarPart 对象的节点。我相信我一切正常,除了输出 cout。我收到以下错误

'CarPart::getPartNumber': non-standard syntax; use '&' to create a pointer to member (carpart.cpp line 34)

'CarPart::getDescription': non-standard syntax; use '&' to create a pointer to member (carpart.cpp line 35)

'CarPart::getPrice': non-standard syntax; use '&' to create a pointer to member (carpart.cpp line 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;
}

列表.h

#pragma once
#include "node.h"
class List
{

private:
    int listSize;
    Node* n;
    Node* temp;
    Node* head;
    Node* tail;

public:
    List();
    void push_front(CarPart*);
    void push_back(CarPart*);
    void pop_front();
    void pop_back();
    void display();

    ~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()
{
}

节点.h

#pragma once
#include "CarPart.h"
class Node
{
private:
    CarPart* data;
    Node* next;
    Node* previous;

public:
    Node();
    CarPart* getData();
    void setData(CarPart*);
    void setNext(Node*);
    void setPrevious(Node*);
    Node* getPrevious();
    Node* getNext();
    void display();
    ~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()
{
}

CarPart.h

#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);
};

CarPart.cpp

#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()
{
}

更新

我修复了下面的错误,但它没有输出汽车零件,控制台只显示00820788008207880082078800820788。我认为它只是指针,但不确定我做错了什么。

【问题讨论】:

    标签: c++ linked-list operator-overloading


    【解决方案1】:

    您错误地调用了 get 函数。您使用的是 dt-&gt;getPartNumber; 而不是 dt-&gt;getPartNumber();

    【讨论】:

    • 我更新为 ave (),而不是只输出数字……显然没有深入到 CarPart 项目。输出是 00820788008207880082078800820788 ...我认为它只是指针...但不确定我做错了什么。
    • 我会调查您在 Node 中的显示功能,因为您似乎正在打印“数据”而没有先取消引用它
    【解决方案2】:

    要调用方法/函数,即使没有参数 (dt-&gt;getPartNumber()),您也必须使用方括号。这一行

    os << dt->getPartNumber()
    

    表示“将调用getPartNumber返回的值传递给os”。

    否则,它会将dt-&gt;getPartNumber 解释为指向getPartNumber 函数的指针,这是一个非常不同的野兽。

    注意:对于以后的问题,请尝试仅发布导致错误的最少代码而不是整个程序,并尝试标记引发错误的行。 Google for SSCCE。

    【讨论】:

    • LOL 我曾经发布我认为相关的内容,每个人都会要求提供所有代码......虽然它可能遗漏了我应该包含的内容。
    • 我更新为 ave (),而不是只输出数字……显然没有深入到 CarPart 项目。输出是 00820788008207880082078800820788 ...我认为它只是指针...但不确定我做错了什么。
    • 这个想法是你剥离你的程序的一部分,直到你得到仍然会导致同样错误的最低限度。在这种情况下,没有依赖关系的文件中出现编译错误,唯一需要的文件是CarPart.cpp.h。如果你有依赖,你试着去掉它们,看看是否有错误。
    • 尝试增量调试。做os &lt;&lt; new PartCart("Hello", "World", 99.00);。如果可行,则问题可能与列表处理有关,而不是PartCart。冲洗并重复。很多次。
    • 另外,不要更改问题。您提出的问题已解决,请将其中一个答案标记为正确。将进一步的问题作为新问题发布(在您对它们进行了足够好的研究以编写一个全面的问题之后)。这不是代码编写/调试服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多