C++ – collect2: Ld returned 1 exit status build make error

ccompiler-errorsexitqtstatus

Compiler: Qt
Language: C++

This program is just not for me haha, this is the third time I've had to ask for help, and it's driving me crazy (thank you everyone for being so patient and helpful with me)

I tried running my program (again for the millionth time) Since I can't get the ostream sorted out, I commented out all the cout functions in my main so I could deal with the rest of my code. But when I try to run it I get collect2: ld returned 1 exit status in my build issues.

I switched to Compile Output… and gosh..

Running build steps for project List…
Configuration unchanged, skipping qmake step.
Starting: "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" -w
mingw32-make: Entering directory `C:/Qt/2010.05/bin/List-build-desktop'

C:/Qt/2010.05/mingw/bin/mingw32-make -f Makefile.Debug

mingw32-make[1]: Entering directory
`C:/Qt/2010.05/bin/List-build-desktop'

g++ -enable-stdcall-fixup -Wl,-enable-auto-import
-Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\List.exe debug/main.o debug/list.o -L"c:\Qt\2010.05\qt\lib" -lQtCored4

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:194:
undefined reference to `List::getNewNode(double const&)'

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:215:
undefined reference to `List::getNewNode(double const&)'

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:215:
undefined reference to `List::getNewNode(int const&)'

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:194:
undefined reference to `List::getNewNode(int const&)'

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:466:
undefined reference to `List::getNewNode(int const&)'

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:466:
undefined reference to `List::getNewNode(double const&)'

collect2: ld returned 1 exit status

mingw32-make[1]: Leaving directory
`C:/Qt/2010.05/bin/List-build-desktop'

mingw32-make: Leaving directory `C:/Qt/2010.05/bin/List-build-desktop'

mingw32-make[1]: * [debug\List.exe] Error 1

mingw32-make: * [debug] Error 2

The process "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" exited with
code %2. Error while building project List (target: Desktop) When
executing build step 'Make'

That's basically what I got. I have 2 header files and one source file.
Headers: ListNode.h and list.h and my cpp is main.cpp

everything links to each other, so i don't understand why it's giving me undefined reference errors.

All my other friends have pretty much given up on this assignment, and I refuse to give up. I just won't go to bed tonight ha. Thanks again for the help!

EDIT::– CODE

ListNode.h

#ifndef LISTNODE_H
#define LISTNODE_H

template<typename NODETYPE> class List;

template<typename NODETYPE>
class ListNode
{
    friend class List< NODETYPE >;

public:
    ListNode(const NODETYPE &);
    NODETYPE getData() const;
private:
    NODETYPE data;
    ListNode< NODETYPE > *nextPtr;

};

template<typename NODETYPE>
ListNode< NODETYPE >::ListNode(const NODETYPE &info)
    :data(info), nextPtr(0)
{

}

template<typename NODETYPE>
NODETYPE ListNode< NODETYPE >::getData() const
{
    return data;
}


#endif // LISTNODE_H

I won't put everything, because it's a lot… this is the List.h..I can only imagine how much of it is still wrong, but I can't check because the error…

#include <iostream>
using std::cout;

#include <fstream>

#include <string>
using std::ostream;

#include "ListNode.h"

template< typename NODETYPE >
class List
{
  //template <typename OUTPUT >
  //friend ostream &operator <<(ostream &, const List<NODETYPE> & );

public:
    List();
    ~List();
    void insertAtFront( const NODETYPE & );
    void insertAtBack( const NODETYPE & );
    bool removeFromFront( NODETYPE & );
    bool removeFromBack( NODETYPE & );
    bool isEmpty() const;
    void print() const;


    bool append(const NODETYPE &);
    bool add_n(int, const NODETYPE &);
    bool concat(List&);
    void reverse();
    bool remove_last();
    bool remove_n(int);
    void sort();
    bool merge(List&);
    void add_in(const NODETYPE &);
    void remove(const NODETYPE &);
    NODETYPE sum();
    int count();

private:
    ListNode< NODETYPE > *firstPtr;
    ListNode< NODETYPE > *lastPtr;

    ListNode< NODETYPE > *getNewNode( const NODETYPE &);

};

