hello,
I have to implement a sequence class, however the header file is
predefined -
class sequence
-
{
-
public:
-
// TYPEDEFS and MEMBER CONSTANTS
-
typedef double value_type;
-
typedef std::size_t size_type;
-
static const size_type DEFAULT_CAPACITY = 30;
-
// CONSTRUCTORS and DESTRUCTOR
-
sequence(size_type initial_capacity = DEFAULT_CAPACITY);
-
sequence(const sequence& source);
-
~sequence( );
-
// MODIFICATION MEMBER FUNCTIONS
-
void resize(size_type new_capacity);
-
void start( );
-
void advance( ); //set the current_index to the next number in
-
the array
-
void insert(const value_type& entry); //insert before the
-
number with the current index
-
void attach(const value_type& entry); //insert after the number
-
with the current index
-
void remove_current( );
-
void operator =(const sequence& source);
-
// CONSTANT MEMBER FUNCTIONS
-
size_type size( ) const;
-
bool is_item( ) const;
-
value_type current( ) const;
-
private:
-
value_type* data; //das array mit den zahlen drinnen
-
size_type used; //wieviele zahlen in dem array stehen
-
size_type current_index;
-
size_type capacity;
-
};
-
Unfortunately, I guess something is wrong with the copy constructor, or
the insert, attach or resize function: -
sequence::sequence(const sequence& source)
-
{
-
data = new value_type[source.capacity];
-
capacity = source.capacity;
-
used = source.used;
-
current_index = source.current_index;
-
copy(source.data, source.data + used, data);
-
}
-
Here the insert, attach and resize function: -
-
void sequence::insert(const value_type& entry)
-
{
-
if(used == capacity)
-
resize(used);
-
-
if(!(is_item()))
-
current_index = 0;
-
-
for(int i = used; i current_index; i--)
-
{
-
data[i] = data[i-1];
-
}
-
data[current_index] = entry;
-
++used;
-
}
-
-
void sequence::attach(const value_type& entry)
-
{
-
if(used == capacity)
-
resize(used);
-
-
if(current_index >= used)
-
{
-
start();
-
advance();
-
advance();
-
}
-
if(used!=0)
-
{
-
for(int i = used; i current_index+1; i--)
-
{
-
data[i] = data[i-1];
-
}
-
data[current_index+1] = entry;
-
current_index++;
-
}
-
else
-
{
-
data[current_index] = entry;
-
}
-
++used;
-
}
-
-
void sequence::resize (size_type new_capacity)
-
{
-
value_type* larger_array;
-
-
if(new_capacity < used)
-
new_capacity = used;
-
-
larger_array = new value_type[new_capacity];
-
copy(data, data + used + current_index, larger_array);
-
delete[] data;
-
data = larger_array;
-
capacity = new_capacity;
-
}
-
Has anybody an idea what went wrong here? I am working on the problem
fixing for such a long time but I haven't found the error yet...:((
pat
Sep 27 '06
14 4805
pat270881 wrote:
>
I have already debug the program and the error occurs here:
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);
but I simply don't know why...? :((
No, that's the symptom. Mlimber is telling you that if you see
bad_alloc, that means that new is failing. That's your potential
cause. Don't fixate on what you think is wrong. If you were certain,
you wouldn't have come here. If you ask someone's advice, then listen
to it.
If you're seeing bad_alloc somewhere, that means that new is failing
(not that you're overwriting heap, though that may happen as well). Put
a break point on your new statements, and check that you're passing sane
values as the sizes.
pat270881 wrote:
I have already debug the program and the error occurs here:
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near
middle." << endl;
original.insert (2);
but I simply don't know why...? :((
You are making it too hard to help you. Right now if I wanted to debug
your program, I'd have to collect together several pieces of it from
multiple posts, and then I'd still have to write some code of my own to
have anything that would compile and run.
Your best bet is to post a complete program that demonstrates your
problem. This means that I could copy and paste directly from your
post into my editor, compile, and see your problem (Hint: a complete
program has a main function, and you haven't posted one anywhere).
--
Alan Johnson
pat270881 schrieb:
void sequence::inser t(const value_type& entry)
{
if(used == capacity)
resize(used);
if(!(is_item()) )
current_index = 0;
for(int i = used; i current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = entry;
++used;
}
[...]
Has anybody an idea what went wrong here? I am working on the problem
fixing for such a long time but I haven't found the error yet...:((
resize(used);
should be
resize(used+1);
Otherwise you are accessing data[used] in the for-loop, which is one past
the last element.
Why don't you use std::vector?
--
Thomas http://www.netmeister.org/news/learn2quote.html
Thomas J. Gritzan wrote:
Why don't you use std::vector?
Because this is homework.
Cheers! --M
Sorry, that I cost you so much nerves, that was the error - when a
handed over the used to the resize function I haven't increased the
used, now I use 2*used in insert and attach and now it works :))
Thank you very much for your help!!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chinook |
last post by:
OO approach to decision sequence?
---------------------------------
In a recent thread (Cause for using objects?), Chris Smith replied with (in
part):
> If your table of photo data has several types of photos, and you find
> yourself saying
>
> if is_mugshot:
|
by: Neil Waldie |
last post by:
I am trying to code a class for use with the XMLSerializer that can produce
XML valid for the following schema (fragment):
<xs:element name="Group">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="SubElement1" />
<xs:element minOccurs="0" maxOccurs="1" name="SubElement2" />
<xs:element minOccurs="0" maxOccurs="1" name="SubElement3" />
</xs:sequence>
|
by: Eric Lilja |
last post by:
I'm looking at an assignment where the students are expected to write a
class with a given purpose. According to the assignment, you should be
able to do this with instances of the class:
++instance_of_class;
instance_of_class++;
(++instance_of_class)++; // Isn't this invalid C++?
but not:
++(instance_of_class++);
|
by: Defcon2030 |
last post by:
<bHey, can someone help me with this? I've been working on it for a
few days now, and my head's starting to spin... </b>
// FILE:ex1_imp.cxx
//
//
//
// CLASS IMPLEMENTED: sequence (see ex1.h for documentation)
// INVARIANT for the sequence class:
// 1. The number of items in the sequence is in the member variable
|
by: davydany |
last post by:
Hey guys...a n00b Here for this site.
I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the array data from 0 to3, I get this {13, JUNK VALUE, 17,38, 18} and JUNK VALUE is like 1.8e831 or something like that.
This happens when I use the attach() function and use the current() function to display the values at data
I really want to...
| |
by: =?iso-8859-2?q?Seweryn_Habdank-Wojew=F3dzki?= |
last post by:
Hi
There is an example of the code below (mostly written by P. Bindels).
In that example is strongly highlighted that very important is the
sequence
of compiled code. In C++ could be assume that such a sequence exists.
But the question is if it is a feature or it is a bug?
Especially in the scope of that example we could observe that
templates
can be somehow separated from its specializations.
|
by: rval |
last post by:
I have this schema while teh following sequence
<complexType name="SetOfTasks" mixed="true">
<sequence>
<choice minOccurs="1" maxOccurs="unbounded">
<element minOccurs="0" ref="tns:task1" />
<element minOccurs="0" ref="tns:task2" />
<element minOccurs="0" ref="tns:task3" />
<element minOccurs="0" ref="tns:task4" />
<element minOccurs="0" ref="tns:task5" />
|
by: Anan18 |
last post by:
Hello sir,
I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test program to check the correctness of the sequence class. I cannot figure out what the problem is and keep looking back and forth at the textbook and its not helping, can someone please please help me , i cannot figure out the errors in the...
|
by: schickb |
last post by:
I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.
Are there reasons for not supporting this generally? I realize not all
iterators would have the __index__ method,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |