无法在链表中输出对象

时间:2015-12-13 00:38:41

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

我创建了一个链表,每个列表中都包含一个包含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;
}

List.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();
};

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.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();
};

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.我认为它只是指针,但不确定我做错了什么。

2 个答案:

答案 0 :(得分:1)

您正在错误地调用get函数。您使用的是 dt->getPartNumber; ,而不是 dt->getPartNumber();

答案 1 :(得分:0)

要调用方法/函数,即使没有参数(dt->getPartNumber()),也总是放括号。这一行

os << dt->getPartNumber()

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

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

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