template<typename NODETYPE>
List<NODETYPE>::List()
    : firstPtr(0), lastPtr(0)
{

}

template< typename NODETYPE>
List<NODETYPE>::~List()
{
    if( !isEmpty() )
    {
        cout << "Destroying Nodes...\n";

        ListNode< NODETYPE > *currentPtr = firstPtr;
        ListNode< NODETYPE > *tempPtr;

        while ( currentPtr != 0)
        {
            tempPtr = currentPtr;
            cout << tempPtr->data << "\n";
            currentPtr = currentPtr->nextPtr;
            delete tempPtr;
        }
    }

    cout << "All nodes destroyed \n\n";
}

template< typename NODETYPE >
void List< NODETYPE>::insertAtFront(const NODETYPE &value)
{
    ListNode< NODETYPE > *newPtr = getNewNode (value);

    if( isEmpty())
        firstPtr = lastPtr = newPtr;
    else
    {
        newPtr->nextPtr = firstPtr;
        firstPtr = newPtr;
    }
}

template< typename NODETYPE >
void List<NODETYPE>::insertAtBack(const NODETYPE &value)
{
    ListNode<NODETYPE> *newPtr = getNewNode(value);

    if( isEmpty())
        firstPtr = lastPtr = newPtr;
    else
    {
        lastPtr->nextPtr = newPtr;
        lastPtr = newPtr;
    }
}

template< typename NODETYPE >
bool List<NODETYPE>::removeFromFront(NODETYPE &value)
{
    if(isEmpty())
        return false;
    else
    {
        ListNode<NODETYPE> *tempPtr = firstPtr;

        if(firstPtr == lastPtr)
            firstPtr = lastPtr = 0;
        else
            firstPtr = firstPtr->nextPtr;

        value = tempPtr->data;
        delete tempPtr;
        return true;
    }
}

template<typename NODETYPE>
bool List<NODETYPE>::removeFromBack(NODETYPE &value)
{
    if( isEmpty())
        return false;
    else
    {
        ListNode<NODETYPE> *tempPtr = lastPtr;

        if(firstPtr == lastPtr)
            firstPtr = lastPtr = 0;
        else
        {
            ListNode<NODETYPE> *currentPtr = firstPtr;

            while (currentPtr->nextPtr != lastPtr)
                currentPtr = currentPtr->nextPtr;

            lastPtr=currentPtr;
            currentPtr->nextPtr = 0;
        }
        value = tempPtr->data;
        delete tempPtr;
        return true;
    }
}

template<typename NODETYPE>
bool List<NODETYPE>::isEmpty() const
{
    return firstPtr == 0;
}

template<typename NODETYPE>
void List<NODETYPE>::print() const
{
    if( isEmpty())
    {
        cout<<"The list is empty \n\n";
        return;
    }
    ListNode<NODETYPE> *currentPtr = firstPtr;

    cout<< "The list is: ";

    while(currentPtr != 0)
    {
        cout<<currentPtr->data>> ' ';
        currentPtr = currentPtr->nextPtr;
    }

    cout<< "\n\n";
}

/*template<typename NODETYPE>
ostream &operator <<(ostream &output, const List<NODETYPE>& value)
{
    output << value;
    return output;
}*/


template<typename NODETYPE>
bool List<NODETYPE>::append(const NODETYPE &value)
{
    ListNode<NODETYPE> *newPtr = getNewNode(value);

   if(isEmpty())
   {
        firstPtr = lastPtr = newPtr;
        return true;
    }
    else
    {
        ListNode<NODETYPE> *tempPtr = lastPtr;

        tempPtr->nextPtr=newPtr;
        lastPtr = newPtr;
        return true;
    }

}

template<typename NODETYPE>
bool List<NODETYPE>::add_n(int a, const NODETYPE &value)
{
    ListNode<NODETYPE> *newPtr = getNewNode(value);

    if(isEmpty())
    {
        firstPtr = lastPtr = newPtr;
        return true;
    }
    if(a <= count())
    {
        lastPtr->nextPtr = newPtr;
        lastPtr = newPtr;
        return true;
    }
    else
    {
        ListNode<NODETYPE> *currentPtr = firstPtr;
        for(int cntr = 1; cntr < count(); cntr++)
        {
            if(cntr == a)
            {
                newPtr->nextPtr = currentPtr->nextPtr;
                currentPtr->nextPtr = newPtr;
                return true;
            }

            currentPtr = currentPtr->nextPtr;
        }
        return false;
    }
}

