I keep getting this error -
error C2819: type 'List' does not have an overloaded member 'operator ->'
i can't figure out why? help?
Main.cpp -
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
#include "List.h"
#include "Node.h"
The error happens here:
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
Note:I know what I'm doing is just a list, but for a collage class I have to do it :) -Any help / pointers / how to do things better would be GREAT!!!
Can't do both:
Node * temp = list.getFirst();
Node * temp2 = list->getLast();
The former works since you are passing in list
by value, but for the latter to work, you need to have the operator->
overloaded. Just use:
Node * temp2 = list.getLast();
Note: In C++, the syntax for accessing aggregate members (be data members or functions) is:
T.member if T is an aggregate type
and
T->member if T is a pointer to an aggregate type
(Ok, I have simplified a bit, but this ought to help you get started.)
In your PrintList function, you have a single argument of type List:
void PrintList ( List list )
When you try to access it with list.getFirst() everything works fine. When you try to do list->getLast() the compiler generates an error that is telling you that your variable isn't a pointer type. In your short PrintList function, you probably want to replace all your -> with the . operator. If you want to use a ->, you have to do something like:
List * listptr = new List(); listptr->getFirst(); listptr->getLast();
Otherwise, simply replace your second line with: Node * temp2 = list.getLast();
You can create a custom -> operator, but I don't think that's really what you want to be doing.
Also, I didn't look into it too carefully, but is your while loop ever going to terminate? Unless the calls to getItemName are going to change the value of the node, that looks like it could go on forever.
list isn't a pointer in Node * temp2 = list->getLast();
Changing Node * temp2 = list->getLast();
to Node * temp2 = list.getLast();
will make it work, but I don't think it's the best solution. You are passing the list into PrintList()
by value, which will invoke the copy constructor. A better approach would be to pass in a const
reference to the list. You would still need to make the change from ->
to .
though.
Note this
*Node * temp2 = list->getLast();*
list is supposed to be an object, not a pointer, and thus you shouldn't use "->", but rather "."
精彩评论