Basically, I'm getting an error when I try to append a CD object to my linked list.

What error? This one:

First-chance exception at 0x550257aa (msvcr100d.dll) in Programming 3 Course Project.exe: 0xC0000005: Access violation reading location 0xfeeefee2.
Unhandled exception at 0x550257aa (msvcr100d.dll) in Programming 3 Course Project.exe: 0xC0000005: Access violation reading location 0xfeeefee2.

My code to append:

        case 1:
                    tempCD.defineCD();
                    cout << "Works" << endl;
                    CDlist.appendNode(tempCD);
                    cout << "Still works." << endl;
                    break;

My linked list's appendNode function code:

//Append List****************************
//***************************************
template <class T>
void TList<T>::appendNode(T newValue)
{
    ListNode<T> *newNode;
    ListNode<T> *nodePtr;

    //Make a new node, store the value.
    newNode = new ListNode<T>(newValue);

    //If head doesn't exist, set newNode to head.
    if (!head)
        head = newNode;
    //Otherwise, put it at the end  of the list.
    else
    {
        nodePtr = head;
        while (nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}

I'm not really certain what's going on, but my output from the switch statement where the appendation is made outputs "Works." but not "Still works." before it crashes, so I'm certain it's happening here.

http://en.wikipedia.org/wiki/Magic_number_%28programming%29

0xfeeefeee is what your system writes into memory that is freed heap memory. This means that something was deleted there, and no longer exists, but you're trying to use it as a valid pointer anyway.

Do you have a contructor for ListNode? if you do does it set the next pointer to NULL?

Not ListNode's, but TLists's.

    template <class T>
    class ListNode
    {
    public:
        T value;
        ListNode<T> *next;

        ListNode (T nodeValue)
        {
            value = nodeValue;
            next = NULL;
        }
    };

    template <class T>
    class TList
    {
    private:
        ListNode<T> *head;  //A reference to the first node in the list.

    public:
    //Constructor*****************************
    //****************************************
        TList()
        {head = NULL;}

One of the reasons I find this so difficult to understand is that I use the list successfully with the tempCD.defineCD(); function. At the end of that function, I have the following code:

        for (int index = 0; index < trackNum; index++)
        {
            if (index == 0)
                cin.get();

            cout << "\n\n";
            cout << "What is the title of track " << (index + 1) << "?" << endl;
            cout << "> ";
            getline(cin, trks);
            tracks.appendNode(trks);

            cout << "\n\n";
            cout << "What is the length of track " << (index + 1) << "? (mm:ss)" << endl;
            cout << "> ";
            getline(cin, tracklens);
            tracktimes.appendNode(tracklens);
        }

That works fine, obviously, since it makes it through and outputs "Works" before stopping the program.

Can you post the code for the CD class? It could be that the copying of the cd into the list is where you are getting the error.

Sure. I haven't overloaded the > and < operators yet, but neither is used in appendNode and.. I haven't figured out how I want to do that yet, since all my values are strings. >.>

#ifndef CD_CLASS_H
#define CD_CLASS_H
#include "ListTemplate.h"
#include "Media.h"
#include<iostream>
#include<string>

class CD : public Media
{
private:
    string artist;
    TList<string> tracks;
    TList<string> tracktimes;

public:
    CD()
    {
        title = "";
        length = "";
        artist = "";
    }

    /*
    *  Overriding operators is necessary for the class to work
    *  with the linked list.  These are the operator override
    *  function prototypes.
    */
    bool operator == (const CD &);

    friend ostream &operator << (ostream &, const CD &);

    /***********************************
    *defineCD
    *This function takes the user through and
    *sets values for all of this class' variables.
    ***********************************/
    void defineCD()
    {
        string t, // title placeholder
            art, //artist placeholder
            len, //length placeholder
            trks,
            tracklens;

        int trackNum;  //How many tracks the CD has.

        cin.get();
        cout << "\n\n";
        cout << "What is the title of the new CD?" << endl;
        cout << "> ";
        getline(cin, t);

        cout << "\n\n";
        cout << "What artist is the CD by?" << endl;
        cout << "> ";
        getline(cin, art);

        cout << "\n\n";
        cout << "How long is the CD? (hh:mm:ss)" << endl;
        cout << "> ";
        getline(cin, len);

        cout << "\n\n";
        cout << "How many tracks does the CD have?" << endl;
        cout << "> ";
        cin >> trackNum;

        for (int index = 0; index < trackNum; index++)
        {
            if (index == 0)
                cin.get();

            cout << "\n\n";
            cout << "What is the title of track " << (index + 1) << "?" << endl;
            cout << "> ";
            getline(cin, trks);
            tracks.appendNode(trks);

            cout << "\n\n";
            cout << "What is the length of track " << (index + 1) << "? (mm:ss)" << endl;
            cout << "> ";
            getline(cin, tracklens);
            tracktimes.appendNode(tracklens);
        }
    }

};

bool CD::operator == (const CD &right)
{
    bool status;

    if (title == right.title && artist == right.artist)
        status = true;
    else
        status = false;

    return status;
}



ostream &operator << (ostream &strm, const CD &obj)
{
    strm << obj.artist << obj.title;

    return strm;
}
#endif

Do you have copy constructors built? I am not seeing any. Since your cd class has a list in it and it gets copied into the list in you main program you could be having the issue there. If the list inside the class are copied by value then when things go out of scope you will have pointers pointing to nothing. Also what are title and length on line 18-19? If they are members of the media class you constructor should look like this:

CD::CD() : Media("",""), Artist("") {}

You're right on the constructor part... and probably the other as well.

I have never tried to make a copy constructor for an object containing a linked list. I assume I'll need to make one for my linked list as well, so that the lists copy? I'll just do that instead of waiting for an answer and see what happens. Probably good practice to do it anyway.

If you are dealing with anything in your class that is not POD then you should create your own copy constructor and assignment operator. The default the compiler creates does a shallow copy and that is not what you want. You can consider stl types as POD since they take care of themselves.

My code!

I'm really not sure hot to move forward with this, since my textbook doesn't cover linked list copy constructors, but I made my own based on what I think it should do/look like...

template <class T>
TList<T>::TList(const Tlist<T>& old)
  {
    ListNode<T> *newNode;
    ListNode<T> *nodePtr;
    ListNode<T> *oldPtr;

    //Set pointer oldNode to the the head of the old list.
    oldPtr = old.head;

    //As long as oldPtr exists, do this
    while (oldPtr != NULL)
    {
        //If the head node does not yet exist,
        //create the new node with the value in the node
        //oldPtr points to, then set the head to the new node,
        //then have oldPtr point to the next node in its list.
        if (!head)
        {
            newNode = new ListNode<T>(oldPtr->value);
            head = newNode;
            oldPtr = oldPtr->next;
        }
        //If the head node does exist, create the new node with
        //the value in the node oldPtr points to and make it the next
        //node in the list, then have oldPtr point to the next in the
        //old list.
        else
        {
            newNode = new ListNode<T>(oldPtr->value);
            newNode = nodePtr->next;
            oldPtr = oldPtr->next;
        }
    }
}

I have

Tlist(const Tlist<T>& old);

below my constructor within my class, then the main code for it outside of the class.

Compile time errors!

There are the following problems with this, according to Visual Studio:

1>------ Build started: Project: Programming 3 Course Project, Configuration: Debug Win32 ------
1>  CP_Main.cpp
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>          c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(47) : see reference to class template instantiation 'TList<T>' being compiled
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C2143: syntax error : missing ',' before '<'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): warning C4183: 'Tlist': missing return type; assumed to be a member function returning 'int'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(50): error C2143: syntax error : missing ',' before '<'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(83): error C2244: 'TList<T>::TList' : unable to match function definition to an existing declaration
1>          c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(33) : see declaration of 'TList<T>::TList'
1>          definition
1>          'TList<T>::TList(const int)'
1>          existing declarations
1>          'TList<T>::TList(void)'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>          c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\cd_class.h(12) : see reference to class template instantiation 'TList<T>' being compiled
1>          with
1>          [
1>              T=std::string
1>          ]
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C2143: syntax error : missing ',' before '<'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): warning C4183: 'Tlist': missing return type; assumed to be a member function returning 'int'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>          c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\cp_main.cpp(15) : see reference to class template instantiation 'TList<T>' being compiled
1>          with
1>          [
1>              T=CD
1>          ]
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C2143: syntax error : missing ',' before '<'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): warning C4183: 'Tlist': missing return type; assumed to be a member function returning 'int'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>          c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\cp_main.cpp(16) : see reference to class template instantiation 'TList<T>' being compiled
1>          with
1>          [
1>              T=DVD
1>          ]
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C2143: syntax error : missing ',' before '<'
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\chris\documents\visual studio 2010\projects\programming 3 course project\programming 3 course project\listtemplate.h(36): warning C4183: 'Tlist': missing return type; assumed to be a member function returning 'int'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Line 36 is my function declaration, line 50 is my function header, and line 83 is the closing bracket of my copy constructor function.

