errors!

Pages: 12
Hi I got bumped back in my progress because I am getting a bunch of errors.
Ok I fixed some errors but still complaining about conversion for shared ptrs. I am trying to convert the pointers to smart pointers and use auto where i can but its not working. >< Please help
Last edited on
I'm not sure why you're using shared_ptr since there is no shared ownership.
Get rid of the shared pointers since no pointers are shared.
Line 32 should be Node <ItemType> *getNext() const
Line 80: Function currently doesn't return anything if the "else" clause is executed.
Line 155 should be for (size_t position = 1;
Line 189: The function doesn't return anything if ableToSet is false.
Hi I got bumped back in my progress because I am getting a bunch of errors when testing the linked list class.

Why is it that so many posters seem to think that withholding the details of the errors is more helpful than providing them?

If you actually tell us what the errors are, then that makes it easier for us to help you find them.
Last edited on
I am trying to switch over to smart pointers but still working with them. The else part of getNodeAt is suppose to throw an exception but it was not working. It has no build errors, I need to make the exception handler work (the commented out areas). I tested it but I cant get the exception part to work.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
    shared_ptr<Node<ItemType>> getNodeAt(const int position) const  //Locates a specified node in a linked list.
    {									//@pre position is the number of the desired node.
        if ((position >= 1) & (position <= itemCount)) //position >=1 and position <= itemCount.
        {												//@post the node is found and a pointer to it is returned.
            shared_ptr<Node<ItemType>> curPtr = headPtr;						//@param position the number of the node to locate.
            for (int skip = 1; skip < position; skip++) //@return a pointer to the node at the given position.
                curPtr = curPtr->getNext();
            return curPtr;
        }

        throw PrecondViolatedExcept("invalid position");
    }


I would reiterate that shared_ptr is not appropriate here.
The project is going to expand a lot, I was told I should use them maybe for future needs. Thank you that fixed the exception problem :) . How would I make a copy constructor for the LinkedList class? Id have to take in a reference object and work it to copy the info I am just not sure how to make it work. ><
The project is going to expand a lot

That doesn't make it appropriate.

How would I make a copy constructor for the LinkedList class?

Consider how you build a list without a copy constructor. Follow that procedure in the constructor with the elements of the list that is passed in.
What could I use in place of the shared_ptr ? I need to use smart pointers. So maybe a unique ptr?
I need to use smart pointers.

Why?
I was told I need to for my project by my teacher. I need to intake information from a file, and work it so the user can search by keyword instead of position. I need to make a child class of the linkedlist class to do all the work. Then I have to work main after to do some specifics.
Its nothing like I have done before. I am just lost started off too fast in the class. I am working on the data handling right now but hit another road block. I was reading the info from the file but some lines of info exceed the amount that getline can put into a string, so they are ending up on the next line and not being held in one node as it sits. I really don't want to do character arrays for each line. -Scratch that it was just overflowing on the output screen max of 80 chars it is holding the whole line ><.
Last edited on
I was trying to have it finished by Monday so i can get it looked at and see if its efficient enough and fix errors.
Its Friday at 9:30pm where I am at.
> I was told I need to for my project by my teacher.

Since it appears that move semantics have not been covered as yet, and a smart pointer must be used,
continue to use std::shared_ptr.

To throw an exception(for instance, in getEntry):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	ItemType getEntry(const int position) const //throw(PrecondViolatedExcept)//CHECK FOR ERRORS
	{
		bool ableToGet = (position >= 1) && (position <= itemCount);
		if (ableToGet)
		{
			shared_ptr<Node<ItemType>> nodePtr = getNodeAt(position);
			return nodePtr->getItem();

		}
		else
		{
			std::string message = "getEntry() called with an empty list or ";
			message = message + "invalid position. ";
			throw PrecondViolatedExcept( "invalid position" ) ;
		}
         }

An likewise for the other functions where exceptions are to be thrown.


To catch the exception:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    LinkedList<string> list_impl; // use value sematics
	ListInterface<string>& mylist = list_impl ; // reference

	mylist.insert(1, "Apples");
	mylist.insert(2, "Oranges");
	mylist.insert(3, "Cheese");
	mylist.insert(4, "Tomatoes");
	mylist.insert(5, "Lettuce");

    try
    {
        std::cout << mylist.getEntry( 3 ) << '\n' ; // Cheese
        std::cout << mylist.getEntry( 68 ) << '\n' ; // invalid position, throws
    }
    catch( const PrecondViolatedExcept& e )
    {
        std::cout << "*** error: " << e.what() << '\n' ;
    }
}
Thank you :).
Last edited on
You appear to be on the right track.

Take a break now, have a good night's sleep,; come back with a refreshed mind to try and refine it.
Ok so I am trying to make the copy constructor and am confused because of the position based implementation of the linked list. This is what I have.
1
2
3
4
5
6
7
8
9
10
LinkedList(const LinkedList<ItemType>& aList)
	{
		itemCount = 0;
		shared_ptr<Node<ItemType>> origChainPtr = headPtr;
		if (origChainPtr != nullptr)
		{
			auto newNodePtr = std::make_shared<Node<ItemType>>(aList);

		}	
	}
Not the most efficient, but this should work:
1
2
3
4
5
LinkedList(const LinkedList<ItemType>& aList ) : itemCount(0) // initialise
{
    for( int i = 1 ; i <= aList.getLength() ; ++i ) // note: first position is 1
        this->insert( i, aList.getEntry(i) ) ;
}
Ah I see. >< Thank you. To use I just assign the pointers to each other like:
1
2
ListInterface<string>* nextlist = new LinkedList<string>();
		nextlist = mylist;

??
Pages: 12