473,387 Members | 1,574 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,387 software developers and data experts.

How do I declare a template class variable and make an instance seperately?

Hello all :)
Hopefully someone can shed some light on this problem I'm having. I'm
trying to declare a variable to a type of class I've defined (which is
a minHeap), but actually instantiate it later when I get a command from
the user. This is my class and if you look down a lil you can see what
I'm trying to do in my main(). I want to ask for the size from the
user, but when I try to I can't get my code to compile. Here is the
class definition and the constructor. This is the error I'm getting.

/Users/ci/minheap/minheap.h:42: error: prototype for 'HEAP<T>::HEAP()'
does not match any in class 'HEAP<T>'

I've been reading up on smart pointers but i'm not sure if that's what
I'm suppose to be using. Any help/info would help me alot.......
THANKS

#-------------------------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;

struct ELEMENT{int key;};

template<class T>
class HEAP {
public:
HEAP(int n);
~HEAP(){ delete [] heap;}
T Min(){if (CurrentSize == 0)
throw OutOfBounds();
return heap[0];}

HEAP<T>& Insert(int k);
int DeleteMin();

void printHeap() const;

private:
int CurrentSize, Capacity;
T *heap; // element array
};

template<class T>
HEAP<T>::HEAP(int n){ //constructor.
Capacity = n;
cout << "Heap's Capacity is: " << Capacity << endl;
heap = new ELEMENT[Capacity];
CurrentSize = 0;
}

int main(void){

HEAP<ELEMENT> A(10); //THIS WORKS FINE
HEAP<ELEMENT> B; // I CAN'T DO THIS THOUGH...why?? how can
I??

int size;

cout << "Give the desired size of this heap: " << endl;
cin >> size;
B(size); // I WANT TO INSTANTIATE B DOWN HERE

}

Nov 7 '05 #1
7 3151

Irish wrote:
Hello all :)
Hopefully someone can shed some light on this problem I'm having. I'm
trying to declare a variable to a type of class I've defined (which is
a minHeap), but actually instantiate it later when I get a command from
the user.
Don't declare it until you're ready to use it.
This is my class and if you look down a lil you can see what
I'm trying to do in my main(). I want to ask for the size from the
user, but when I try to I can't get my code to compile. Here is the
class definition and the constructor. This is the error I'm getting.

/Users/ci/minheap/minheap.h:42: error: prototype for 'HEAP<T>::HEAP()'
does not match any in class 'HEAP<T>'
Because you haven't defined a default constructor ... but you actually
don't need it, if you just defer declaring the variable until you need
it.

#-------------------------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;

struct ELEMENT{int key;};

template<class T>
class HEAP {
public:
HEAP(int n);
~HEAP(){ delete [] heap;}
T Min(){if (CurrentSize == 0)
throw OutOfBounds();
return heap[0];}

HEAP<T>& Insert(int k);
int DeleteMin();

void printHeap() const;

private:
int CurrentSize, Capacity;
T *heap; // element array
};

template<class T>
HEAP<T>::HEAP(int n){ //constructor.
Capacity = n;
cout << "Heap's Capacity is: " << Capacity << endl;
heap = new ELEMENT[Capacity];
CurrentSize = 0;
}

int main(void){

HEAP<ELEMENT> A(10); //THIS WORKS FINE
Delete the following line entirely.
HEAP<ELEMENT> B; // I CAN'T DO THIS THOUGH...why?? how can
I??

int size;

cout << "Give the desired size of this heap: " << endl;
cin >> size;
B(size); // I WANT TO INSTANTIATE B DOWN HERE
For the preceding line, substitute:
HEAP<ELEMENT>B(size);

}


Best regards,

Tom

Nov 7 '05 #2
Irish wrote:

[snip]
template<class T>
class HEAP {
public:
HEAP(int n);
~HEAP(){ delete [] heap;}
T Min(){if (CurrentSize == 0)
throw OutOfBounds();
return heap[0];}

HEAP<T>& Insert(int k);
int DeleteMin();

void printHeap() const;

private:
int CurrentSize, Capacity;
T *heap; // element array
};
[snip]
int main(void){

HEAP<ELEMENT> A(10); //THIS WORKS FINE
HEAP<ELEMENT> B; // I CAN'T DO THIS THOUGH...why?? how can
I??
Your class does not contain a default constructor (one that requires no
arguments passed to it). That's what the above is trying to do.

int size;

cout << "Give the desired size of this heap: " << endl;
cin >> size;
B(size); // I WANT TO INSTANTIATE B DOWN HERE
In that case, remove the erroneous line at the top and do this here:
HEAP<ELEMENT> B(size);

You can't define an object but delay its "instantiation" till later in the
function. Defining it is what instantiates it.

}


DW
Nov 7 '05 #3
Thanks Tom...... that works! But how come it doesn't work if I put it
into a switch statement's case block? Like if I change my main to:

int main(void){

int go = 1, size;
char command1;

while(go == 1){
cin >> command1;
switch(command1){
case 'c':
case 'C':
cin >> size;
HEAP<ELEMENT> B(size); //THIS GIVES ME AN ERROR??
break;

case 's': //STOP PROGRAM
case 'S':
return 0;
break;
//etc.......
}
}
}

I get the error...

Nov 7 '05 #4
whoops forgot this
/Users/ci/minheap/main.cpp:46: error: jump to case label
/Users/ci/minheap/main.cpp:41: error: crosses initialization of
'HEAP<ELEMENT> B'

Nov 7 '05 #5
K.... well I played with it a lil longer and I found a fix but I don't
know why this way works and not the other...... can anyone explain for
me?

Thanks in advance :)
Chris

int main(void){

int go = 1, size;
char command1;

HEAP<ELEMENT> *heap;

while(go == 1){
cin >> command1;
switch(command1){
case 'c':
case 'C':
cin >> size;
heap = new HEAP<ELEMENT>(size)
break;

case 's': //STOP PROGRAM
case 'S':
return 0;
break;
//etc.......
}
}
}

Nov 7 '05 #6
Irish wrote:
K.... well I played with it a lil longer and I found a fix but I don't
know why this way works and not the other...... can anyone explain for
me?


Think about scope.

In your previous switch statement, think about it like this
(pseudo-code)

{
//Some stuff
{
int b;
}
//Can't use b here, as it's out of scope now.
}

b is no longer a valid variable once you reach the second comment line.
You were declaring it then attempting to use it after it was no longer
in scope.

However, when you do something like this:
{
int* b;
{
b = new int;
}
//Use *b here
delete b;
}

Now, b is declared in a shallow'er scope and is still in scope (the
same one as the current control path) when you attempt to use it.

HTH,
Josh McFarlane

Nov 7 '05 #7
Ahhhhh..... thanks for the good explanation :P

peace, Irish

Nov 7 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul | last post by:
Hi there, Quick question please. I have a file containing a number of re-usable classes. Each require database access. Seperately, I have a database class called dbaccess. Is it best to: A....
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Steven T. Hatton | last post by:
#include <iostream> namespace ns{ const char name = "This is a Class Name";//won't compile //char name = "This is a Class Name"; // compiles template <typename T, char* Name_CA=name> struct...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
7
by: Jim Langston | last post by:
What I want to do is have an operator= accept a template variable. I will have some classes which all will contain an instance of a different class. I want an operator= in yet a 3rd class to...
2
by: Dave Rudolf | last post by:
Hey all, So I have a template class whose only template parameter is a type that the class is to manage. It's actually an implementation of the common Singleton design pattern. The class looks...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.