Other things!

In "oldPtr = old.head;", oldPtr is underlined and it says "error: this declaration has no storage class or type specifier", and "old." is underlined with "identifier 'old' is undefined".

"while", "if", and "else" are underlined with "expected a declaration".

Stuff!

Other than that, which I can't account for (as usual, because I usually can't figure out what's wrong unless I'm shown or the compiler tells me directly), do I have the concept right? :\ I can't test it.

Ugh. Ignore most of my previous post. Tlist != TList.

That being said, I was able to run my program again and test, and try to fix things, and this is what my current copy constructor looks like:

    template <class T>
    TList<T>::TList(const TList<T>& old)
      {
        ListNode<T> *newNode;
        ListNode<T> *nodePtr;
        ListNode<T> *oldPtr;

        //Set pointer oldNode to the the head of the old list.
        oldPtr = old.head;
        nodePtr = head;

        //As long as oldPtr exists, do this
        while (oldPtr != NULL)
        {
            //If the head node does not yet exist,
            //create the new node with the value in the node
            //oldPtr points to, then set the head to the new node,
            //then have oldPtr point to the next node in its list.
            if (!head)
            {
                newNode = new ListNode<T>(oldPtr->value);
                head = newNode;
                oldPtr = oldPtr->next;
            }
            //If the head node does exist, create the new node with
            //the value in the node oldPtr points to and make it the next
            //node in the list, then have oldPtr point to the next in the
            //old list.
            else
            {
                nodePtr = nodePtr->next;
                newNode = new ListNode<T>(oldPtr->value);
                nodePtr->next = newNode;
                oldPtr = oldPtr->next;
            }
        }
    }

