【问题标题】:undefined reference to a generic object in c++ [duplicate]c++ 中对通用对象的未定义引用[重复]
【发布时间】:2014-02-26 23:13:34
【问题描述】:

好的,所以我是 C++ 的新手,并且有一个任务是创建一个带有提供的标题 sortedlist.h 和 node.h 的排序链表。我不需要方法中的任何代码的帮助,只需要如何在我的 main.cpp 类中创建我的对象时,我更习惯于 java 而不是 c++。我的问题是当我尝试创建我得到的 sortedLinkedList 对象时:

对 `linkedSortedList::linkedSortedList()' 的未定义引用

我不确定如何让主文件识别对象,或者我设置错误这是我的第一个 c++ 工作,所以我可能会犯一些简单的错误。但问题是我无法在对象类中测试我的代码,直到我得到这个工作,无论如何这就是我所拥有的。

我的 ma​​in.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。这是问题错字还是两个文件都有?

标签: c++ oop generics


【解决方案1】:

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

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

【讨论】:

  • 那么需要添加到标题中的内容以及我需要在哪里添加它是您说将template &lt;class Elm&gt; 移动到标题中的方法而不是.cpp 文件
  • 从linkedsortedlist.cpp 中取出所有方法定义,并将其移动到类定义下的头文件中。然后,从您的项目中删除linkedsortedlist.cpp。
猜你喜欢
  • 1970-01-01
  • 2019-04-12
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
相关资源
最近更新 更多