GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Oct-2008, 18:14
dreamuser dreamuser is offline
New Member
 
Join Date: Jun 2008
Posts: 5
dreamuser is an unknown quantity at this point

Need a little syntax help I think


I have a templated class something like this:

CPP / C++ / C Code:
template <class ItemType>
class TernaryTree
{
.

.

.
private:

  struct TernaryNode{
    .
    .
  }

  TernaryNode *root;

}

Anyway my question is when I do the following declaration:

TernaryNode * getNodeByElement(. . . )

I do not know the syntax for the implementation... I've tried things like
TernaryNode TernaryTree<ItemType>::*getNodeByElement( . . . )

but nothing has worked so far.

What am I doing wrong here?
Last edited by admin : 06-Oct-2008 at 22:36. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 06-Oct-2008, 19:33
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need a little syntax help I think


Quote:
Originally Posted by dreamuser
What am I doing wrong here?
It is not clear from the code posted as to where you are trying to access this nested structure. Given that this structure is declared private, it can only be used within the scope of the class. The following example illustrates this point:
CPP / C++ / C Code:
template <class T>
class foo {
    struct bar { } *p;
public:
    bar* f()  { return p; }
    void g();
};

int
main() {
    foo<int> fi;
    fi.g();

    return 0;
}

template <class T>
void
foo<T>::g() {
    foo<T>::bar *p = f();
    bar *q = f();
}
Note that inside member function g(), instances of the nested structure can be defined both implicitly & explicitly. You will not have this latitude if the nested structure is to be accessed publicly. eg.
CPP / C++ / C Code:
template <class T>
class foo {
public:
    struct bar { } *p;
    bar* f()  { return p; }
};

int
main() {
    foo<int> fi;

    foo<int>::bar *p = fi.f();

    return 0;
}
Reply back if you still have questions.

Likewise, when posting code, please place a [cpp] tag before the code, & [/cpp] afterwards. This maintains formating & color codes keywords & identifiers.
  #3  
Old 06-Oct-2008, 19:51
dreamuser dreamuser is offline
New Member
 
Join Date: Jun 2008
Posts: 5
dreamuser is an unknown quantity at this point

Re: Need a little syntax help I think


The function is a private member function of the class.

They are all declared and implemented in a header file and I use the scope operator to connect them like:

CPP / C++ / C Code:
template <class ItemType>
bool TernaryTree<ItemType>::GetStuff()
{
.
.
.
}

I need to know the correct syntax to use implementing
CPP / C++ / C Code:
TernaryNode * getNodeByElement( .. .. )
  #4  
Old 06-Oct-2008, 20:12
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need a little syntax help I think


Quote:
Originally Posted by dreamuser
I need to know the correct syntax to use implementing
CPP / C++ / C Code:
TernaryNode * getNodeByElement( .. .. )
What in my first example does not answer your question?
  #5  
Old 06-Oct-2008, 20:24
dreamuser dreamuser is offline
New Member
 
Join Date: Jun 2008
Posts: 5
dreamuser is an unknown quantity at this point

Re: Need a little syntax help I think


I am used to implementing functions like this
CPP / C++ / C Code:
template <class ItemType>
bool TernaryTree<ItemType>::DoStuff()
{
.. .. .. 
... . .. 
}

Can you be a little more specific to my situation with your answer?
  #6  
Old 06-Oct-2008, 20:40
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need a little syntax help I think


Quote:
Originally Posted by dreamuser
Can you be a little more specific to my situation with your answer?
CPP / C++ / C Code:
template <class T>
class TernaryTree {
    struct TernaryNode { } *root;
    TernaryNode* getNodeByElement()  { return root; }
    void f();
public:
    void g();
};

int
main() {
    TernaryTree<int> ti;
    ti.g();

    return 0;
}

template <class T>
void
TernaryTree<T>::f() {
    TernaryTree<T>::TernaryNode *p = getNodeByElement();
    TernaryNode *q = getNodeByElement();
}

template <class T>
void
TernaryTree<T>::g() {
    TernaryTree<T>::TernaryNode *p = getNodeByElement();
    TernaryNode *q = getNodeByElement();
    f();
}
Again, if the member function(s) returning pointers to the nested structure are only used within class scope, the pointer itself can either be explicitly or implicitly typed (note in both examples, the enclosing class does not need to be specified...).
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists advice request promsan C Programming Language 74 23-May-2007 09:29
syntax errors in document coder MS Visual C++ / MFC Forum 0 11-Mar-2006 05:12
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 11:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 21:30
syntax error ??? crq C++ Forum 2 23-Jan-2005 17:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.