I'm sorry for making another topic, but my brain just went kapowie!; plus I've burned myself out from studying for my midterm tomorrow.
For my copy constructor, I honestly don't think I'm doing this right...is there a workaround OTHER than using the function provided in the <algorithm> library? Otherwise, I guess I'll have to resort to it.
My program crashes when I execute it and I'm guessing it's due to the copy constructor. (I'm also only posting smaller portions of my code to save your eyes.)
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cstdlib>
#include "stringArray.h"
usingnamespace std;
int main( )
{
stringArray sa1, sa2(100);
sa1.addString("abcdefg");
sa1.addString("lmnopq");
sa1.printStringArray( );
sa2.addString("1234");
// This line causes the crash
stringArray sa3(sa1);
return EXIT_SUCCESS;
}
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include "stringArray.h"
usingnamespace std;
// Constructor
stringArray::stringArray (const size_t max_size)
{
// Sets the number of items/data in the bag to 0
count = 0;
// Sets the bag's capacity size to whatever the parameter is
capacity = max_size;
// Dynamically creates space for strings in the bag
data = new stringArray::str[max_size];
}
// Copy constructor
stringArray::stringArray (const stringArray& source)
{
// Assigns all member values to whatever
count = source.count;
capacity = source.capacity;
// Maybe call the overload function? (=)
// std::copy(source.data, source.data + source.count, data);
for (int i = 0; i < capacity; i++)
{
data[i] = source.data[i];
}
}
#ifndef STRINGARRAY_H_INCLUDED
#define STRINGARRAY_H_INCLUDED
class stringArray
{
public:
// Constructor and a copy constructor
stringArray (const size_t max_size = DEFAULT_SIZE);
stringArray (const stringArray& source);
// Destructor
~stringArray();
// Adds a new string in the bag
// Returns true if successful; false otherwise
bool addString(constchar new_string[ ]);
// Removes one occurrence of old_string from the bag
// Returns true if successful; false otherwise
bool removeString(constchar old_string[ ]);
// Removes all occurrences of old_string from the bag
// Returns number of strings removed
// May need to be changed to an int
size_t removeAll(constchar old_string[ ]);
// Substitutes/replaces one occurrence of old_string with new_string
// Returns true if successful; false otherwise
bool substitute(constchar old_string[ ], constchar new_string[ ]);
// Substitutes/replaces all occurrences of old_string with new_string
// Returns number of strings replaced
size_t substituteAll(constchar old_string[ ], constchar new_string[ ]);
// Returns total number of strings in the bag
size_t size( ) const;
// Returns true of cur_string is in the bag; false if not
bool is_string(constchar cur_string[ ]) const;
// Returns number of occurrences of cur_string
size_t occurrences(constchar cur_string[ ]) const;
// Prints out all strings in the object
// One string per line
void printStringArray( ) const;
// Prints all strings out in the object
// One string per line
// Sorted order from least to greatest
void printSortedStringArray( ) const;
// Sets the default_size/capacity of the bag to 20
staticconst size_t DEFAULT_SIZE = 20;
// Typedef stuff
typedefconstchar* str;
private:
// Member variables here
// count = The number of items in the bag
size_t count;
// capacity = The number of items that can be stored in the bag
size_t capacity;
// data = The item in the bag
str* data;
};
#endif // STRINGARRAY_H_INCLUDED
I realized that, but how do I go about fixing it? That's what I'm having trouble with. I know it's got to do something with dereferencing it, but at the same time, I don't know how it should look.
It doesn't actually have anything to do with dereferencing. You would have to use new, like you did in your other constructor that takes the size as a parameter. You would just be getting the size from a different variable now instead.
But what if I wanted to add more items to the container that I'm creating? Hence why I'm filling it up to the container's capacity. Count is just how many items the container has, and capacity is...well, capacity.
My TA suggested this in recitation last time, so that's why I included it in my code.
Also, as I said earlier...I don't think we're allowed to use std::copy(). This is why I'm asking for the algorithm for this.
I actually found out that my copy constructor isn't causing my program to crash...it's the constructor that's causing it to crash. I think I was looking at the wrong line last night.
I don't see how the constructor could cause a crash. The typedef that you have made is extraordinarily confusing. I recommend that you delete that typedef and just refer to char*. it took me a few minutes to figure out what in the heck the str type is.
Designing a custom string class was a challenge for some of the greatest programmers in the world. Good luck. You've got a long way to go.
Yeah, his point was that it was confusing to have that there. IMHO, char* doesn't really need to be typedef'd, although if you REALLY wanted to I would call it c_string or something to make it obvious it isn't related to std::string.