errors!

Pages: 12
ListInterface<> is an abstract class, and nextlist is just a pointer.

To copy LinkedList<>:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    LinkedList<std::string> mylist ;

    mylist.insert(1, "Apples");
    mylist.insert(2, "Oranges");
    // etc.

    LinkedList<std::string> lst2 = mylist ; // create a copy

    // verify that it is copied
    for( int i = 1 ; i <= lst2.getLength() ; ++i ) 
        std::cout << lst2.getEntry(i) << '\n' ;
}
So if I were to declare it in dynamic memory, how would I work the pointers to assign the values if it looked like this:
1
2
3
4
5
6
7
8
9
count =1;
LinkedList<string>* mylist = new LinkedList<string>();
while (getline(infile, tempstr))
		{
			mylist->insert(count, tempstr);
			count++;
		}
LinkedList<string>* nextlist = new LinkedList<string>();
// Assignment  

Raw pointers (not exception safe as written):
1
2
3
4
5
6
7
8
9
LinkedList<string>* mylist = new LinkedList<string> ;

// insert stuff into mylist with mylist mylist->insert()

LinkedList<string>* nextlist = new  LinkedList<string>( *mylist ) ; // copy construct

// ...
delete nextlist ;
delete mylist ;


Smart pointers:
1
2
3
4
5
auto mylist = std::make_unique< LinkedList<string> >() ;

// insert stuff into mylist with mylist->insert()

auto nextlist = std::make_unique< LinkedList<string> >( *mylist ) ; // copy construct 
Wow I slacked there using raw pointers and wasn't thinking about that. I really appreciate your help, you are a life saver.
For this either my code in main is wrong or something in my function is wrong. I have been playing with it for some time and it wont work. (I tried multiple ways to pass in an object to satisfy the displaylist function from linkedlists. I know I can loop and display it manually with getEntry with positions leading up to mylist->getLength() but I figure I should use the function in place ><
Last edited on
1
2
3
4
5
6
7
8
// void displayList(ItemType aList)
void displayList() const // display the contents of this list
{
    for( std::size_t position = 1; position <= getLength() ; ++position )
    {
        std::cout << getEntry(position) << '\n' ;
    }
}


And then:
1
2
// mylist->displayList(*mylist);//It is complaining here. 
mylist->displayList() ; 


And searchFun should be
1
2
3
4
5
6
7
8
9
int searchFun( const LinkedList<std::string>& aList, const string& searchItem )
{
	for( int count = 1; count <= aList->getLength() ; ++count )
	{
		if( aList->getEntry(count).find(searchItem) != string::npos ) return count ;
	}
	
	return -1 ; // not found
}
Last edited on
Ah not sure why there was a parameter on the display function, maybe from before the program was converted. That makes more sense.
Topic archived. No new replies allowed.
Pages: 12