So, I'm new to C++ (brand new), and as an assignment I have to write a class that acts as an array wrapper. Since I'm so new, I'm unsure whether my overloaded operators and such work, so if anyone could be so kind as to tell me how my code looks:
This would be the header:
class MyArray
{
private:
string* sList;
unsigned int size;
public:
MyArray(const unsigned int size = 1);
MyArray(const MyArray &toCopy);
MyArray& operator=(const MyArray& toAssign);
~MyArray();
//MyArray& operator+
string& operator[](const int index);
const int size();
};
And this would be the underlying code:
MyArray::MyArray(const unsigned int initSize)
: size(initSize)
{
sList = new string[initSize];
return;
}
MyArray::MyArray(const MyArray &toCopy)
: size(toCopy.size)
{
if(toCopy.sList)
{
sList = new string[size];
for(int a=0; a<size; a++){
strcpy(sList[a], toCopy.sList[a]);
}
}
else sList = NULL;
return;
}
MyArray& operator=(const MyArray& toAssign)
{
if(this != &toAssign)
{
if(sList)
{
delete [] sList;
}
size = toAssign.size;
if (toAssign.sList)
{
sList = new string[size];
for(int a=0; a<size; a++){
strcpy(sList[a], toCopy.sList[a]);
}
}
else
{
sList = NULL
}
}
}
MyArray::~MyArray()
{
delete [] sList;
return;
}
string& MyArray::operator[](const int index)
{
return sList[index];
}
const int MyArray::size()
{
return this.size;
}
The operator+ function still needs to be written, but I want to make sure what I have makes sense before I proceed.
How do you want your code (i.e. the class you are authoring) to be used by other programmers (including you)?
Write an example program to demonstrate the use of your class.
An example program serves as a rudimentary test set. You can start as the following.
int main() {
MyArray arr1( 5 );
MyArray arr2( arr1 );
}
Have you thought about how user code will put (string?) elements into the MyArray class?
There are couple of other issues with the current version of the code. That is okay to begin with, but it is important for you to learn to test your own code; you need to learn the skill where you have some basic confidence in your own code (not necessarily perfect code) because you cannot always ask somebody else to test your code.
Suggestion: Since you mentioned that you are new, I would suggest you to build a array wrapper class for int's first. This is because, managing strings has some extra challenges than managing ints :-). Once you do that, you can easily do it for strings.
There is a naming conflict between size and size()
Copy C++ strings using =, not strcpy (which is for char*)
Missing MyArray:: in definition of operator=
toCopy should be toAssign in operator=
Missing semicolon after sList = NULL
Missing return *this; at the end of operator=
In defintion of size(), this.size should be size, this->size or (*this).size
All of these mistakes will be discovered by a compiler (you may need to enable warnings for the missing return to be reported; on g++ use the -Wall flag). It is just a matter of understanding the compiler's error messages and knowing how to fix the problems.
Related
My experience so far is mostly in Java but i'm trying to learn C++ and i'm still struggling to understand pointers, overloads etc. Forgive me about the title but i didn't know how to present my problem. Anyway i came across this exercise while studying and i would like some help. The exercise provides a class Container and a main() and i'm supposed to extend the Class and make the main() work to provide the proper result.
template <typename T>
class Container
{
protected:
T * storage;
size_t num_items;
size_t storage_size;
public:
virtual void operator += (const T item) { }
virtual ~Container() { }
Container() : storage(nullptr), num_items(0), storage_size(0) { }
T operator [] (size_t index)
{
if (num_items==0)
return T(0);
return storage[index<num_items?index:num_items];
}
inline size_t size() { return num_items; }
};
void main(int argc, char* argv[])
{
Container<long> * store = new Container_ex(); // I'm suppose to fill this gap
---------------
size_t num_data;
cin >> num_data;
for (size_t i=0; i<num_data; i++)
{
long item;
cin >> item;
*store+=item;
}
for (size_t i=0; i<store->size(); i++)
std::cout << (*store)[i] << " ";
std::cout << std::endl;
delete store;
}
So far i've done this. The problem lies when i try to overload += cause i get an exception error (this->storage was nullptr) but in any case i don't understand the use of storage completely in this exercise so i would appreciate any help.
class Container_ex : public Container<long> {
public:
Container_ex() : Container() {}
~Container_ex() { delete[] storage; }
void operator += (const long item)
{
*storage = const_cast<long&>(item);
*storage = *storage + item;
num_items = num_items + 1;
storage_size = storage_size + 1;
}
};
Your pointer needs to point on a valid memory address before you can do anything with it. If it doesn't, it leads to undefined behavior that can end in a crash of your application, for example.
But when do you need to do it ? Certainly not in the operator overload. When you create an object, you must ensure that it is in a valid state. It means either setting it to nullptr so it is a null pointer, or allocating memory for it.
If you do it in operator overload, how can you be sure that operator overloading will be used ? Maybe the user just wants doing []. So you have to make a constructor allocating memory for your pointer. Or, better, use smart pointers that will save your life and provent headaches, especially if you come from a language where there is no explicit pointer like Java.
Finally, there are some great books to learn C++ better than the course you use.
Programming: Principles and Practice Using C++ by Bjarne Stroustrup, the creator of C++. Assume no previous experience in programming. Check to take the updated version for C++11 | C++14.
C++ Primer by Stanley Lippman, Josée Lajoie and Barbara E. Moo. ~1k pages, full of details and explanations on C++11. Good for beginners with a previous experience in programming.
I encountered an issue that I have been trying to solve for some time, and simply couldn't do it. Here is the scenario:
1) I have an Template array class that look something like this
//code taken from http://www.learncpp.com, much appreciation for Alex
#ifndef ARRAY_H
#define ARRAY_H
#include <assert.h> // for assert()
template <typename T>
class Array {
private:
int m_nLength;
T *m_ptData;
public:
Array() {
m_nLength = 0;
m_ptData = 0;
}
Array(int nLength) {
m_ptData= new T[nLength];
m_nLength = nLength;
}
~Array() {
delete[] m_ptData;
}
void Erase() {
delete[] m_ptData;
m_ptData= 0;
m_nLength = 0;
}
T& operator[](int nIndex) {
assert(nIndex >= 0 && nIndex < m_nLength);
return m_ptData[nIndex];
}
int GetLength() { return m_nLength; }
friend ostream& operator<<(ostream& out, const Array<T>& n) {
for(int i=0; i<n.m_nLength; i++) {
if(i) it << "\n";
it << n[i];
}
return it;
}
};
#endif
2) And this is the class I tried making array of and how I did it (it has dynamic memory allocation)
class Tune {
char* artist;
char* song;
public:
explicit Tune(const char* a, const char* s) {
artist = new char [strlen(a)+1]; strcpy(artist, a);
song = new char [strlen(s)+1]; strcpy(song, s);
}
...
#include "Array.h"
void main() {
Array<Tune> tunes(5); //Array of 5 elements
}
error C2512: 'Tune' : no appropriate default constructor available
1> c:\x\x\x\visual studio 2010\projects\x\x\array.h(26) : while
compiling class template member function 'Array<T>::Array(int)'
1> with
1> [
1> T=Tune
1> ]
1> c:\x\x\x\visual studio 2010\projects\x\x\main.cpp(10) : see reference to
class template instantiation 'Array<T>' being compiled
1> with
1> [
1> T=Tune
1> ]
3) Then I remembered that I could solve that issue with something like this (without using my array template class):
void main() {
Tune **tunes = new Tune*[5];
...
}
I would like to know is this the solution and how do I create Array of pointers using my template array class, and second (lest say I have overriden operator<<), how to I print one or all of the elements of the array.
The full program is huge, this is the piece of it. Most of the code is under comments, so the issue is isolated.
I am pretty stuck and this project means a lot to me, but I am an inexperienced programmer so I find it hard to handle issue like this. Thanks for help in advance.
Cheers!
First of all please show the full error message. Secondly it is not clear what is MyType and whether it has the default constructor.
If MyType is for example some arithmetic type then the code below will be compiled without errors.
#include "Array.h"
int main() {
Array<MyType> data(5); //Array of 5 elements
}
At least class Array has the default constructor though it is not used. As for the type MyType then it can be said nothing because you did not show neither the full error message nor the definition of MyType.
I suggest to check whether MyType has the default constructor.
If you want to create an array of pointers then you should write
Array<MyType *> data(5);
As for this code
void main() {
MyType **data = new MyType*[5];
...
}
then it has nothing common with the problem. Take into account that main shall be defined as having return type int.
EDIT: If do not take into account errors in the definition of class Tune, then it has no default constructor. So you should decide whether you want to create an array of obects of type Tune or an array of pointers to objects of type Tune. I already showed how fo define the array of pointers. Or define the default constructor for class Tune.
How do I create an array of pointers using my template Array class?
If your class needs an alternate way of constructing its elements, you should create another constructor that initializes it the way you wish, similar to std::vector's constructor:
Array( int count, const T& value );
The easiest way to implement this would be to declare m_ptData as a double pointer and initialize it like so:
Array( int count, const T& value ) : m_ptData(new T*[count])
{
for (int i = 0; i < count; ++i)
{
m_ptData[i] = new T(value);
}
}
The best (and most difficult) way would be to use placement-new to initialize it, this is how std::vector does it. You can use it like this:
int main()
{
Array<MyType> data(5, MyType("abc")); // Array of 5 elements,
// all initialized to MyType("abc")
}
How do I print one or all of the elements of the array?
The inserter operator<<() should be used to print the entire array, so making it do something like printing only some of the elements would be a bit confusing to maintainers of your code. As alternatives, you can create a stream manipulator if you wish to customize output, or you can use a member function that takes a count of the numbers you would like to print. Also, your class can have begin() and end() functions which return pointers to the beginning and end of the array, so the user of the class can implement the printing at their discretion. In either cases, looping should be used to print the elements.
The problem occurs in my ToDoList.cpp class file.
ToDoList.cpp:
ToDoList::ToDoList() {
arraySize = 3;
arrayData = 0;
array = new string(arraySize); //error here
}
ToDoList::ToDoList() {
array = new string(todolist.arraySize); //and error here
arraySize = todolist.arraySize;
arrayData = todolist.arraySize;
}
ToDoList.h:
class ToDoList {
public:
ToDoList();
ToDoList(const ToDoList&);
~ToDoList();
void AddItem(string item);
void ListItems();
private:
string* array;
int arraySize;
int arrayData;
};
If you want an array of strings, use
array = new string[arraySize];
You should use
array = new string[arraySize];
and
array = new string[todolist.arraySize];
In your example, you are trying to create an object of std::string class(string(arrraySize)) which is not valid. Compiler is giving the appropriate error to understand it.
EDIT
Your class may be written using std::vector which is efficient correct and easy to understand code.
class ToDoList {
public:
ToDoList() {};
~ToDoList() {};
void AddItem(std::string& item);
void ListItems();
private:
std::vector<std::string> array;
};
void ToDoList::AddItem(std::string& item) {
array.push_back(item);
}
void ToDoList::ListItems() {
for(size_t i = 0; i<array.size(); i++) {
std::cout<<array[i]<<std::endl;
}
}
There's no std::string constructor with just a length. If you want to initialise a string with something in it, you need to say what characters to use. You could use new string(arraySize, fillCharacter), but given this usage, maybe std::vector may be more appropriate.
Edit: the extra details shows you are trying to do something different. So see the other answers. However it looks like you are trying to re-invent a vector<string> , so you may find it easier to use a std::vector instead of manually allocating your array of strings.
Hi I have a test tomarrow and can't figure out why subtraction is made on the pointer before checking if the refcount is 0. I've been searching on google but still cant figure it out. So I'm hoping turning to you guys :) would help.
Easyiest is too just show you the code, I've marked the lines with comments, so here it is:
This is the class StringRep that has pointers to it for counting pointerref to it,
struct StringRep{
int size; // amount of chars incl. EOL \0-tecken
char* chars; // Pointer to char
int refCount; // Amount of String-variables
};
And this is class String that uses the StringRep,
class String{
public:
String(char* str);
String(const String& other);
~String();
const String& operator=(const String& rhs);
char get(int index) const { return srep->chars[index]; }
void put(char ch, int index);
private:
StringRep* srep;
};
String::String(const String& other):srep(other.srep){
srep->refCount++;
}
String::~String(){
if (--srep->refCount == 0){ //why --srep here?
delete [] srep->chars;
delete srep;
}
}
const String& String::operator=(const String& rhs){
if (srep != rhs.srep){
if (--srep->refCount == 0){ //why --srep here?
delete [] srep->chars;
delete srep;
}
srep = rhs.srep;
srep->refCount++;
}
return *this;
}
void String::put(char ch, int index){
if (srep->refCount > 1){ //Why not --srep here?
StringRep* tmpRep = new StringRep;
tmpRep->refCount = 1;
tmpRep->size = srep->size;
tmpRep->chars = new char[tmpRep->size];
std::strcpy(tmpRep->chars, srep->chars);
--srep->refCount;
srep = tmpRep;
}
srep->chars[index] = ch;
}
This is all info I have on the example question for the test, I know that --spek points to the object before spek, but cant figure out the logic behing checking if what is pointed at before now is 0 then its okey to delete, or to copy, but why? As I said I've searched the webb and have found some answers to help me understand the functions of the pointer and the subtraction etc, it more the logic that is confusing.
Best regards
Because of operator precendence, --srep->refCount is not decrementing srep, but the refCount member.
So, the code is decrementing the refCount, and if it comes down to 0, it can assume that the last reference to the object is being destroyed.
--srep->refCount
is parsed as
--(srep->refCount)
because prefix decrement has lower priority than -> (however, postfix decrement has the same priority as ->). Always use parens in your own code!
This is a homework assignment. The Field container was the assignment from a week ago, and now I'm supposed to use the Field container to act as a dynamic array for a struct NumPair which holds two char * like so:
struct NumPair
{
char *pFirst, *pSecond;
int count;
NumPair( char *pfirst = "", char *psecond = "", int count = 0)
: pFirst(strdup(pfirst)), pSecond(strdup(psecond)), count(count)
{ }
NumPair( const NumPair& np )
: count(np.count), pFirst(strdup(np.pFirst)), pSecond(strdup(np.pSecond))
{ }
NumPair& operator=( const NumPair& np )
{
if(this != &np)
{
pFirst = strdup(np.pFirst);
pSecond = strdup(np.pSecond);
count = np.count;
}
return *this;
}
and the Field container
Field<NumPair> dict_;
The homework requires the use of char *, and not string, so that we can get better with all this low-level stuff. I've already had some question about char to wchar_t conversions, etc.
Now I have a question as to whether or not I'm destructing the NumPair properly. The scenario is as follows:
1) Field destructor gets called
template <class T>
Field<T>::~Field()
{
delete[] v_;
}
2) Delete calls the destructor of every element NumPair in v_;
~NumPair()
{
free(pFirst);
free(pSecond);
}
Is this okay? I haven't really read too many articles about mixing and matching elements created on the heap and free-store as we wish. I figure as long as I don't use delete on an improper malloc'ed element, I should be fine.
However, I don't know the entire intricacies of the delete command, so I'm wondering whether or not this is valid design, and what I could do to make it better.
Also, of course this isn't. I'm getting an error of the type:
This may be due to a corruption of the heap and points to dbgheap
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) ); // Here
}
Again, how could I improve this without the use of string?
FIELD CTOR/Copy Ctor/Assignment
template <class T>
Field<T>::Field()
: v_(0), vused_(0), vsize_(0)
{ }
template <class T>
Field<T>::Field(size_t n, const T &val)
: v_(0), vused_(n), vsize_(0)
{
if(n > 0)
{
vsize_ = 1;
while(vsize_ < n)
vsize_ <<= 1;
v_ = new T[vsize_];
std::fill(v_, (v_ + vused_), val);
}
}
template <class T>
Field<T>::Field(const Field<T> &other)
: v_(new T[other.vsize_]), vsize_(other.vsize_), vused_(other.vused_)
{
std::copy(other.v_, (other.v_ + other.vused_), v_);
}
template <class T>
Field<T>& Field<T>::operator =(const Field<T> &other)
{
this->v_ = other.v_;
this->vused_ = other.vused_;
this->vsize_ = other.vsize_;
return *this;
}
FIELD MEMBERS
T *v_;
size_t vsize_;
size_t vused_;
Your copy constructor (of Field<>) seems OK, but the operator= is problematic.
Not only does it leak memory (what happens to the original v_?), but after that, two instances of Field<> hold a pointer to the same block of memory, and the one that is destructed first will invalidate the others v_ - and you can't even tell whether that has happened.
It's not always easy to decide how to deal with operator= - some think that implicit move semantics are okay, but the rest of us see how that played out with the majority of people, with std::auto_ptr. Probably the easiest solution is to disable copying altogether, and use explicit functions for moving ownership.
Your string handling in NumPair looks ok (strdup + free) and your Field container delete[] looks okay but it's hard to say because you don't show what v_ is.
eq mentions in a comment that you should also beware of how you are copying NumPairs. By default, C++ will give you an implicit member-wise copy constructor. This is where a RAII type like std::string makes your life easier: Your std::string containing struct can be copied without any special handling on your part and memory referenced in the string will be taken care of by the string's copy. If you duplicate your NumPair (by assigning it or returning it from a function for example) then the destruction of the temporary will free your strings out from under you.
Your copy constructor for Field just copies the pointers in v_. If you have two copies of a Field, all of the NumPairs in v_ will be deleted when the first Field goes out of scope, and then deleted again when the second one does.