|
i have started turning this program for a class into a container template class program and i'm having trouble with one part.
Stack *Z = new Stack;
the line above is where i'm having trouble. this line is in my main program and i need to set it up for template but i don't know how to do it for this line i can do templates for everywhere else but because of the * i'm not sure how to handle this one. please any help would be great! thank you!
below is the rest of the code:
#include <iostream><br />
<br />
using namespace std;<br />
<br />
struct Box {<br />
int data;<br />
struct Box * next;<br />
};<br />
template < typename T ><br />
class Stack {<br />
<br />
public:<br />
Stack();<br />
~Stack();<br />
bool Pop(int &);<br />
void Push(int);<br />
bool Empty();<br />
private:<br />
struct Box * Start;<br />
};<br />
template <typename t=""><br />
Stack <t>::Stack() { Start=NULL; }<br />
template <typename t=""><br />
Stack <t>::~Stack() {<br />
<br />
struct Box *p1=Start, *p2;<br />
<br />
while (p1!=NULL) {<br />
p2=p1->next;<br />
delete p1;<br />
p1=p2;<br />
}<br />
}<br />
template <typename t=""><br />
bool Stack <t>::Pop(int &x) {<br />
<br />
struct Box *p1;<br />
if (Start==NULL) return false;<br />
<br />
x=Start->data;<br />
p1=Start->next;<br />
delete Start;<br />
Start=p1;<br />
return true;<br />
}<br />
template <typename t=""><br />
void Stack <t>::Push(int x) {<br />
<br />
struct Box *p1 = new struct Box;<br />
p1->data = x;<br />
p1->next = Start;<br />
Start=p1;<br />
return;<br />
}<br />
template <typename t=""><br />
bool Stack <t>::Empty() { if (Start==NULL) return true; else return false; }<br />
<br />
<br />
int main() {<br />
template <typename t=""><br />
Stack X;<br />
int x;<br />
<br />
for (int i=0; i<10; ++i) X.Push(i);<br />
<br />
while(X.Pop(x)) cout << x << endl;<br />
<br />
cout << "-------------------------------" << endl;<br />
<br />
Stack *Z = new Stack;<br />
<br />
for (int i=0; i<10; ++i) (*Z).Push(i);<br />
<br />
while(Z->Pop(x)) cout << x << endl;<br />
<br />
Stack Y[10];<br />
<br />
for (int j=0; j<10; j++) {<br />
<br />
for (int i=0; i<10; ++i) Y[j].Push(i);<br />
<br />
while(Y[j].Pop(x)) cout << x << endl;<br />
<br />
}<br />
<br />
return 0;<br />
<br />
}</typename></t></typename></t></typename></t></typename></t></typename></t></typename></iostream>
|
|
|
|
|
first of all I dont know why you'd write your own, since the STL provides one ..
but assuming I cant dissuade you, I think :-
Brandon Ramirez wrote: Stack *Z = new Stack;
should be
Stack<int> *Z = new Stack<int> ;
for a stack of int's ... I am in two minds as to wether :-
Stack<int> *Z = new Stack<int>();
is more correct - suck it and see ?
'g'
|
|
|
|
|
Garth J Lancaster wrote: I am in two minds as to wether :-
Stack<int> *Z = new Stack<int>();
is more correct - suck it and see ?
No difference - they both just call the default constructor, so it's just a matter of style.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
thanks for the clarification
'g'
|
|
|
|
|
thanks Garth, that was helpful! i know there is on in the STL but it was an assignment to make our own.
|
|
|
|
|
Hello, folks:
in trial version software, we all see the dialog box with a date frame, during which a user is allowed to test the application until the last day expired.I wonder if there is any component out there up for grab? Or any code?
thanks,
jon
|
|
|
|
|
Try Trail Creator at http://softwarekey-trial-creator.concept-software-inc.qarchive.org/
Why the hell is the link stuff not working
|
|
|
|
|
Hi,
I need to add Vertical and Horizontal scrollbars to a CListBox. I have checked Horizontal scroll and Vertical scroll in the properties of Listbox control , but it doesn't work. I also tried with m_AvailableList.ShowScrollBar(SB_BOTH); But this doesn't work..
Can I know how to add vertical and horizontal scrollbars to ListBox control.
Thanks in advance.
Venkat
|
|
|
|
|
You need to items for see scrollbars.
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
: Use Dev-C++ 4.9.9.2
Your task is to implement a Catalog of Books.
1. For this first you have to implement Book class The Book class is a simple class to store information about a particular book and it has the following data members:
o Title of the book (string)
o Author of the book (string)
o ISBN of the book (string)
o Status of the book (bool)
Write default and parameterized constructors and setters getters for the Book class.
2. Catalog class is a linked list of Book objects and that can perform the following functionality.
o Add method to add a book in the Catalog
o printAllBooks method to print all the books in the Catalog.
o searchByAuthor method will take author name as an argument and will find a book written by that author.
o Available method to print all those books that are currently available. If the status of a book is false than it means that book is not available, if the status is true then it means book is available.
3. Include a main function as follows:
o Main will create 5 different books and add them in Catalog
o Print all books present in Catalog linked list.
o Search a Book by its Author name.
o Print all the books which are available.
Hint:
Make Nodes of Book type and then link them to make Catalog linked list. It means there will be another class between Book and Catalog and that is BookNode class
|
|
|
|
|
Well, good luck with your homework.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
must be a new course starting somewhere - saw two like this last week
'g'
|
|
|
|
|
This isn't even subtle.
This signature was proudly tested on animals.
|
|
|
|
|
#include <iostream>
#include <string>
using namespace std;
int wmain(int , wchar_t* )
{
for ( ; ; )
{
cout << "Enter Title: ";
string s;
cin >> s;
cout << "Title: \"" << s << "\" is invalid." << endl << endl;
}
return 0;
}
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Amazing how a complex problem can be easily solved with such a few lines of code, isn't it?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
That's excellent. But you can add this to increase the performance :
for ( ; ; )
{
cout << "Enter Title: ";
string s;
cin >> s;
Sleep(5000);
cout << "Title: \"" << s << "\" is invalid." << endl << endl;
}
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
thk u joe
ur progrm is what i was looking for. it hlp me enter all the book titles. but my instructor gave me F in the homework. plz hlp, how fix it.
[edit] This was meant to be a joke, but on my original post, I forgot to use the proper icon [/edit]
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
modified on Sunday, March 29, 2009 7:04 PM
|
|
|
|
|
Professor to student: go here[^]
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
modified on Sunday, March 29, 2009 9:22 PM
|
|
|
|
|
Dear Joe,
I am ashamed and deeply embarrassed by my earlier post. It was meant to be sarcastic joke. But I forgot to mark it as joke
In fact if your read the content, it does not make sense. There is no way using your code as is to enter any book title, nor is it possible to exit the loop. I was amused by your code, and thought I throw a joke, but that went south.
Oh, well, better I modify the post before I throw other people off the cliff.
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
Are you kidding? You made me laugh. (My reply may have fallen flat; it's what the professor should say to the student.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
modified on Sunday, March 29, 2009 9:21 PM
|
|
|
|
|
|
Hi all,
I have a report view type ListCtrl.here i m inserted number of items.
i m created a button remove to remove list items.
i m select multiple items from list than click on remove button.
than all selected ites are not delete.
i m using this::
for (int n=0;n < m_List.GetItemCount();n++)
{
if(m_List.GetItemState(n, LVIS_SELECTED) == LVIS_SELECTED)
{
m_List.DeleteItem(n);
}
}
please help me for this thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
Of course: if you remove a specific item in your list, all items that follow will have another index (one lower). For instance if you remove the first item (index=0), then the index of the second item will change from 1 to 0 (because it become the first one).
You should reverse your loop: start from the end of the list to the begining.
|
|
|
|
|
But why all (not some) selected ites are not deleted?
|
|
|
|
|
Thanks its works.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|