gpt4 book ai didi

C++ 错误 C2819 : type 'List' does not have an overloaded member 'operator ->'

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:48 25 4
gpt4 key购买 nike

我一直收到这个错误 -

error C2819: type 'List' does not have an overloaded member 'operator ->'

我不明白为什么?帮助?

Main.cpp -

#include <iostream>
#include <string>
#include <cassert>
using namespace std;

#include "List.h"
#include "Node.h"

错误发生在这里:

void PrintList ( List list ) {
Node * temp = list.getFirst();
Node * temp2 = list->getLast();
while ( temp->getItemName() != temp2->getName() ) {
cout << temp.getItemName() << endl;
}
}

List.h -

#ifndef LIST_H
#define LIST_H

#include "Node.h"
class List
{
private:
Node * First;
Node * Last;
int num_in_list;
public:
List () { num_in_list = 0; First = NULL; Last = NULL; }
int get_num_in_list() { return num_in_list; }
Node * getFirst() { return First; }
Node * getLast() { return Last; }
void del_frnt ();
void push_front (Node *);
void push_back (Node *);
void del_last ();
void add (Node*);
Node * pop_back ();
Node * pop_front ();
int search_item_list (string);
Node * get (int);
};
#endif

List.cpp -

#include <iostream>
#include <string>
#include <cassert>
#include "Node.h"
#include "List.h"
using namespace std;

Node * List:: get ( int position_of_node ) {
assert ( First != NULL);
Node * temp = First;
for (int i = 1; i < position_of_node; i++)
{ temp = temp->getNext(); }
return temp;
}

int List:: search_item_list (string item_searching_for ) {
assert (First != NULL && num_in_list != 0);
int counter = 1;
Node * temp = First;
while ( temp->getItemName() != item_searching_for || temp->getNext() == NULL )
{ temp = temp->getNext(); counter++; }
return counter;
}

void List:: add (Node * node_to_be_added) {
if (num_in_list == 0) { First = node_to_be_added; Last = node_to_be_added; }
else if (num_in_list != 0 ) {
Last->setNext(node_to_be_added);
node_to_be_added->setPrevous(Last);
Last = node_to_be_added;
}
num_in_list++;
}

Node * List :: pop_back ( ) {
assert (Last != NULL);
if ( num_in_list > 1) {
Node * temp = Last;
Last = Last->getPrevous();
Last->setNext(NULL);
temp->setNext(NULL);
temp->setPrevous(NULL);
return temp;
}
else if ( num_in_list == 1 ) {
Node * temp = First;
First = NULL;
return temp;
}
else return NULL;
}

Node * List:: pop_front ( ) {
assert ( First != NULL && num_in_list > 0);
if ( num_in_list > 1 ) {
Node * temp = First;
First = First->getNext();
First->setPrevous(NULL);
temp->setNext(NULL);
temp->setPrevous(NULL);
return temp;
}

else if ( num_in_list == 1) {
Node * temp = First;
First = NULL;
return temp;
}
else return NULL;
}

void List:: del_last ( ) {
assert ( Last != NULL );
if ( num_in_list > 1) {
Node * temp_node = Last->getPrevous();
Node * new_last = Last;
temp_node->setNext(NULL);
delete new_last;
num_in_list--;
}
else if ( num_in_list == 1) {
Node * temp = First;
delete temp;
num_in_list = 0;
First = NULL;
}
}

void List:: del_frnt ( ) {
assert ( First != NULL);
if ( num_in_list > 1) {
Node * saveFirst = First;
First->getNext()->setPrevous(NULL);
First = First->getNext( );
delete saveFirst;
num_in_list--;
}
else if ( num_in_list == 1 ) {
Node * saveFirst = First;
delete saveFirst;
num_in_list = 0;
First = NULL;
}
}

void List:: push_back (Node * new_node) {
assert ( Last != NULL );
Last->setNext(new_node);
new_node->setPrevous(Last);
Last = new_node;
num_in_list++;
}

void List:: push_front (Node * new_node) {
if ( First != NULL) {
First->setPrevous(new_node);
new_node->setPrevous(NULL);
new_node->setNext(First);
First = new_node;
num_in_list++;
}
else if ( First == NULL ) {
First = new_node;
Last = new_node;
num_in_list = 1;
}
}

Node.h -

#ifndef NODE_H
#define NODE_H
#include <string>
using namespace std;

class Node
{
private:
string ItemName;
string Quantity;
Node * Next;
Node * Prevous;
public:
Node () { ItemName = " "; Quantity = " "; }
void setItemName (string a) { ItemName = a; }
string getItemName () { return ItemName; }
void setQuantity (string a) { Quantity = a; }
string getQuantity () { return Quantity; }
void setNext (Node * a) { *Next = *a; }
Node * getNext () { return Next; }
void setPrevous (Node * a) { *Prevous = *a; }
Node * getPrevous () { return Prevous; }
};
#endif

注意:我知道我正在做的只是一个列表,但对于拼贴课我必须这样做:) -任何帮助/指示/如何更好地做事都是太棒了!!!

最佳答案

不能同时做:

Node * temp = list.getFirst();
Node * temp2 = list->getLast();

前者可以工作,因为您按值传递 list,但要使后者工作,您需要重载 operator->。只需使用:

Node * temp2 = list.getLast();

注意:在 C++ 中,访问聚合成员(数据成员或函数)的语法是:

T.member if T is an aggregate type

T->member if T is a pointer to an aggregate type

(好的,我已经简化了一点,但这应该可以帮助您入门。)

关于C++ 错误 C2819 : type 'List' does not have an overloaded member 'operator ->' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1842002/

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