[英]Compiler can't find overloaded operator<< while compiling. C++
我正在努力實現一個模板化堆 class。到目前為止,它真的很粗糙。 我正在嘗試顯示我的堆,但出現此錯誤:體系結構 x86_64 的未定義符號:“運算符<<(std::__1::basic_ostream<char, std::__1::char_traits >&, Heap const&)”,引用自:main.o ld 中的 _main:未找到體系結構 x86_64 的符號 clang:錯誤:linker 命令失敗,退出代碼為 1(使用 -v 查看調用)
我在我的 class 中使用了模板,所以我的功能也在 header 文件中實現,這里是 hpp 文件:(:),無需讀取整個文件。 只有 ostream& 運算符<<()。
/*
Header file for Heap implemenetation
*/
#ifndef Heap_hpp
#define Heap_hpp
#include <stdio.h>
#include <vector>
#include <iterator>
#include <string>
#include <iostream>
template<typename elementType>
class Heap{
private:
std::vector<elementType> myVecrtor;
int mySize = 1; //The minimum size is 1 since the first element is a dummy.
void perculateDown(int root);
void perculateUp();
public:
Heap();
bool checkEmpty();
void insert(elementType iteam);
int getSize();
std::vector<elementType> getHeap();
elementType getMax();
int getMaxIndex();
void removeMax();
void removeAtIndex(int index);
friend std::ostream& operator<<(std::ostream &out, Heap const &h);
};
template<typename elementType>
Heap<elementType>::Heap(){
}
template <typename elementType>
bool Heap<elementType>::checkEmpty(){
if(mySize > 1)
return true;
else
return false;
}
template <typename elementType>
void Heap<elementType>::insert(elementType iteam){
auto it = mySize;//itterator
//myVecrtor.insert(it, iteam);//insert after first element(dummy)
mySize++;
perculateUp();
}
template <typename elementType>
std::vector<elementType> Heap<elementType>::getHeap(){
return myVecrtor;
}
template <typename elementType>
elementType Heap<elementType>::getMax(){
return myVecrtor[getMaxIndex()];
}
template <typename elementType>
int Heap<elementType>::getMaxIndex(){
int maxIndex = 1;
elementType max = myVecrtor[maxIndex];
for(int i = 0; i < myVecrtor.size(); i++){
if(max < myVecrtor[i])
maxIndex = i;
}
return maxIndex;
}
template <typename elementType>
void Heap<elementType>::removeMax(){
int maxIndex = getMaxIndex();
myVecrtor[maxIndex] = myVecrtor[mySize];
mySize--;
perculateDown(maxIndex);
}
template <typename elementType>
void Heap<elementType>::removeAtIndex(int index){
myVecrtor[index] = myVecrtor[mySize];
mySize--;
perculateDown(index);
}
template <typename elementType>
std::ostream& operator<<(std::ostream &out, const Heap<elementType> &h){//it is giving me the error here
out<<"\t\tHeap:";
for(int i = 0; i < h.mySize; i++){
out<<h.myVecrtor.at(i);
}
return out;
}
template <typename elementType>
void Heap<elementType>::perculateUp(){
int loc = mySize - 1;
int parent = loc /2;
// while(parent >= 1 && myVecrtor[loc] > myVecrtor[parent]){
elementType temp = myVecrtor[parent];
myVecrtor[parent] = myVecrtor[loc];
myVecrtor[loc] = temp;
loc = parent;
parent = loc / 2;
// }
}
template <typename elementType>
void Heap<elementType>::perculateDown(int root){
int r = root, c = r*2;
while (r < mySize - 1) {
if(c < mySize && myVecrtor[c] < myVecrtor[c+1])
c++;
if(myVecrtor[r] < myVecrtor[c]){
elementType temp = myVecrtor[r];
myVecrtor[r] = myVecrtor[c];
myVecrtor[c] = temp;
r = c;
c *= 2;
}
else
break;
}
}
#endif /* Heap_hpp */
我嘗試了所有其他功能,看看我是否犯了一個愚蠢的錯誤或其他問題,但除了這個以外,所有功能都有效:(:): ik namespace std 的使用是一個壞習慣,但我只是用於測試
#include <vector>
#include "Heap.hpp"
#include <ostream>
using namespace std;
int main(int argc, const char * argv[]) {
Heap<int> h;
h.insert(5);
h.getHeap();
h.getMaxIndex();
h.getMax();
h.removeMax();
h.removeAtIndex(1);
h.getHeap();
cout<<h;
}
我不知道問題出在哪里,但我發現與我的問題非常接近的一件事與命名空間 std 有關,但沒有從中得到任何結果。 任何幫助,將不勝感激!
您當前擁有的operator<<
的友元聲明是針對非模板operator<<
的。
有兩種方法可以解決這個問題:
要解決此問題,請使用具有自己的參數子句的聲明替換該友元聲明,以便您在 class 中擁有一個友元模板聲明:
template<typename elementType>
class Heap{
//other code as before
public:
template<typename U>
friend std::ostream& operator<<(std::ostream &out, Heap<U> const &h);
};
在這里我們可以轉發聲明 class 模板Heap
和operator<<
。
//forward declaration for Heap<>
template<typename T> class Heap;
//forward declaration for operator<<
template<typename T> std::ostream& operator<<(std::ostream& os, Heap<T> const &h);
template<typename elementType>
class Heap{
//other members as before
public:
friend std::ostream& operator<<<elementType>(std::ostream &out, Heap<elementType> const &h);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.