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

easy but couldnt see the error

jw
it crashes but why?thanks for the helps.
#include<vector>
#include<iostream>
using namespace std;

template<class Object>
class Stack{
private:
vector<Object>myVec;
int topOfStack;
public:
Stack()
{
topOfStack=-1;
}
void push(Object x)
{
if(topOfStack==myVec.size()-1) {myVec.resize(myVec.size()*3);}//if
stack is full resize it
myVec[++topOfStack]=x;

}
};

void main(){
Stack<int>m;
m.push(5);
m.push(10);
}

Nov 11 '05 #1
5 1392
You keep resizing vector to 0. And you don't need to resize it, there
is push_back method. STL has much better ways to do what you are
trying to do and vector is not the best choice.

Nov 11 '05 #2
On 2005-11-11, jw <ja*****@gmail.com> wrote:
it crashes but why?thanks for the helps.
#include<vector>
#include<iostream>
using namespace std;

template<class Object>
class Stack{
private:
vector<Object>myVec;
int topOfStack;
public:
Stack()
{
topOfStack=-1;
}
void push(Object x)
{
if(topOfStack==myVec.size()-1) {myVec.resize(myVec.size()*3);}//if


What is zero times three?

--
Neil Cerutti
Nov 11 '05 #3
yes i know push_back but we must do it with out using push_back

Nov 11 '05 #4
On 2005-11-11, berkay <be**********@gmail.com> wrote:
yes i know push_back but we must do it with out using push_back


Fool them all and use a deque and push_front, front, and
pop_front. ;-)

--
Neil Cerutti
Nov 11 '05 #5
Neil Cerutti <le*******@email.com> wrote in
news:sl*********************@FIAD06.norwich.edu:
On 2005-11-11, berkay <be**********@gmail.com> wrote:
yes i know push_back but we must do it with out using push_back


Fool them all and use a deque and push_front, front, and
pop_front. ;-)


Or how about std::stack<> ? :)
Nov 11 '05 #6

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

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
16
by: hoggmeister | last post by:
Hi, Im new to C coming from a java background. I having difficulty adjusting to C and was hoping someone could help me with a little simple code to get started. I would like a little program...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
1
by: nmn | last post by:
thank you in advance.. i am trying to retrive data to my txt fields from access tables by using dbcombo i am using DATA1 for the datasource and "car" for the table. i have no problem in listing...
6
cjbrx3115
by: cjbrx3115 | last post by:
Hey everyone, I just bought a new Windows Vista (Home Premium) computer. My old computer, a Windows XP Professional, had all of my old documents, spreadsheets, etc. I was planing on using the Easy...
5
by: emre esirik(hacettepe computer science and enginee | last post by:
/////////////////////// DIVISION FUNCTION //////////////////// void div ( int pol1 , int pol1p , int pol2 , int pol2p) { int r=5,z,i,g,j,k,l,dividend,rising={0}; for (i=0 ; i<6 ; i++)...
2
by: emre esirik(hacettepe computer science and enginee | last post by:
struct guitar active; char *buffer_char3; while(!feof(ptdata)) { fgets(buffer,26, ptdata); fgets(buffer,26, ptdata); fgets(buffer,26, ptdata); fgets(buffer,26, ptdata); fgets(buffer,26,...
4
by: shall | last post by:
I have several pages ( ASP) that were working OK on NT4. We moved it to Server 2003. At random , it appears that the Server.CreateObject fails. I've looked through the code and it looks OK. ...
25
by: Andy B | last post by:
How hard/easy is it to use/learn VB compared to c#?
19
by: eMRe | last post by:
class A { .. }; class B : public A { .. }; int main( ) {
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.