#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;
}
[C++]class with template function
hi all,
i need some help on the code below:
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]
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.
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.
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).
The call syntax you've used is valid (as long as you correct the obvious error in the function definition, and make it public).
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);
};
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);
};
@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 :
when i call the function like this:
and the functions itself is
so,any more suggestions ?
@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 ?
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.
Otherwise, post a minimal but complete program that has the problem that we can compile for ourselves.
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 errorif i try like this, so i was wondering how is the correct syntax, or isit correct to write functions like this?! */ return 0;}
The function itself "void MainWindow::drawTree<node>(node*) should be place in the header of the class itself, nit in the cpp
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement