c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : error C2143: syntax error : missing ';' before 'BinaryTree<E>::getNode'

in cpp file

template <class E>
nodePtr BinaryTree<E>::getNode(E item){

    nodePtr<E> temp = new nodePtr;
    temp->data = item;
    return nodePtr;

}

in headr file:

    struct node;
    typedef node *nodePtr;
    struct node
    {
        E data;
        nodePtr left;
        nodePtr right;
    };

I can't understand why the compiler wont accept this functio signature. please help.

Rahul.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If I'm not mistaken your "node" is also a template. Hence your code should look as following:

template <class E>
struct node
{
    E data;
    node<E>* left;
    node<E>* right;
};

template <class E>
node<E>* BinaryTree<E>::getNode(E item) {...}

In case you want to use forward declaration try something like that (not 100% sure this sintax is correct)

template <class E>
struct node<E>;

template <class E>
typedef node<E>* nodePtr<E>

template <class E>
nodePtr<E> BinaryTree<E>::getNode(E item) {...}
Comments
Problem solved.

First off, shurik_r got most of that correct, however, you cannot use
typedef for unspecified templates , so you cant write

// THIS IS NOT ALLOWED 
template <class E>
typedef node<E>* nodePtr<E>;

However, you can do this should you wish

typedef node<int> nodeInt;

The correct way to write a template forward declaration is this

template<typename E> struct node;

Actually, you don't need the forward declaration since you have a full declaration of node. And nodePtr is a horrific mess when node<E>* seems easier to read (to me).

First of all thankyou for your comets guys, This is what my header looks like.

#ifndef BinaryTree_H
#define BinaryTree_H

# include <iostream>
using namespace std;

template <class E> class BinaryTree
{
public:
    // Class constructors and destructor
    BinaryTree();
    BinaryTree(const BinaryTree<E> &tree);
    ~BinaryTree();
    // Member functions
    int height() const;
    void insert(E item);
    void recordDepths(int depthList[]) const;
protected:
    // Data members
    struct node;
    typedef node *nodePtr;
    struct node
    {
        E data;
        nodePtr left;
        nodePtr right;
    };
    nodePtr root;
    // Member functions
    nodePtr getNode(E item);
    nodePtr copy(nodePtr treeHead);
    void destroy(nodePtr &treeHead);
    void insertValue(E value, nodePtr &treeHead);
    int treeHeight(nodePtr treeHead) const;
    void checkDepth(nodePtr treeHead, int currDepth, int depthList[]) const;
};




#endif

this is my cpp file.

# include "BinaryTree.h"

template <class E>
void BinaryTree<E>::insertValue(E value, nodePtr &treeHead){

    // Add the item to the binary sort tree to which the parameter
    // "root" refers.  Note that root is passed by reference since
    // its value can change in the case where the tree is empty.
    if ( treeHead == NULL ) {
        // The tree is empty.  Set root to point to a new node containing
        // the new item.  This becomes the only node in the tree.
        treeHead = getNode( value );
        // NOTE:  The left and right subtrees of root
        // are automatically set to NULL by the constructor.
        // This is important!
        return;
    }
    else if ( value < treeHead->item ) {
        insertValue( value, treeHead->left );
    }
    else {
        insertValue( newItem, treeHead->right );
    }
}  // end treeInsert()

template <class E>
nodePtr<E> BinaryTree<E>::getNode(E item){

    nodePtr<E> temp = new nodePtr;
    temp->data = item;
    return nodePtr;

}

template <class E>
void BinaryTree<E>::checkDepth(nodePtr treeHead, int currDepth, int depthList[]) const{

    nodePtr temp = new nodePtr;
    temp = treeHead;
    int count;
    if (temp->left != NULL){
        depthList[currDepth] = depthList[currDepth] + 1;
        checkdepth(temp->left, currDepth+1, depthlist[]);
    }

    if (temp->right != NULL){
        depthList[currDepth] = depthList[currDepth] +1;
        checkdepth(temp->right, currDepth+1, depthlist[]);
    }

}

template <class E>
 BinaryTree<E>::BinaryTree(){
    nodePtr root = new nodePtr;
}

template <class E>
int BinaryTree<E>::treeHeight(nodePtr treeHead) const{

}

template <class E>
BinaryTree<E>::BinaryTree(const BinaryTree<E> &tree){

}

template <class E>
nodePtr BinaryTree<E>::copy(nodePtr treeHead){

}

