C++ 我需要帮助重载运算符==,<<&燃气轮机&燃气轮机;使用树木
到目前为止,我已经尝试了很多东西,但都没有用。我似乎无法正确获取任何重载运算符语法或访问权限。有人能告诉我如何正确使用这些重载运算符吗? 头文件C++ 我需要帮助重载运算符==,<<&燃气轮机&燃气轮机;使用树木,c++,operators,overloading,C++,Operators,Overloading,到目前为止,我已经尝试了很多东西,但都没有用。我似乎无法正确获取任何重载运算符语法或访问权限。有人能告诉我如何正确使用这些重载运算符吗? 头文件 #include <iostream> #include <fstream> #include <string> using namespace std; #ifndef BINARY_SEARCH_TREE #define BINARY_SEARCH_TREE template <typename Da
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#ifndef BINARY_SEARCH_TREE
#define BINARY_SEARCH_TREE
template <typename DataType>
class BST
{
public:
/***** Function Members *****/
BST();
/*------------------------------------------------------------------------
Construct a BST object.
Precondition: None.
Postcondition: An empty BST has been constructed.
-----------------------------------------------------------------------*/
bool empty() const;
/*------------------------------------------------------------------------
Check if BST is empty.
Precondition: None.
Postcondition: Returns true if BST is empty and false otherwise.
-----------------------------------------------------------------------*/
bool search(const DataType & item) const;
/*------------------------------------------------------------------------
Search the BST for item.
Precondition: None.
Postcondition: Returns true if item found, and false otherwise.
-----------------------------------------------------------------------*/
void insert(const DataType & item);
/*------------------------------------------------------------------------
Insert item into BST.
Precondition: None.
Postcondition: BST has been modified with item inserted at proper
position to maintain BST property.
------------------------------------------------------------------------*/
void remove(const DataType & item);
/*------------------------------------------------------------------------
Remove item from BST.
Precondition: None.
Postcondition: BST has been modified with item removed (if present);
BST property is maintained.
Note: remove uses auxiliary function search2() to locate the node
containing item and its parent.
------------------------------------------------------------------------*/
void inorder(ostream & out) const;
/*------------------------------------------------------------------------
Inorder traversal of BST.
Precondition: ostream out is open.
Postcondition: BST has been inorder traversed and values in nodes
have been output to out.
Note: inorder uses private auxiliary function inorderAux().
------------------------------------------------------------------------*/
//OVER LOADED OPERATORS.
bool operator==(const BST & right)const;
//Friend functions.
friend std::ostream & operator <<(std::ostream & outs, const BST & BinNode) {outs << BinNode.Left()<< " " << BinNode.right();
return outs;};
friend std::istream & operator >>(std::istream& ins, BST & target) {ins << target.left << " " << target.right;
return ins;};
//Insertion of the file using a text tile.
void readFile();
private:
/***** Node class *****/
class BinNode
{
public:
DataType data;
BinNode * left;
BinNode * right;
// BinNode constructors
// Default -- data part is default DataType value; both links are null.
BinNode()
{
left = 0;
right = 0;}
// Explicit Value -- data part contains item; both links are null.
BinNode(DataType item)
{
data = item;
left = 0;
right = 0;
}
};// end of class BinNode declaration
typedef BinNode * BinNodePointer;
/***** Private Function Members *****/
void search2(const DataType & item, bool & found,
BinNodePointer & locptr, BinNodePointer & parent) const;
/*------------------------------------------------------------------------
Locate a node containing item and its parent.
Precondition: None.
Postcondition: locptr points to node containing item or is null if
not found, and parent points to its parent.#include <iostream>
------------------------------------------------------------------------*/
void inorderAux(ostream & out,
BinNodePointer subtreePtr) const;
/*------------------------------------------------------------------------
Inorder traversal auxiliary function.
Precondition: ostream out is open; subtreePtr points to a subtree
of this BST.
Postcondition: Subtree with root pointed to by subtreePtr has been
output to out.
------------------------------------------------------------------------*/
/***** Data Members *****/
BinNodePointer myRoot;
}; // end of class template declaration
//--- Definition of constructor
template <typename DataType>
inline BST<DataType>::BST()
{myRoot = 0;}
//--- Definition of empty()
template <typename DataType>
inline bool BST<DataType>::empty() const
{ return myRoot == 0; }
//--- Definition of search()
template <typename DataType>
bool BST<DataType>::search(const DataType & item) const
{
BinNodePointer locptr = myRoot;
bool found = false;
while (!found && locptr != 0)
{
if (item < locptr->data) // descend left
locptr = locptr->left;
else if (locptr->data < item) // descend right
locptr = locptr->right;
else // item found
found = true;
}
return found;
}
//--- Definition of insert()
template <typename DataType>
inline void BST<DataType>::insert(const DataType & item)
{
BinNodePointer
locptr = myRoot, // search pointer
parent = 0; // pointer to parent of current node
bool found = false; // indicates if item already in BST
while (!found && locptr != 0)
{
parent = locptr;
if (item < locptr->data) // descend left
locptr = locptr->left;
else if (locptr->data < item) // descend right
locptr = locptr->right;
else // item found
found = true;
}
if (!found)
{ // construct node containing item
locptr = new BinNode(item);
if (parent == 0) // empty tree
myRoot = locptr;
else if (item < parent->data ) // insert to left of parent
parent->left = locptr;
else // insert to right of parent
parent->right = locptr;
}
else
cout << "Item already in the tree\n";
}
//--- Definition of remove()
template <typename DataType>
void BST<DataType>::remove(const DataType & item)
{
bool found; // signals if item is found
BinNodePointer
x, // points to node to be deleted
parent; // " " parent of x and xSucc
search2(item, found, x, parent);
if (!found)
{
cout << "Item not in the BST\n";
return;
}
//else
if (x->left != 0 && x->right != 0)
{ // node has 2 children
// Find x's inorder successor and its parent
BinNodePointer xSucc = x->right;
parent = x;
while (xSucc->left != 0) // descend left
{
parent = xSucc;
xSucc = xSucc->left;
}
// Move contents of xSucc to x and change x
// to point to successor, which will be removed.
x->data = xSucc->data;
x = xSucc;
} // end if node has 2 children
// Now proceed with case where node has 0 or 2 child
BinNodePointer
subtree = x->left; // pointer to a subtree of x
if (subtree == 0)
subtree = x->right;
if (parent == 0) // root being removed
myRoot = subtree;
else if (parent->left == x) // left child of parent
parent->left = subtree;
else // right child of parent
parent->right = subtree;
delete x;
}
//--- Definition of inorder()
template <typename DataType>
inline void BST<DataType>::inorder(ostream & out) const
{
inorderAux(out, myRoot);
}
//--- Definition of search2()
template <typename DataType>
void BST<DataType>::search2(const DataType & item, bool & found,
BinNodePointer & locptr,
BinNodePointer & parent) const
{
locptr = myRoot;
parent = 0;
found = false;
while (!found && locptr != 0)
{
if (item < locptr->data) // descend left
{
parent = locptr;
locptr = locptr->left;
}
else if (locptr->data < item) // descend right
{
parent = locptr;
locptr = locptr->right;
}
else // item found
found = true;
}
}
//--- Definition of inorderAux()
template <typename DataType>
void BST<DataType>::inorderAux(ostream & out,
BinNodePointer subtreeRoot) const
{
if (subtreeRoot != 0)
{
inorderAux(out, subtreeRoot->left); // L operation
out << subtreeRoot->data << " "; // V operation
inorderAux(out, subtreeRoot->right); // R operation
}
}
//---Overloading the Operator double equals.
template <typename DataType>
bool BST<DataType>::operator ==(const BST& right) const
{
//Postcondition: The value returned is true if p1 and p2
// are identical; otherwise false returned.
return (BinNodePointer.right == BinNodePointer.right) && (BinNodePointer.left == BinNodePointer.left);
}
//tried to put all operations here to see a clean main with just function calls.
template<typename DataType>
void BST<DataType>::readFile()
{
BST<string> start;
string data,motor;
ifstream infile;
infile.open("Tree.txt");
if (infile.fail( ))
{
cout << "Input infile opening failed.\n";
exit(1);
}
getline(infile, data);
while (! infile.eof( ))
{
start.insert(data);
cout << data <<endl;
getline(infile, data);
}
cout<< "\n\nStarting a binary search tree.\n"
<< "\nEnter the ID & Password you wish to compare: ";
/* if(start.operator==(motor))
cout << "They are equal.";
else
cout <<"they are not equal.";
*/
//cout << start.inorder(data);
}
#endif
利用
friend std::ostream & operator <<(std::ostream & outs, const BST & BinNode) {outs << BinNode.Left<< " " << BinNode.right;
return outs;};
friend std::ostream&operator这里有一个看起来不对劲的地方:
template <typename DataType>
bool BST<DataType>::operator ==(const BST& right) const
{
//Postcondition: The value returned is true if p1 and p2
// are identical; otherwise false returned.
return (BinNodePointer.right == BinNodePointer.right) &&
(BinNodePointer.left == BinNodePointer.left);
}
您试图直接从类型名中获取字段值,这(我认为)在大多数(每个?)上下文中都是错误的
这将有助于了解您看到的错误类型以及它们出现在哪一行
编辑:
BinNodePointer是一种类型。它不包含或指向任何数据,只是数据的“形状”
它描述了指向BinNode的指针。因此,您的代码与此等效:
return ((BinNode *).right == (BinNode *).right) &&
((BinNode *).left == (BinNode *).left);
模板
boolbst::operator==(常量BST&右侧)常量
{
//后置条件:如果p1和p2为真,则返回的值为真
//相同;否则返回false。
//返回(binnodepener.right==binnodepener.right)&(binnodepener.left==binnodepener.left);
return(*right==*(right.right))&&(*left==*(right.left));
}
您在使用此代码时遇到了哪些错误?我怀疑您没有考虑您的要求。在编写运算符==之前,您需要知道两个BST树的相等是什么意思,是吗?另外,对于Operator first,我得到了一个2664错误,无法将std::string const转换为std::string。我知道从哪里开始打印输出函数,但我正在测试运算符==它不允许我比较文件中的字符串。在对操作符==进行了大量调整和测试之后,我决定把它留到最后,使用操作符>。这也是错误的,我现在正试图找出我的运算符==因为在尝试了这么多示例之后,我似乎无法理解它。例如,我尝试使用运算符(错误消息说“Left不是BST的成员”)输出树中已经存在的内容。果然,我没有看到Left()的定义
代码中的任意位置。这是我尝试比较时遇到的错误。错误1错误C2664:“BST::operator==”:无法将参数1从“std::string”转换为“const BST&”我尝试将另一个指针指向BinNodePointInter并比较左右,但也不起作用。请参阅我对原始答案所做的编辑…这有帮助吗你明白发生了什么吗?@Eric你提出了一个非常糟糕的代码。类BST有数据成员BinNodePointer myRoot;你必须使用它,而不是强制使用BST。@Vlad不,这不是正确的代码。如果你仔细阅读,你会发现我给出的代码与OP使用的代码相同,因此是不正确的。
Error 1 error C2039: 'Left' : is not a member of 'BST<DataType>'
Error 2 error C2039: 'right' : is not a member of 'BST<DataType>'
template <typename DataType>
bool BST<DataType>::operator ==(const BST& right) const
{
//Postcondition: The value returned is true if p1 and p2
// are identical; otherwise false returned.
return (BinNodePointer.right == BinNodePointer.right) && (BinNodePointer.left == BinNodePointer.left);
}
if(start.operator==(motor))
cout << "They are equal.";
else
cout <<"they are not equal.";
Error 1 error C2664: 'BST<DataType>::operator ==' : cannot convert parameter 1 from 'std::string' to 'const BST<DataType> &'
template <typename DataType>
bool BST<DataType>::operator ==(const BST& right) const
{
//Postcondition: The value returned is true if p1 and p2
// are identical; otherwise false returned.
return (BinNodePointer.right == BinNodePointer.right) &&
(BinNodePointer.left == BinNodePointer.left);
}
typedef BinNode * BinNodePointer;
return ((BinNode *).right == (BinNode *).right) &&
((BinNode *).left == (BinNode *).left);
template <typename DataType>
bool BST<DataType>::operator ==(const BST& right) const
{
//Postcondition: The value returned is true if p1 and p2
// are identical; otherwise false returned.
//return (BinNodePointer.right == BinNodePointer.right) && (BinNodePointer.left == BinNodePointer.left);
return (*right == *(right.right)) && (*left == *(right.left));
}