Jump to content
  • Advertisement
Sign in to follow this  
DpakoH

[C++]class with template function

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, i need some help on the code below:
#include <iostream>

class test {
  test();
  ~test();
public:
  template <class T> void returndata(T* a);
};
test::test(){}
test::~test(){}
template <class T> void test:: returndata(T* a){ std::cout<< a->data; }

struct node{
       int data;
       node* left;
       node* right;
       };

node *insert(node *tree,int ele)

{
   if(tree==NULL)

   {
        tree=new node;
        tree->left=tree->right=NULL;
        tree->data=ele;
   }
   else
 if(ele < tree->data)
   tree->left=insert(tree->left,ele);
 else
   tree->right=insert(tree->right,ele);
   return(tree);

}

int main() {

  node* root= NULL;
  root = insert(root,10);
  root = insert(root,15);
  root = insert(root,5);

  test t;
  
// HERE COMES WHERE I NEED HELP

t.returndata<node>(root);

/*
 basically i want to call this class function but it gives me an error 
if i try like this, so i was wondering how is the correct syntax, or is 
it correct to write functions like this?!
*/

  return 0;
}





my question is stated in the source code , so pls check it all :) best, y. [Edited by - DpakoH on July 24, 2008 2:14:21 AM]

Share this post


Link to post
Share on other sites
Advertisement
yeap , you are right , but that's not the problem :)
it was just a typo , as i wasnt copy-pasting the code, i was making in out at the moment.

the problem is how to call the class function ?

best,
y.

Share this post


Link to post
Share on other sites
So, you're basically asking us what is the problem with a bit of code you didn't show us?

The call syntax you've used is valid (as long as you correct the obvious error in the function definition, and make it public).

Share this post


Link to post
Share on other sites
I just copy/pasted your code and i got 3x:
cannot access private member declared in class 'test'

class test
{
public: // << there is your problem, add public
test();
~test();
template <class T> void returndata(T* a);
};

Share this post


Link to post
Share on other sites
@D3DXVECTOR3 : no , that's not the problem. in my project the function is defined as public:

@ToohrVyk : well i showed some sample code, otherwise if i have to paste my project it will be a really Loooooong post.


and still there is a problem. the compiler error is :

/home/yani/Desktop/eclipse/workspace/JUB/Debug/../main.cc:70: undefined reference to `void MainWindow::drawTree<node>(node*)'



when i call the function like this:

window.drawTree<node>(root); //window is instance of MainWindow class



and the functions itself is

template <class T> void MainWindow::drawTree(T* root)
{
// TO DO
// just an empty function
}




so,any more suggestions ?

Share this post


Link to post
Share on other sites
Maybe it should be "typename T" instead of "class T" in the template definition.

Otherwise, post a minimal but complete program that has the problem that we can compile for ourselves.

Share this post


Link to post
Share on other sites
This compiles fine with g++ 3.4.0:

#include <iostream>

class test {
public:
test();
~test();
template <class T> void returndata(T* a);
};
test::test(){}
test::~test(){}
template <class T> void test:: returndata(T* a){ std::cout<< a->data; }

struct node{
int data;
node* left;
node* right;
};

node *insert(node *tree,int ele)

{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
}
else
if(ele < tree->data)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);

}

int main() {

node* root= NULL;
root = insert(root,10);
root = insert(root,15);
root = insert(root,5);

test t;


// HERE COMES WHERE I NEED HELP

t.returndata<node>(root);

/*
basically i want to call this class function but it gives me an error
if i try like this, so i was wondering how is the correct syntax, or is
it correct to write functions like this?!
*/


return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!