I get:

First-chance exception at 0x01234c0b in Programming 3 Course Project.exe: 0xC0000005: Access violation writing location 0x00000026.
Unhandled exception at 0x01234c0b in Programming 3 Course Project.exe: 0xC0000005: Access violation writing location 0x00000026.

Can anyone tell me what I'm doing wrong?

Until helped, I'm going to keep updating when I make changes. :) Maybe I can fix it on my own? I doubt it, but I'll try.

This is where I'm stuck. It says I have some errors where "next" is concerned, like not knowing the value of it, etc,

The problem you are having is that on line 31 you set nodePtr to its next link which is null and then on line 33 instead of putting nodePtr = newNode you make next equal to newNode which breaks the link. I dont have a compiler on my machine but you can give this a shot and see if it will work for you or you can just fix the code you have.

template <class T>
TList<T>::TList(const TList<T>& old)
{
    ListNode<T> *newNode;
    ListNode<T> *oldNode;

    oldNode = old.head;

    if (oldNode)
    {
        //create a new head for the copy
        head = new ListNode<T>(oldNode->value);
        newNode = head;

        // loop through the list and copy into the new list
        while((oldNode = oldNode->next))
        {
            newNode->next = new ListNode<T>(oldNode->value);
            newNode = newNode->next;
        }
    }
}

You did something in that second while statement that I didn't know I could do... it worked, though, and I learned something. And I had way too many variables, apparently. :\ I tried fixing it as suggested before I took/modified your code, but mine still didn't work.

Thank you, sir.

Wrong thread. -_-

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.