链接器可以查找超载运算符<<在编译时。 C++
我正在努力实施模板堆类。到目前为止,这真的很粗糙。 我正在尝试显示我的堆,但是我遇到了这个错误:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Heap<int> const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我在课堂中使用了模板,因此在标头文件内的功能也实现,这是HPP文件: (!):无需阅读整个文件,只有Ostream&amp;操作员&lt;&lt;()。
/*
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;//iterator
//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;
}
习惯从中得到任何东西。 任何帮助将不胜感激!
I am working on implementing a templated heap class. It has been really rough so far.
I am trying to display my heap but I am getting this error:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Heap<int> const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I used template in my class so the implementation of my functions in inside the header file as well, here is the hpp file:
(!): No need to read the whole file, just the 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;//iterator
//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 */
I tried all my other functions to see if I had a silly mistake or something but all worked except this one:
(!): ik the use of namespace std is a bad habbit but i am using just for testing
#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;
}
I have no idea what's the problem, tho one thing I found very close to my problem is related to the namespace std, but didn't get anything out of it.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前拥有的
运营商的朋友声明
运算符
是针对非template运营商&lt;&lt;
的。有两种解决此问题的方法:
方法1
至 solve 这个朋友声明的声明替换了具有自己的参数条款的声明,以便您是 friend模板声明,在课堂内,带有:
demo
方法2
在这里我们可以转发两者,我们可以转发两个类模板
HEAP
和操作员&lt;
。demo
The friend declaration for
operator<<
that you currently have is for a non-templateoperator<<
.There are 2 ways to solve this:
Method 1
To solve this replace that friend declaration with a declaration that has its own parameter clause so that you've a friend template declaration, inside the class with:
Demo
Method 2
Here we can forward declare both the class template
Heap
andoperator<<
.Demo