The compiler only displays a portion of the output and then it exits this is what it compiles:

===============================================
Operations on Empty lists: 
===============================================
Size = -886911509
Length = 0
empty list: 
Sum of two empty lists: 
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
stringlist(2449) malloc: *** error for object 0x1001002d8: incorrect checksum for freed object - object was probably modified after being freed.

this is the .cpp file

#include <iostream>
#include <string>
#include <cstdio>
#include "stringlist.h"
using namespace std;


StringList::StringList( int size )
{    int value;
	size=10;
	this->size=size;
	this->size=value;
	data = new string[size];
	for (int i =0; i< size; i++)
    {
        data [i] = value;
    }
	
}
StringList:: StringList( const StringList &list2 )
{
	size = list2.size;
    data = new string[size];
	      
        if(data!=NULL)
		{
			for (int i=0; i<size; i++) {
				data[i]=list2.data[i];
			}
     
}        
}
StringList& StringList::operator=( const StringList &list_2 )
{
	if ( this != &list_2 ) 
	{
		delete[]data;
		data = new string[size];
		size=list_2.size;
	}
	for (int i = 0; i<size; i++)
	{
		data[i]=list_2.data[i];
	}
	return *this;
}

StringList::~StringList()
{
	delete[] data;
}
int StringList:: askSize() const
{
	return size;
}
int StringList::askLength() const
{
	int length=0;
	return length;
}
void StringList::addToFront( string s1 )
{
	string value;
	value=s1;
	data=new string[size];
	for(int ii=0; ii < size; data[ii++])
		
		s1 = value;
	
 
}
void StringList:: insert( string s1, int pos )
{
	data = new string[size];
	
	for(int ii=0; ii < size; data[ii++])
		
		s1= pos;
	
}

void StringList::append( string s1 )
{
	
	for(int ii=0; ii < size; ++ii)
	{
		data[ii]= s1;
	}
	


}

void StringList:: remove( string s1 )
{
	delete data;
	size=0;
	data=NULL;
}

void StringList::remove( int pos )
{
	delete data;
	pos=0;
	data=NULL;
	
}

int StringList::find( string s1 ) const
{
	int index;
	if(index >= 0 && index < size)
	{	
		data[index] = s1;
	}
	

}

void StringList::print( ) const
{
	int start_index;
	int end_index ;
	if (start_index >= 0 && end_index < size)
	{
		for (int i = start_index; i <= end_index; i++)
			
			cout << i << "," << data[i]<< ")"<< endl;
		
	}
}


StringList StringList:: operator+( const StringList &list_2 ) const
{
	return StringList(size+list_2.size);
}

StringList StringList:: operator-( const StringList &list_2 ) const
{
	return StringList(size-list_2.size);
}

bool StringList:: operator==( const StringList &list_2 ) const
{
	return (size==list_2.size);
}

bool StringList:: operator!=( const StringList &list_2 ) const
{
	return (size!=list_2.size);
}

Recommended Answers

All 3 Replies

There are many problems in your code. I would suggest starting again with a simpler set of functions that you want your class to perform. Also, if you include the header file and the main file, it would be easier for people to see what is going on.

So, taking your constructor:

StringList::StringList( int size )
{    int value;
	size=10;
	this->size=size;
	this->size=value;
	data = new string[size];
	for (int i =0; i< size; i++)
    {
        data [i] = value;
    }
 
}

In psuedo-code, this is doing the following:

  • Accept an integer argument called size
  • Make an integer called value (it should be assumed to contain a random number)
  • Assign the value 10 to size (erasing whatever the user had passed in as an argument)
  • Assign the value of the local size variable (which is 10 ) to the size member variable
  • Assign the value of value to the size member variable (remember, value has an unknown value!)
  • Allocate an amount of memory big enough to store size string objects (which is the local size , not the member variable size , so it has a value of 10 , not the (now) unknown value of the size member variable).
  • For each of the new elements in the allocated memory, try and assign value . (this is assigning an int to a string , which is almost certainly not what you want to do)

I think that what you wanted to achieve in this constructor is this:

StringList::StringList( int newSize )
{
   size = newSize;
   data = new string[size];
}

This gives you an array of newSize strings .

