Unable to declare 2 friend overloaded<< in a template .h
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?
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::
.)
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
Possibly you need this:
const BSTree::Node* &out
Node is internal structure.
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.
精彩评论