0

I'm trying to create two overloaded operators in a template BSTree.h and am encountering errors that really don't tell me what the problem is. Running a search on the error codes seperate or in conjunction hasn't yielded anything for me.

The first overloaded<< for the BSTree doesn't cause any errors on compile, but the 2nd overloaded<< I created for my Node struct keeps returning the following errors:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '*'

#ifndef BSTREE_H
#define BSTREE_H

#include <iostream>
#include <fstream>

template <typename T>
class BSTree{

friend ostream& operator<<(ostream&, const BSTree<T>&); 

public:
    BSTree();
    //BSTree(const BSTree &);
    ~BSTree();

    void buildTree(ifstream&);
    void setType(char);
    bool getType(char);

    bool insert(T*);

    bool isEmpty();


private:
    char type;

    struct Node{
        T* data;

        //subnode[0] == left subtree
        //subnode[1] == right subtree
        Node* subnode[2];
    };

    Node* head;
    void destructorHelper(Node* &);
    bool insertHelper(T*, Node* &);
    friend ostream& operator<<(ostream&, const Node*&); 

};

The compiler says the errors occur at the line where the Node overloaded<< code is.

template <typename T>
ostream& operator<<(ostream &output, const BSTree<T> &out) {
    if(head != NULL)
        output << head;
    return output;
}

template <typename T>
ostream& operator<<(ostream &output, const Node* &out) {
    if(out != NULL){
        output << out->subnode[0];
        output << *out->data;
        output << out->subnode[1];
    }

    return output;
}  

Am I not allowed to declare 2 overloaded<< in the same .h even if they are for different objects? Or am I messing something up in my code?

Moniker
  • 5
  • 2

4 Answers4

2

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Usually this means that the compiler doesn't know an identifier as a type, so it goes assuming it's a parameter name, with the type implicitly being int. (In C of old there was a rule that the int in a parameter type could be omitted.) Code like

void foo(bar);

might emit this, if the compiler doesn't know the type bar and assumes void foo(int bar).

This

template <typename T>
std::ostream& operator<<(std::ostream &output, const typename BSTree<T>::Node* &out)
{
  // ...
}

should compile. (Note the qualifications std:: and BSTree::.)

sbi
  • 219,715
  • 46
  • 258
  • 445
  • This partially worked, doing just const BSTree::Node* returned an C4346 error. I had to do const typename::BSTree::Node* out to make the error go away – Moniker Dec 07 '10 at 19:28
  • 1
    @Moniker: Ah, I overlooked that `BSTree` is a template. Another brainfart of mine, the second today here. I'm sorry. I'm glad you got over this nevertheless. I'll fix my answer anyway, whoever stumbles into this shouldn't have to read the comments to get help. – sbi Dec 07 '10 at 20:42
1

You've got several mistakes in your code:

  • you need to add <> after the function names you declare as friend
  • even if you're in the body of a friend a function, you need to specify objects you call methods on. Remember that functions are not bound to an instance.
  • in the last prototype, Node is not reachable outside the class it's defined in. You must precise BSTree::Node
jopasserat
  • 5,721
  • 4
  • 31
  • 50
0

Possibly you need this:

const BSTree::Node* &out

Node is internal structure.

Alex F
  • 42,307
  • 41
  • 144
  • 212
0

I suspect the problem is that ostream is not in scope at the time that your operator<<() friend functions are declared.

Either add using std::ostream; just inside class BSTree{, or specify the fully qualified typenames:

friend std::ostream& operator<<(std::ostream&, const BSTree<T>&); 
...
friend std::ostream& operator<<(std::ostream&, const Node*&);

Either way, the actual definitions of these functions will need to be changed similarly. Whatever you do, don't be tempted to use either using std::ostream; or (even worse) using namespace std; in a header file at file scope, because it will affect every subsequent declaration in the translation unit.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169