编译器在编译时找不到重载的运算符<<。 C++
Compiler can't find overloaded operator<< while compiling. C++
我正在努力实现模板化堆 class。到目前为止真的很艰难。
我正在尝试显示我的堆,但出现此错误:
未定义的体系结构符号 x86_64:
“operator<<(std::__1::basic_ostream&, Heap const&)”,引用自:
_main main.o
ld:未找到体系结构的符号 x86_64
clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
我在 class 中使用了模板,所以我的函数也在头文件中实现,这里是 hpp 文件:
(!): 无需读取整个文件,只需读取 ostream& operator<<().
/*
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 使用命名空间 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<<
好友声明是 non-template 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);
};
我正在努力实现模板化堆 class。到目前为止真的很艰难。
我正在尝试显示我的堆,但出现此错误:
未定义的体系结构符号 x86_64:
“operator<<(std::__1::basic_ostream
我在 class 中使用了模板,所以我的函数也在头文件中实现,这里是 hpp 文件: (!): 无需读取整个文件,只需读取 ostream& operator<<().
/*
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 使用命名空间 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<<
好友声明是 non-template 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);
};