【发布时间】:2014-02-26 23:13:34
【问题描述】:
好的,所以我是 C++ 的新手,并且有一个任务是创建一个带有提供的标题 sortedlist.h 和 node.h 的排序链表。我不需要方法中的任何代码的帮助,只需要如何在我的 main.cpp 类中创建我的对象时,我更习惯于 java 而不是 c++。我的问题是当我尝试创建我得到的 sortedLinkedList 对象时:
对 `linkedSortedList::linkedSortedList()' 的未定义引用
我不确定如何让主文件识别对象,或者我设置错误这是我的第一个 c++ 工作,所以我可能会犯一些简单的错误。但问题是我无法在对象类中测试我的代码,直到我得到这个工作,无论如何这就是我所拥有的。
我的 main.cpp 很简单:
#include <cstdlib>
#include "LinkedSortedList.h"
#include <iostream>
using namespace std;
// ---------------------------------------------------------------------
// main() -- Load some values into the list and print it. Then clear
// the list and reload it. Finally, print the values in the
// list using getfirst, which should remove all the values
// from the list.
// ---------------------------------------------------------------------
int main() {
int value; // Next list value
// Create a list
linkedSortedList<int> mylist;
// Load some values
mylist.insert(7);
mylist.insert(3);
mylist.insert(-2);
mylist.insert(5);
// Print the whole list
cout << "Using print():" << endl;
mylist.print();
cout << endl;
return 0;
}
这是我的 linkedSortedList.cpp
#include "linkedSortedList.h"
#include "LinkedNode.h"
template <class Elm> linkedSortedList<Elm>::linkedSortedList() {
head = LinkedNode<Elm>();
tail = LinkedNode<Elm>();
tail = head.next;
}
/**linkedSortedList::linkedSortedList(const linkedSortedList& orig) {
}**/
template <class Elm> linkedSortedList<Elm>::~linkedSortedList() {
}
// Clear the list. Free any dynamic storage.
template <class Elm> void linkedSortedList<Elm>::clear(){
}
// Insert a value into the list. Return true if successful, false
// if failure.
template <class Elm> bool linkedSortedList<Elm>::insert(Elm newValue){
LinkedNode<Elm> Temp = LinkedNode<Elm>(newValue,NULL);
if(size = 0){
head.value = newValue;
}else{
if(size = 1){
head.next = &Temp;
}else{
tail.next = &Temp;
}
tail = Temp.next;
}
listLength++;
}
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
template <class Elm> bool linkedSortedList<Elm>::getfirst(Elm &returnvalue){
*returnvalue = head.value;
}
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
template <class Elm> void linkedSortedList<Elm>::print() const{
LinkedNode<Elm> current = head;
while(current.next->value != NULL){
current.print();
current = current.next;
}
}
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
template <class Elm> bool linkedSortedList<Elm>::find(Elm searchvalue)const{
}
// Return the number of items in the list
template <class Elm> int linkedSortedList<Elm>::size() const{
return listLength;
}
;
还有我的 linkedSortedList.h
#ifndef LINKEDSORTEDLIST_H
#define LINKEDSORTEDLIST_H
#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
using namespace std;
template <class Elm> class linkedSortedList: public SortedList <Elm>{
public:
linkedSortedList();
~linkedSortedList();
void clear();
// Insert a value into the list. Return true if successful, false
// if failure.
bool insert(Elm newvalue);
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
bool getfirst(Elm &returnvalue);
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
void print() const;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
bool find(Elm searchvalue) const;
// Return the number of items in the list
int size() const;
private:
LinkedNode<Elm> head;
LinkedNode<Elm> tail;
int listLength;
};
#endif /* LINKEDSORTEDLIST_H */
感谢任何帮助,就像我说我是这门语言的新手并且正在努力学习,所以如果你能提供帮助并且不介意解释一下,那将非常感谢提前
【问题讨论】:
-
当问题出在编译器上时,为什么要发布所有代码?那么是visual studio,g++,....使用makefile,scons,gradle,...
-
您在
linkedSortedList.cpp中包含linkedSortedList.h,但在main.cpp中包含LinkedSortedList.h。这是问题错字还是两个文件都有?