I am getting a no match for operator error and need help hunting it down.

/////////////////////////////////////////////////////////////////////
main.cpp

#include <iostream>

#include "StringTree.h"


using namespace std;

int main()
{

/*
* String Tree problem
*/


StringTree theTree;

theTree.insert("middle");
theTree.insert("grape");
theTree.insert("apple");
theTree.insert("house");
theTree.insert("pine");
theTree.insert("tree");
theTree.insert("never");

cout << "The tree is \n";
cout << theTree.displayTree();

//cout << "Pre-order: " << theTree.preOrder();
//cout << "In-order: " << theTree.inOrder();
//cout << "Post-order: " << theTree.postOrder();

return 0;
}

//////////////////////////////////////////////////////////////////////////
StringTree.h


#ifndef STRINGTREE_H
#define STRINGTREE_H
#include <string>

class StringTree
{
public:
StringTree();
virtual ~StringTree();
void insert(std::string key);
void displayTree();

protected:

private:
//defines what a node is
struct node{
std::string key;
node* left;
node* right;
};
//this is the root
node*root;
//
node*CreateLeaf(std::string key);
//add leaf private
void AddLeafPrivate(std::string key, node*Ptr);
//print in private shhhh
void displayTreePrivate(node*Ptr);

};

#endif // STRINGTREE_H
///////////////////////////////////////////////////////////////////////////
StringTree.cpp

#include "StringTree.h"

#include <cstdlib>

StringTree::StringTree()
{
root=nullptr;
}

StringTree::~StringTree()
{
//dtor
}

StringTree::node*StringTree::CreateLeaf(std::string key){
node*n=new node;

n->key=key;
n->left=nullptr;
n->right=nullptr;

return n;
}


void StringTree::insert(std::string key){
return AddLeafPrivate(key, root);
}

void StringTree::AddLeafPrivate(std::string key, node*Ptr){
if(root==nullptr){
root=CreateLeaf(key);
}
else if(key< Ptr->key){
if(Ptr->left!=nullptr){
AddLeafPrivate(key, Ptr->left);
}
else{
Ptr->left=CreateLeaf(key);
}
}
else if(key> Ptr->key){

if(Ptr->right!=nullptr){

AddLeafPrivate(key, Ptr->right);
}
else{
Ptr->right=CreateLeaf(key);
}
}
else{
std::cout<<"The key "<< key<<" has already been added to the tree."<<std::endl;
}
}


void StringTree::displayTree(){
displayTreePrivate(root);
}

void StringTree::displayTreePrivate(node*Ptr){
if(root!=nullptr){
if(Ptr->left!=nullptr){
displayTreePrivate(Ptr->left);
}
std::cout<< Ptr->key<<" ";
if(Ptr->right!=nullptr){
displayTreePrivate(Ptr->right);
}
else{
std::cout<<"The tree is empty... hmm..";
}
}

}
////////////////////////////////////////////////////////////////////////////
The error is as follows:

C:\Users\Tim Allen\Desktop\C++162\BST\main.cpp|27|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')|


Its an annoyingly small bug. I am pretty sure my logic is right for the code though. This is supposed to be a ordered binary search tree of strings.
Feel free to tear apart my code what could I do better what is ok anything you think will help. Thank you
Perhaps replace
 
    cout << theTree.displayTree();
with simply
 
    theTree.displayTree();

you are a god send. However I do not necessarily understand why that changed or fixed things.
Remove cout before displayTree():

cout << theTree.displayTree();
I do not necessarily understand why that changed or fixed things.

You've defined StringTree::displayTreePrivate as void, so that it doesn't return a value. So you're streaming an undefined value to standard out.

In any case, that method already calls another method which contains the code that outputs data to standard out, so why are you trying to also stream a (non-existant) return value in main?

Also, please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
However I do not necessarily understand why that changed or fixed things.

Well the cout was not needed in the first place. Function displayTree() which invokes displayTreePrivate() has its own cout inside the function.

It also wouldn't make sense to try to do anything with the value returned by a function when the function is declared as void and thus has no return value.
Topic archived. No new replies allowed.