지금까지 많은 것들을 시도했지만 쓸모가 없습니다. 내가 오버로드 된 연산자 구문이나 액세스 권한을 얻지 못하는 것 같습니다. 누구나 제대로 이러한 과부하 연산자를 사용하는 방법의 올바른 방향으로 나를 가리켜 주시겠습니까? 헤더 파일.나무를 사용하여 연산자 ==, <<,>> 오버로드 도움이 필요합니다
#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
이것은 내 오버로드 된 연산자를 쓴 후 나는 기본적으로 테스트를 시작 내 main.ccp입니다, 그리고 훨씬을 조정하기 때문에, 나는 약 2 일이 내가 어떤 멤버 함수에 액세스 할 수 없습니다 알아 내려고 지출 조정 후.
#include<iostream>
#include<fstream>
#include"BST.h"
using namespace std;
int main()
{
BST<string> C;
C.readFile();
C.empty();
C.insert("myself");
cout << C;
system("pause");
return 0;
}
나는 ==
, <<
, >>
의 운영 사례로 보았다,하지만 난 도움이 많이 아무것도 이진 검색 트리를 사용하여 발생 적이 없다.
과 같습니다. (< < C cout을)
나는
friend std::ostream & operator <<(std::ostream & outs, const BST & BinNode) {outs << BinNode.Left<< " " << BinNode.right;
return outs;};
이
를 사용하여cout << C;
이진 검색 트리 안에 이미 무엇을 출력 내가 ostream에 전화에서 얻을 오류입니다 노력하고 있어요 메인에서
Error 1 error C2039: 'Left' : is not a member of 'BST<DataType>'
Error 2 error C2039: 'right' : is not a member of 'BST<DataType>'
또한 내 readFile() 함수에서부터 , 운영자 ==을 트리 안에 이미 문자열로 들어오는 문자열을 비교하려고하지만 난이 나를 죽이고 무엇 ↑ 포인터 클래스
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);
}
에 연산자를 만들 필요가 보인다. 나는 내가 사용했던 교과서가 나에게 아주 좋은 본보기를 보여주지 못하여 올바른 도움을 줄 수없는 것처럼 보입니다. 그래서 저는 도움을 청합니다.
내가이 사용에서 ReadFile() 함수 내에서 == 연산자를 호출했을 때 난 그냥 여기에 게시하려고합니다 답변 ..
을 게시 할 수 없기 때문에 :
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> &'
는이 코드를 어떻게 오류를 얻고있다? –
나는 당신이 묻는 것에 대해 생각하지 않는다고 생각합니다. 연산자 ==를 작성하기 전에 두 개의 BST 트리가 동일한 지의 여부를 알아야합니다. 또한 연산자 <<에 대해서는 인쇄 기능입니다. 우리는 트리에 대한 인쇄 기능을 작성할 때 어디에서 시작해야할지 모르십니까? – RichardPlunkett
처음에는 2664 오류가 발생하여 std :: string const를 std :: string으로 변환 할 수 없습니다. 어디에서 인쇄 기능을 시작해야하는지 알지만 파일에서 문자열을 비교하지 못하게하는 연산자 인 ==을 테스트하고있었습니다. 연산자 ==를 너무 많이 조정하여 테스트 한 후에 나는 이것을 마지막으로두고 연산자 << and >>로 작업하기로 결정했습니다. 그렇다면 그것은 틀 렸습니다. 저는이 예제를 시도한 후에 그것을 이해할 수 없기 때문에이 연산자를 이해하려고 노력하고 있습니다. – KidSess