指针编译器问题
我已经三年多没有使用过指针了,而且我对这个话题非常生疏。当我编译以下代码时,我收到了大量错误。错误如下:
ubuntu@ubuntu:~/Desktop/stuff$ g++ test.cpp LinearNode.cpp LinkedList.cpp
LinkedList.cpp: In member function ‘void LinkedList::add(int)’:
LinkedList.cpp:26: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:31: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:39: error: request for member ‘setPrevious’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:40: error: cannot convert ‘LinearNode’ to ‘LinearNode*’ in assignment
LinkedList.cpp: In member function ‘int LinkedList::remove(int)’:
LinkedList.cpp:60: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:62: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:63: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:67: error: invalid conversion from ‘LinearNode*’ to ‘int’
LinkedList.cpp:67: error: initializing argument 1 of ‘LinearNode::LinearNode(int)’
LinkedList.cpp:68: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp: In member function ‘void LinkedList::print()’:
LinkedList.cpp:97: error: invalid conversion from ‘LinearNode*’ to ‘int’
LinkedList.cpp:97: error: initializing argument 1 of ‘LinearNode::LinearNode(int)’
Linked List.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
#include"LinearNode.h"
using namespace std;
class LinearNode;
class LinkedList
{
public:
LinkedList();
void add(int element);
int remove (int element);
void print();
private:
int count;
LinearNode* contents;
};//ends the class linked list
#endif
Linked List:
#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"
using namespace std;
//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
count = 0;
contents = NULL;
}//ends the constructor
//adds an element to the front of the linked list
void LinkedList::add(int element)
{
int found = 0, current = 0;
for (int index = 0; index < count; index++)
{
if (contents.getElement() == element)
found = 1;
else
{
contents = *contents.getNext();
}//ends the else statement
}//ends the while loop
if (found == 0)
{
LinearNode node(element);
node.setNext(contents);
contents.setPrevious(&node);
contents = node;
count++;
//print();
cout << endl;
}//ends the found == 0 if statment
}//ends the add function
//this function removes one element from the linked list.
int LinkedList::remove(int element)
{
int found = 0, result = 0;
LinearNode previous;
LinearNode current;
if (count == 0)
cout << "The list is empty" << endl;
else
{
if (contents.getElement() == element)
{
result = contents.getElement();
contents = *contents.getNext();
}//ends the contents.getElement() == element
else
{
previous = contents;
current = *contents.getNext();
for (int index = 0; ( (index < count) && (found == 0) ); index++)
if (current.getElement() == element)
found = 1;
else
{
previous = current;
current = *current.getNext();
}//ends the else statement
if (found == 0)
cout << "The element is not in the list" << endl;
else
{
result = current.getElement();
previous.setNext(current.getNext());
}//ends else statement
}//ends the else stamtement
count--;
}//ends the else statement of count == 0
return result;
}//ends the remove function
void LinkedList::print()
{
LinearNode current;
current = contents;
for (int index = 0; index < count; index++)
{
cout << current.getElement() << endl;
current = *current.getNext();
}//ends the for loop
}//ends Print function
LinearNode.h:
#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>
using namespace std;
class LinearNode
{
public:
//Constructor for the LinearNode class that takes no arguments
LinearNode();
//Constructor for the LinearNode class that takes the element as an argument
LinearNode(int el);
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();
//sets the next element in the set
void setNext(LinearNode* node);
//sets the previous element in the set
void setPrevious(LinearNode* node);
//sets the element of the node
void setElement(int el);
//gets the element of the node
int getElement();
private:
LinearNode* next;
LinearNode* previous;
int element;
};//ends the LinearNode class
#endif
LinearNode:
#include<iostream>
#include"LinearNode.h"
using namespace std;
//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
next = NULL;
element = 0;
}//ends LinearNode default constructor
//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
next = NULL;
previous = NULL;
element = el;
}//ends LinearNode constructor
//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
return next;
}//ends getNext function
//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
return previous;
}//ends getPrevious function
//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
next = node;
}//ends the setNext function
//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
previous = node;
}//ends the setPrevious function
//returns element of the node
int LinearNode::getElement()
{
return element;
}//ends the getelement function
//sets the element of the node
void LinearNode::setElement(int el)
{
element = el;
}//ends the setElement function
I haven't used pointers in over 3 years and I am very rusty on this topic. I have receive a ton of errors when I compile the following code. The errors are as follows:
ubuntu@ubuntu:~/Desktop/stuff$ g++ test.cpp LinearNode.cpp LinkedList.cpp
LinkedList.cpp: In member function ‘void LinkedList::add(int)’:
LinkedList.cpp:26: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:31: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:39: error: request for member ‘setPrevious’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:40: error: cannot convert ‘LinearNode’ to ‘LinearNode*’ in assignment
LinkedList.cpp: In member function ‘int LinkedList::remove(int)’:
LinkedList.cpp:60: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:62: error: request for member ‘getElement’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:63: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp:67: error: invalid conversion from ‘LinearNode*’ to ‘int’
LinkedList.cpp:67: error: initializing argument 1 of ‘LinearNode::LinearNode(int)’
LinkedList.cpp:68: error: request for member ‘getNext’ in ‘((LinkedList*)this)->LinkedList::contents’, which is of non-class type ‘LinearNode*’
LinkedList.cpp: In member function ‘void LinkedList::print()’:
LinkedList.cpp:97: error: invalid conversion from ‘LinearNode*’ to ‘int’
LinkedList.cpp:97: error: initializing argument 1 of ‘LinearNode::LinearNode(int)’
Linked List.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
#include"LinearNode.h"
using namespace std;
class LinearNode;
class LinkedList
{
public:
LinkedList();
void add(int element);
int remove (int element);
void print();
private:
int count;
LinearNode* contents;
};//ends the class linked list
#endif
Linked List:
#include<iostream>
#include"LinearNode.h"
#include"LinkedList.h"
using namespace std;
//linkedlist constructor for an empty linked list
LinkedList::LinkedList()
{
count = 0;
contents = NULL;
}//ends the constructor
//adds an element to the front of the linked list
void LinkedList::add(int element)
{
int found = 0, current = 0;
for (int index = 0; index < count; index++)
{
if (contents.getElement() == element)
found = 1;
else
{
contents = *contents.getNext();
}//ends the else statement
}//ends the while loop
if (found == 0)
{
LinearNode node(element);
node.setNext(contents);
contents.setPrevious(&node);
contents = node;
count++;
//print();
cout << endl;
}//ends the found == 0 if statment
}//ends the add function
//this function removes one element from the linked list.
int LinkedList::remove(int element)
{
int found = 0, result = 0;
LinearNode previous;
LinearNode current;
if (count == 0)
cout << "The list is empty" << endl;
else
{
if (contents.getElement() == element)
{
result = contents.getElement();
contents = *contents.getNext();
}//ends the contents.getElement() == element
else
{
previous = contents;
current = *contents.getNext();
for (int index = 0; ( (index < count) && (found == 0) ); index++)
if (current.getElement() == element)
found = 1;
else
{
previous = current;
current = *current.getNext();
}//ends the else statement
if (found == 0)
cout << "The element is not in the list" << endl;
else
{
result = current.getElement();
previous.setNext(current.getNext());
}//ends else statement
}//ends the else stamtement
count--;
}//ends the else statement of count == 0
return result;
}//ends the remove function
void LinkedList::print()
{
LinearNode current;
current = contents;
for (int index = 0; index < count; index++)
{
cout << current.getElement() << endl;
current = *current.getNext();
}//ends the for loop
}//ends Print function
LinearNode.h:
#ifndef LINEARNODE_H
#define LINEARNODE_H
#include<iostream>
using namespace std;
class LinearNode
{
public:
//Constructor for the LinearNode class that takes no arguments
LinearNode();
//Constructor for the LinearNode class that takes the element as an argument
LinearNode(int el);
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();
//sets the next element in the set
void setNext(LinearNode* node);
//sets the previous element in the set
void setPrevious(LinearNode* node);
//sets the element of the node
void setElement(int el);
//gets the element of the node
int getElement();
private:
LinearNode* next;
LinearNode* previous;
int element;
};//ends the LinearNode class
#endif
LinearNode:
#include<iostream>
#include"LinearNode.h"
using namespace std;
//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
next = NULL;
element = 0;
}//ends LinearNode default constructor
//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
next = NULL;
previous = NULL;
element = el;
}//ends LinearNode constructor
//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
return next;
}//ends getNext function
//returns previous element in structure
LinearNode* LinearNode::getPrevious()
{
return previous;
}//ends getPrevious function
//sets the next variable for the node
void LinearNode::setNext(LinearNode* node)
{
next = node;
}//ends the setNext function
//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)
{
previous = node;
}//ends the setPrevious function
//returns element of the node
int LinearNode::getElement()
{
return element;
}//ends the getelement function
//sets the element of the node
void LinearNode::setElement(int el)
{
element = el;
}//ends the setElement function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有许多单独的错误,但关于指针的错误是您使用点 (
.
) 来访问指向类的指针的成员。点用于访问类对象的成员(而不是指针)。您应该使用箭头 (->
) 来访问类指针的成员。例如,
应该是
There are a number of separate errors here, but the one about pointers is that you are using the dot (
.
) to access members of a pointer-to-class. The dot is for accessing members of a class object (not a pointer). You should use the arrow (->
) to access members of a pointer-to-class.For example,
should be
由于您使用的是
contents
指针,因此您需要正确引用它才能访问成员。例如,第一个错误是由LinkedList::add
引起的:Since you're using a pointer for
contents
, you need to deference it correctly to access members. For example, the first error is caused byLinkedList::add
:.
的优先级高于*
。所以改成并且
你必须在多个地方纠正它。
如果您需要摆脱所有这些,只需使用 -
.
has a higher precedence over*
. So changeto
And you have to correct it at multiple places.
If you need to get rid of all these, simply use -