未定义的对c ++中通用对象的引用

时间:2014-02-26 23:13:34

标签: c++ oop generics

好的,所以我是c ++的新手,并且有一个赋值来创建一个带有提供的头文件sortedlist.h和node.h的有序链表。我不需要方法中的任何代码的帮助,只有如何将它带到我在main.cpp类中创建对象的地方我比java更习惯于java。我的问题是当我尝试创建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 */

任何帮助都表示赞赏,就像我说我是语言新手一样,如果你能提供帮助,我会努力学习,并且不介意提前解释一下,这是非常感谢

1 个答案:

答案 0 :(得分:1)

问题是,您正在cpp文件中定义模板。您需要在头文件中移动它们,或者在主文件中包含cpp文件。

由于SFINAE,在编译主文件时,编译器无法在不访问每个函数的定义的情况下执行模板解析。