Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do I declare a template class variable and make an instance seperately?

Reply
Thread Tools

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

 
 
Irish
Guest
Posts: n/a
 
      11-07-2005
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

}

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      11-07-2005

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

 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      11-07-2005
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


 
Reply With Quote
 
Irish
Guest
Posts: n/a
 
      11-07-2005
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...

 
Reply With Quote
 
Irish
Guest
Posts: n/a
 
      11-07-2005
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'

 
Reply With Quote
 
Irish
Guest
Posts: n/a
 
      11-07-2005
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.......


}
}
}

 
Reply With Quote
 
Josh Mcfarlane
Guest
Posts: n/a
 
      11-07-2005
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

 
Reply With Quote
 
Irish
Guest
Posts: n/a
 
      11-07-2005
Ahhhhh..... thanks for the good explanation

peace, Irish

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
can I declare a class variable inside that class method? John Black C++ 2 06-15-2004 03:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57