template<typename NODETYPE>
bool List<NODETYPE>::concat(List<NODETYPE> &li)
{
    if(isEmpty())
        return false;
    if(li.isEmpty())
        return false;
    else
    {
        ListNode<NODETYPE> *tempPtr = lastPtr;

        tempPtr->nextPtr = li.firstPtr;
        tempPtr = tempPtr->nextPtr;

        while(tempPtr->nextPtr != 0)
            tempPtr = tempPtr->nextPtr;

        lastPtr = tempPtr;

        return true;

    }

}

template<typename NODETYPE>
void List<NODETYPE>::reverse()
{
    if(isEmpty())
        return;
    else
    {
       int chk = count();
       ListNode<NODETYPE> *currentPtr = firstPtr->nextPtr;
       ListNode<NODETYPE> *tempPtr = firstPtr;
       ListNode<NODETYPE> *tempPtr2;

       for(int a = 1; a < chk; a++)
       {
           tempPtr2 = currentPtr->nextPtr;
           tempPtr->nextPtr = currentPtr->nextPtr;
           currentPtr->nextPtr = firstPtr;
           firstPtr = currentPtr;
           currentPtr = tempPtr2;

       }

       lastPtr = tempPtr;
    }
}

main.cpp

#include "List.h"

#include <iostream>
using std::cout;
using std::endl;


int main()
{
  List<int> Li, Li2, Li3;
  List<double> Ld, Ld2;

  Ld.append(11.1);
  Ld.append(22.2);
  Ld.append(33.3);
  Ld.add_n(35.5,2);
  //cout << "Ld is: " << Ld << endl;

  Ld.reverse();
  //cout << "Ld is: " << Ld << endl;

  Li.add_n(15,-1);
  Li.add_n(16,0);
  Li.add_n(17,1);
  Li.append(10);
  //cout << "Li is: " << Li << endl;

  Li2.append(5);
  Li2.append(6);
  Li2.add_n(7,2);
  Li2.add_n(8,1);
  //cout << "Li2 is: " << Li2 << endl;

  // You shouldn't use Li2 after the concatenation, because the
  // elements aren't copied, just connected to form one list.

  Li.concat(Li2);
  //cout << "Li is: " << Li << endl;
  //cout << "Li2 is: " << Li2 << endl;

  Li.sort();
  //cout << "Li is: " << Li << endl;

  Li3.append(20);
  Li3.append(10);
  Li3.add_n(50,1);
  Li3.add_n(40,3);
  //cout << "Li3 is: " << Li3 << endl;

  Li.merge(Li3);
  //cout << "Li is: " << Li << endl;
  //cout << "Li3 is: " << Li3 << endl;

  Li3.sort();
  //cout << "Li3 is: " << Li3 << endl;

  // Li3 should not be used after the merge, since the nodes have been removed.

  Li.merge(Li3);
  //cout << "Li is: " << Li << endl;

  Li.add_in(25);
  Li.add_in(4);
  //cout << "Li is: " << Li << endl;

  Li.remove(10);
  Li.remove(50);
  //cout << "Li is: " << Li << endl;

  Ld.add_in(14.3);
  Ld.remove(14.3);
  //cout << "Ld is: " << Ld << endl;

  Ld2 = Ld;
  //cout << "Ld is: " << Ld << endl;
  //cout << "Ld2 is: " << Ld2 << endl;

  Ld.sort();
  //cout << "Ld is: " << Ld << endl;
  //cout << "Ld2 is: " << Ld2 << endl;

  cout << "Li has " << Li.count() << " nodes with a total value of "
      << Li.sum() << endl;

  cout << "Ld has " << Ld.count() << " nodes with a total value of "
       << Ld.sum() << endl;


  return 0;
}

.pro file as requested

#-------------------------------------------------
#
# Project created by QtCreator 2012-09-20T02:33:43
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = List
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    list.cpp

HEADERS += \
    list.h \
    ListNode.h

Best Answer

It seems like you forgot to create your List::getNewNode function.

Related Topic