编译器:Qt 语言:C ++
这个程序不适合我哈哈,这是我第三次寻求帮助,这让我发疯了(谢谢大家对我这么耐心和乐于助人)
我尝试运行我的程序(同样是第一百万次)因为我无法解决ostream的问题,所以我在主要内容中注释掉了所有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 debug / list.o -L“c:\ Qt \ 2010.05 \ qt \ lib“-lQtCored4
调试/ main.o:C:\ Qt的\ 2010.05 \ BIN \列表的积聚桌面/../列表// List.h:194: 未定义引用`List :: getNewNode(double const&)'
调试/ main.o:C:\ Qt的\ 2010.05 \ BIN \列表的积聚桌面/../列表// List.h:215: 未定义引用`List :: getNewNode(double const&)'
调试/ main.o:C:\ Qt的\ 2010.05 \ BIN \列表的积聚桌面/../列表// List.h:215: 未定义的引用`List :: getNewNode(int const&)'
调试/ main.o:C:\ Qt的\ 2010.05 \ BIN \列表的积聚桌面/../列表// List.h:194: 未定义的引用`List :: getNewNode(int const&)'
调试/ main.o:C:\ Qt的\ 2010.05 \ BIN \列表的积聚桌面/../列表// List.h:466: 未定义的引用`List :: getNewNode(int const&)'
调试/ main.o:C:\ Qt的\ 2010.05 \ BIN \列表的积聚桌面/../列表// 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: * [debug]错误2
进程“C:/Qt/2010.05/mingw/bin/mingw32-make.exe”已退出 代码%2。构建项目列表时出错(目标:桌面)何时 执行构建步骤'Make'
这基本上就是我得到的。我有2个头文件和一个源文件。 标题:ListNode.h和list.h,我的cpp是main.cpp
一切都相互联系,所以我不明白为什么它给了我未定义的参考错误。
我所有的其他朋友都非常放弃这项任务,我拒绝放弃。我今晚不会去睡觉哈。再次感谢您的帮助!
编辑:: - 代码
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
我不会放一切,因为它很多......这就是List.h ..我只能想象它有多少仍然是错的,但我无法检查因为错误... < / p>
#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文件按要求
#-------------------------------------------------
#
# 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
答案 0 :(得分:3)
您似乎忘了创建List::getNewNode
功能。