private struct synthax

This is a discussion on private struct synthax within the C++ Programming forums, part of the General Programming Boards category; hello what's the correct synthax for a function implementation which will return a private struct pointer? Below, in green there ...

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    private struct synthax

    hello
    what's the correct synthax for a function implementation which will return a private struct pointer?
    Below, in green there is the type I want to be returned by the function insert.
    In red the obvoius synthax error, since sNode belongs to cTree's scope.

    edit: unclarity code correction
    Code:
    template <class T>
    class cTree {
    	private:
    		struct sNode {
    			...
    		};
    
    		sNode* insert (sNode*, T&);
    		...
    	public:
    		...
    };
    
    //function implementation
    template <class T>
    sNode* cTree<T>::insert (sNode* node, T& data) {
    	sNode *tmp;
    	...
    	return tmp;
    }
    I've tried
    Code:
    cTree<T>::sNode* cTree<T>::insert (sNode* node, T& data) {
    but g++ gives me the same error: expected constructor, destructor, or type conversion before ‘*’ token
    I've also tried other "solutions" with no success and I'm still searching for a link with the right answer.
    If anybody can help, thanks
    Last edited by carlorfeo; 02-26-2008 at 09:09 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Maybe I'm missing something obvious, but why do you want to return a pointer to node? If it's going to be called like my_tree.insert(), I don't see that you would need to return anything (except maybe an error code if something bad happens).

    Similarly, I also don't see why you would need to pass an sNode* into the function either.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you should be inserting and retrieving data and the tree should be the only thing that knows something like a Node exists at all.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    sorry, I've made a typing mistake.
    the insert() function is recursive, and should be private
    the function wil return the leaf which will hold the inserted data.
    But don't take care of those things: the class design is ok, I just didn't post it all to let you see the specific problem
    Last edited by carlorfeo; 02-26-2008 at 09:07 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And I expect you have also tried:
    Code:
    cTree<T>::sNode* cTree<T>::insert (cTree<T>::sNode* node, T& data)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    coder
    Join Date
    Feb 2008
    Posts
    127
    right suggestion matsp, inddeed I didn't try that.
    but I get the same error

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    coder
    Join Date
    Feb 2008
    Posts
    127
    ok here it is

    file: bst.hpp:
    Code:
    #include <queue>
    
    template <class T>
    class bst {
    	private:
    		struct sNode {
    			T data;
    			sNode *left;
    			sNode *right;
    		}
    		*root;
    		queue <sNode*> q;
    
    		sNode* insert (sNode*, T&);
    		void inorder (sNode*);
    
    	public:
    		bst ();
    		~bst ();
    		
    		void insert (T);	//insert data
    		void inorder ();	//print inordered data
    };
    file: bst.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    //! CONSTRUCTOR
    template <class T>
    bst<T>::bst () {
    	root = NULL;
    }
    
    //! DESTRUCTOR
    template <class T>
    bst<T>::~bst () {}
    
    //! INSERT
    template <class T>
    void bst<T>::insert (T data) {
    	
    	if (root == NULL) {
    		root = new sNode;
    		root->data = data;
    		root->left = NULL;
    		root->right = NULL;
    	}
    	else {
    		//sNode *tmp = insert (root, data);
    	}
    }
    
    template <class T>
    bst<T>::sNode* bst<T>::insert (bst<T>::sNode *node, T &data) {
    
    	if (data == node->data)		//already exists
    		return NULL;
    	
    	sNode *tmp;
    	if (data < node->data) {			//minus
    		if (node->left == NULL) {
    			tmp = node;
    		}
    		else {
    			tmp = insert (node->left, data);
    		}
    	}
    	else if (data > node->data) {		//major
    		if (node->right == NULL) {
    			tmp = node;
    		}
    		else {
    			tmp = insert (node->right, data);
    		}
    	}
    	return tmp;
    }
    
    //! INORDER
    template <class T>
    void bst<T>::inorder () {
    	if (root != NULL)
    		inorder (root);
    	else
    		cout << "empty\n";
    }
    
    
    template <class T>
    void bst<T>::inorder (sNode* node) {
    	if (node->left != NULL)
    		inorder (node->left);
    
    	cout << node->data;
    	
    	if (node->right != NULL)
    		inorder (node->right);
    }

  9. #9
    coder
    Join Date
    Feb 2008
    Posts
    127
    NOTE: some functions are not complete and probably contain mistakes: I couldn't go ahead since I'm stuck in that compiling error

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Code:
    queue <sNode*> q;
    should be:
    Code:
    std::queue<sNode*> q;
    Code:
    template <class T>
    bst<T>::sNode* bst<T>::insert (bst<T>::sNode *node, T &data) {
    should be:
    Code:
    template <class T>
    typename bst<T>::sNode* bst<T>::insert(typename bst<T>::sNode *node, T &data) {
    Also, I suggest that you read: Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?

    EDIT:
    Aha, I found a relevant explanation from that FAQ as to why the typename keyword should be used in this case:
    Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?

    The reason for fully qualifying queue to std::queue should be obvious.
    Last edited by laserlight; 02-26-2008 at 09:35 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    coder
    Join Date
    Feb 2008
    Posts
    127
    Good laserlight, that worked out.
    I'm learning how to make templates so your link will help me a lot, I'm reading it.
    thanks to all.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Remember that a struct is not a class and a class is not a struct. Yes, they may be similar, yes they may work the same, but they aren't the same.
    Struct has public default visibility and class has private default visibility, so they are not the same.
    I want to point that out.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    coder
    Join Date
    Feb 2008
    Posts
    127
    Sure Elysia, I know they are not the same, thanks for pointing that

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Remember that a struct is not a class and a class is not a struct. Yes, they may be similar, yes they may work the same, but they aren't the same.
    Struct has public default visibility and class has private default visibility, so they are not the same.
    Actually, section 9 of the C++ Standard states:
    A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public by default (clause 11). A union is a class defined with the class-key union; its members are public by default and it holds only one data member at a time (9.5).

    So technically, a struct is a class and a union is a class, in the sense that they are proper subsets of the set of classes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Technically perhaps, but I like to separate them since they do not work the same way.
    So if you say "I'm using a class" and use "struct" (thinking they are classes), then you'll get problems with the visibility.
    If struct was just another keyword for class, then I would agree.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21