Try simplifying the scope of your class and reposting what you have, and maybe we can help get to where you want to be.

okay i made the changes, but what exactly do you mean by simplifying the scope?
but here is my revised code

#include <iostream>
#include <string>
#include <cstdio>
#include "stringlist.h"
using namespace std;


StringList::StringList( int newSize )
{     size = newSize;
	 data = new string[size];
	
}
StringList:: StringList( const StringList &list2 )
{
	size = list2.size;
    data = new string[size];
	      
        if(data!=NULL)
		{
			for (int i=0; i<size; i++) {
				data[i]=list2.data[i];
			}
     
}        
}
StringList& StringList::operator=( const StringList &list_2 )
{
	if ( this != &list_2 ) 
	{
		delete[]data;
		data = new string[size];
		size=list_2.size;
	}
	for (int i = 0; i<size; i++)
	{
		data[i]=list_2.data[i];
	}
	return *this;
}

StringList::~StringList()
{
	delete[] data;
}
int StringList:: askSize() const
{
	return size;
}
int StringList::askLength() const
{
	int length=0;
	return length;
}
void StringList::addToFront( string s1 )
{
	string value;
	value=s1;
	data=new string[size];
	for(int ii=0; ii < size; ii++)
		
		s1 = value;
	
 
}
void StringList:: insert( string s1, int pos )
{
	data = new string[size];
	
	for(int ii=0; ii < size; ii++)
		
		pos=size;
	
}

void StringList::append( string s1 )
{
	
	for(int ii=0; ii < size; ++ii)
	{
		data[ii]= s1;
	}
	


}

void StringList:: remove( string s1 )
{
	delete data;
	size=0;
}

void StringList::remove( int pos )
{
	delete data;
	pos=0;
	
}

int StringList::find( string s1 ) const
{
	int index;
	if(index >= 0 && index < size)
	{	
		data[index] = s1;
	}
	

}

void StringList::print( ) const
{
	int start_index;
	int end_index ;
	if (start_index >= 0 && end_index < size)
	{
		for (int i = start_index; i <= end_index; i++)
			
			cout << i << "," << data[i]<< ")"<< endl;
		
	}
}


StringList StringList:: operator+( const StringList &list_2 ) const
{
	return StringList(size+list_2.size);
}

StringList StringList:: operator-( const StringList &list_2 ) const
{
	return StringList(size-list_2.size);
}

bool StringList:: operator==( const StringList &list_2 ) const
{
	return (size==list_2.size);
}

bool StringList:: operator!=( const StringList &list_2 ) const
{
	return (size!=list_2.size);
}

okay i made the changes, but what exactly do you mean by simplifying the scope?

I mean, reduce the number of different things that you want your class to be able to do. For example, you have 2 constructors, 5 operator overloads and 9 other member functions. Maybe try implementing a class that looks like this:

class StringList
{
public:
    StringList();                                  // Default constructor
    StringList( unsigned newSize );                // Initialise with a number of elements
    StringList( const StringList& original );      // Copy constructor

    void Append( const std::string& newString );   // Add a new string to the list
    void Print() const;                            // Print the list to stdout

private:
    
    unsigned m_size;                               // Store the size of the list
    std::string* data;                             // The actual data in the list
};

your current append function looks like this:

void StringList::append( string s1 )
{
    for(int ii=0; ii < size; ++ii)
        data[ii]= s1;
}

This function will go through the first size elements of data and set them all to the value of s1 . This is probably not what you wanted the function to do. Consider what you have to do to append an item to the list:

  • Allocate enough memory to hold the current size + 1
  • Copy the existing elements into the new memory
  • Add the new string to the end of the data list
  • Deallocate the old memory

Your print function looks like it might just about do what you want:

void StringList::print( ) const
{
    int start_index;
    int end_index ;
    if (start_index >= 0 && end_index < size)
    {
        for (int i = start_index; i <= end_index; i++)
            cout << i << "," << data[i]<< ")"<< endl;
    }
}

The only thing is that start_index and end_index are not initialised, so this will have the effect of sometimes printing the list and sometimes not (depending on whether they happen to satisfy the conditions in the if statement.

Once you can successfully instantiate, copy, append and print with the class, then you could start to add some more features. The important thing is to check that each thing works as you add it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.