473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

18
#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 ???
Oct 16 '09 #1
5 3028
gskbond
18
Hello???Is there anybody who can reply?
Oct 16 '09 #2
newb16
687 512MB
You can't use non-const non-type argument to instantiate template.
Oct 16 '09 #3
Banfa
9,065 Expert Mod 8TB
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.
Oct 16 '09 #4
gskbond
18
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???
Oct 17 '09 #5
Banfa
9,065 Expert Mod 8TB
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.
Oct 17 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
2
by: Stephen Tang | last post by:
Hi, I'm relatively new at this language, so I've been trying to find parallels to problems I've run into in the past. This is the hypothetical problem: I want to write a CD inventory...
10
by: Brian Henry | last post by:
Hi, I am having a problem with an attachment system I made... it works with files up to ~3MB in size then after that if you try to upload a file it just goes to a "Page can not be displayed" page...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
2
by: printline | last post by:
Hi all I have a problem with a form script. The form script should do the following: When a user selects a value from a drop down list and hits the "next" button it should take him to a...
2
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.