template <class E>
BinaryTree<E>::destroy(BinaryTree<E>::nodePtr &treeHead){

}

template <class E>
BinaryTree<E>::height() const{

}

template <class E>
BinaryTree<E>::insert(E item){
    inserValue(item, root);
}

template <class E>
BinaryTree<E>::recordDepths(int depthList[]) const{

}

template <class E>
BinaryTree<E>::~BinaryTree(){

}

Buildlog

------ Build started: Project: cs340 proj1, Configuration: Debug Win32 ------
Compiling...
BinaryTree.cpp
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : error C2143: syntax error : missing ';' before '<'
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : error C2988: unrecognizable template declaration/definition
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : error C2059: syntax error : '<'
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : error C2065: 'E' : undeclared identifier
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(36) : error C2065: 'E' : undeclared identifier
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(36) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(36) : error C2447: '{' : missing function header (old-style formal list?)
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(69) : error C2143: syntax error : missing ';' before 'BinaryTree<E>::copy'
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(69) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(69) : error C2086: 'int nodePtr' : redefinition
        c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(27) : see declaration of 'nodePtr'
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(74) : warning C4346: 'BinaryTree<E>::nodePtr' : dependent name is not a type
        prefix with 'typename' to indicate a type
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(74) : error C2061: syntax error : identifier 'nodePtr'
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(76) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(81) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(86) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\binarytree.cpp(91) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Main.cpp
c:\documents and settings\administrator.rahulabbine44fd\desktop\cs340 proj1\cs340 proj1\main.cpp(9) : error C2061: syntax error : identifier 'BinayTree'
Generating Code...
Build log was saved at "file://c:\Documents and Settings\Administrator.RAHULABBINE44FD\Desktop\cs340 proj1\cs340 proj1\Debug\BuildLog.htm"
cs340 proj1 - 17 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Edited 3 Years Ago by mike_2000_17: Fixed formatting

As you can see the struct is not a template on it's own, it's just a scruct in a template class.

Ok first USE CODE TAGS. Sorry to shout but it is written in the introduction to the forum, it is written as the top announcement to this and almost every forum. e.g http://www.daniweb.com/forums/announcement8-3.html.

Second: Do not expect help after a "bump" post. I tend to get back to posts I have contributed to but normally once a day.

Third when adding extra posts to a question show a slight indication that you have (a) read the previous posts (b) read the error messages that your compiler has written out. (c) notice we are using code tags.

So let me see errors starts on line 27.

template <class E>
nodePtr<E> BinaryTree<E>::getNode(E item)

Surprised, Now read my first post. Then fix.

While you are at it DONT pass template class objects as value, but as reference. Image E was not int but say a 500kb class. In this case use const E& Sorry for the harsh reply, but help is normally gladly given, but in return I expect to feel that I make a small difference, in this case the that hasn't happened.

Edited 7 Years Ago by StuXYZ: n/a

k will use code tags, np and i know that it pointless to use templates when you are trying to pass values by value in your class. however, I did not design the calss.

k will use code tags, np and i know that it pointless to use templates when you are trying to pass values by value in your class. however, I did not design the calss.

(1) It is not pointless to use templates just that it is better to use references.
(2) However, designed the class didn't design it well enough to compile so it that is sufficient excuse then sorry...
(3) Have you actually read the orginal posts and fixed the compile errors?

actually the class is used to store object, thats why it's pointless.

Sorry my fault about not being clear about point 1.

your code is

template <class E>
nodePtr<E> BinaryTree<E>::getNode(E item){
nodePtr<E> temp = new nodePtr;
temp->data = item;
return nodePtr;
}

Note that you call E's copy constructor for item and then you call E's assignment operator to set temp->data. That is not something the optimizer can remove. Since both the copy and assignment operators can have side-effects.
Try this

template <class E>
node<E>* 
BinaryTree<E>::getNode(const E& item)
{
node<E>* temp = new node<E>;
temp->data=item;
return temp;            
}

That corrects a multitude of errors:
(a) you can't return a type. (nodeptr)
(b) you can't use a templated templated pointer
(c) you cant have open template typedef. [explicit is ok]
(d) you avoid the double copy.

I have not added a node<E>(const E&) constructor which I think should be done. And you MUST add a constructor to this struct since you will have pointers that point to a random location if you don't

Anyway hope that helps you get further

I just defined the function at the declaration itself. it's working fine although I would like to know a better way to do it.

Thanks.

This article has been dead for over six months. Start a new discussion instead.