Connecting Tech Pros Worldwide Help | Site Map

How to take input from user and create class object??

Newbie
 
Join Date: Oct 2009
Posts: 10
#1: Oct 16 '09
#include<iostream>
using namespace std;

template <class T, int mway>
struct BNode
{
int noofKeys;
T keys[mway-1];
BNode<T, mway> *pointer[mway];
BNode();
};

template <class T,int mway>
class BST
{
public:
BST();
~BST();
int SearchRecord(T &toBeFound );
int Search(BNode<T,mway> *curNode,T toBeFound);
int Insert(const T &toBeAdded);
};



int main(int argc,char * argv)
{

typedef BST<int,30> mWayBST;
// typedef BST<int,argc> mWayBST;
mWayBST myBST;
int j = 9;
myBST.Insert(j);
myBST.Insert(10);
myBST.Insert(6);
return 1;
}

I am new to template programming and I have a few doubts while coding.
Here above I have used template programming. This is for mWay Search tree.
Template has two parameters .... first is data type of the elements in the search tree and second is the integer order of tree itself....

Now the problem here is that I want to create an object of above class and the order(mWay) is given by user at runtime using arguments.

But an error occurs during compilation of commented line
// typedef BST<int,argc> mWayBST;

How this can be done ???
Newbie
 
Join Date: Oct 2009
Posts: 10
#2: Oct 16 '09

re: How to take input from user and create class object??


Hello???Is there anybody who can reply?
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#3: Oct 16 '09

re: How to take input from user and create class object??


You can't use non-const non-type argument to instantiate template.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,153
#4: Oct 16 '09

re: How to take input from user and create class object??


Instead of passing mway as a template parameter you should pass it as a parameter of the constructor.

Instead of BNode::keys being an array it should be a pointer that you allocated memory to in the constructor (and delete in the destructor).


P.S. This is an international forum, as such it may take up to 24 hours for you to get a reply, the person who can answer your question may be asleep. We do not consider it acceptable to start demanding attention as little as 1 hour after you first posted.
Newbie
 
Join Date: Oct 2009
Posts: 10
#5: Oct 17 '09

re: How to take input from user and create class object??


I have done according to above...
Now I am facing another problem...

I want to implement a queue using templates...... In this queue I will be pushing class object which itself is using a template....

template <class T>
class BNode
{
public:
int Nodemway;
int noofKeys;
int *keys;
BNode<T> **pointer;
BNode(int imway);
~BNode();
};

Also the queue class is :

template<class T>
class Dq {
public:
Dq(int MSize = 10000);
~Dq() {delete [] Dq;}
bool IsEmpty() const {return f == r;}
bool IsFull() const {return (
((r + 1) % MaxSize == f) ? 1 : 0);}
T First() const;
T Last() const;
Dq<T>& Add(const T& x);
Dq<T>& Delete(T& x);
private:
int f;
int r;
int MaxSize;
T *Dq;
};

Here hence I want to implement a generic queue which will actually allow me to push a BNode on rear and remove BNode from front.

Now while creating object of class Dq , how this should be handled?

I am doing this way , but it is giving problem...
Dq<BNode> myQueue = new Dq<BNode>(10000);

Can you please suggest other way of doing this???
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,153
#6: Oct 17 '09

re: How to take input from user and create class object??


With your current classes you would need to use something like

Dq<BNode<int> > myQueue = new Dq<BNode<int> >(10000);

Note the space in "> >" is important or the compiler interprets it as the shift operator and that is a syntax error.

However if you want to have a queue of BNode holding differing types that wont work.

In that case create a non-templated base class for BNode, have you queue hold these then you can have a queue of BNode of any type.

You would need to redesign BNode but you need to do that anyway because currently all BNode members are public which is very poor practice.
Reply


Similar C / C++ bytes