4

编译器:Qt 语言:C++

这个程序不适合我哈哈,这是我第三次不得不寻求帮助,这让我发疯了(感谢大家对我的耐心和帮助)

我尝试运行我的程序(再次第 100 万次)因为我无法整理出 ostream,所以我在 main 中注释掉了所有 cout 函数,以便我可以处理我的其余代码。但是当我尝试运行它时,我得到了 collect2: ld 在我的构建问题中返回了 1 个退出状态。

我切换到编译输出......和天哪......

运行项目列表的构建步骤...配置不变,跳过 qmake 步骤。开始:“C:/Qt/2010.05/mingw/bin/mingw32-make.exe”-w mingw32-make:进入目录`C:/Qt/2010.05/bin/List-build-desktop'

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

mingw32-make[1]:进入目录`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调试/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: 未定义对 `List::getNewNode(double const&)' 的引用

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:215: 未定义对 `List::getNewNode(double const&)' 的引用

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:215: 未定义对 `List::getNewNode(int const&)' 的引用

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:194: 未定义对 `List::getNewNode(int const&)' 的引用

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:466: 未定义对 `List::getNewNode(int const&)' 的引用

debug/main.o:C:\Qt\2010.05\bin\List-build-desktop/../List//List.h:466: 未定义对 `List::getNewNode(double const&)' 的引用

collect2: ld 返回 1 个退出状态

mingw32-make[1]: 离开目录`C:/Qt/2010.05/bin/List-build-desktop'

mingw32-make: 离开目录`C:/Qt/2010.05/bin/List-build-desktop'

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

mingw32-make: * [调试] 错误 2

进程“C:/Qt/2010.05/mingw/bin/mingw32-make.exe”退出,代码为 %2。构建项目列表时出错(目标:桌面)执行构建步骤“Make”时

这基本上就是我得到的。我有 2 个头文件和 1 个源文件。标头:ListNode.h 和 list.h,我的 cpp 是 main.cpp

一切都相互联系,所以我不明白为什么它给了我未定义的参考错误。

我所有的其他朋友都几乎放弃了这项任务,我拒绝放弃。我只是今晚不睡觉哈。再次感谢您的帮助!

编辑::--代码

列表节点.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

我不会把所有的东西都放上去,因为它很多......这是List.h..我只能想象它还有多少是错误的,但我无法检查,因为错误......

#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;
    }
}

主文件

#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 文件按要求

#-------------------------------------------------
#
# 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
4

1 回答 1

3

好像你忘了创建你的List::getNewNode函数。

于 2012-09-20T09:08:03.913 回答