I tried tu running a c++ code with this command on the terminal
g++ --std=c++11 Main.cpp Heap.cpp -o Heap
But the terminal shows this, and I don't know what to do to correct this problem. Have you an advice?
"__ZN7PersonaC1Ev", referenced from:
__ZN4HeapC2Ei in ccOlz7WW.o
__ZN4HeapC1Ei in ccOlz7WW.o
__ZN4Heap6insertENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi in ccOlz7WW.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
I have Xcode tools for MacOs and is the first time that I have this error. Here are the .cpp files
Main.cpp
#include <iostream>
#include <string>
#include "Heap.hpp"
int main() {
int N = 5;
Heap hospital(N);
std::string persona;
hospital.insert("Santiago", 2);
hospital.insert("Claudia", 7);
hospital.insert("Joel", 5);
persona = hospital.top();
hospital.pop();
std::cout << "Proxima persona a ser atendida: " << persona << std::endl;
hospital.insert("Juan", 10);
while (!hospital.empty()) {
persona = hospital.top();
hospital.pop();
std::cout << "Proxima persona a ser atendida: " << persona << std::endl;
}
return 0;
}
Heap.cpp
#include <string>
#include <iostream>
#include <bits/stdc++.h>
#include "Heap.hpp"
using namespace std;
Persona:: Persona(string nombre, int prioridad){
this->nombre = nombre;
this->prioridad = prioridad;
}
Heap:: Heap (int max_personas){
this->max_personas = max_personas;
this->heap_arr = new Persona [max_personas];
this->time = 0;
for (int i = 0; i < max_personas ; i++){
heap_arr[i].prioridad = INT_MIN;
}
}
Heap:: ~Heap (){
free (heap_arr);
}
int Heap::Left (int i){
return (2*(i+1))-1;
}
int Heap::Right (int i){
return (2*(i+1));
}
int Heap::Parent (int i){
return (i-1)/2;
}
void Heap::maxHeapify (int i){
int l = Left(i);
int r = Right(i);
int largest = 0;
if (l <= this->HeapSize-1 && heap_arr[i] < heap_arr[l]){
largest = l;
} else {
largest = i;
}
if (r <= this->HeapSize-1 && heap_arr[largest] < heap_arr[r]){
largest = r;
}
if (largest != i){
swap (heap_arr[i], heap_arr[largest]);
maxHeapify(largest);
}
return;
}
void Heap::HeapIncreaseKey (int i, Persona nueva){
if (nueva < heap_arr[i]){
cout << "Error, la nueva persona no tiene mayor prioridad" << endl;
return;
}
heap_arr[i] = nueva;
while (i > 0 && heap_arr[Parent(i)] < heap_arr[i]){
swap(heap_arr[i], heap_arr[Parent(i)]);
i = Parent(i);
}
return;
}
void Heap::insert(string nombre, int prioridad){
Persona nueva;
nueva.nombre = nombre;
nueva.prioridad = prioridad;
nueva.tiempo_llegada = time;
this->time++;
if (this->HeapSize+1 > this->max_personas){
cout << "Error, se supero la cantidad maxima de personas" << endl;
return;
}
this->HeapSize++;
this->heap_arr[HeapSize-1].prioridad = INT_MIN;
HeapIncreaseKey(HeapSize-1, nueva);
return;
}
string Heap::top (){
return this->heap_arr[0].nombre;
}
void Heap::pop (){
if (HeapSize < 1) {
cout << "Error: la cola esta vacia" << endl;
return;
}
Persona max = heap_arr[1];
heap_arr[1] = heap_arr[HeapSize];
this->HeapSize--;
maxHeapify(1);
return;
}
bool Heap:: empty(){
if (HeapSize < 1){
return true;
}
return false;
}
And the .hpp file
Heap.hpp
#include <string>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Persona {
std::string nombre;
int prioridad;
int tiempo_llegada;
Persona();
Persona (std::string nombre, int prioridad);
};
bool operator < (Persona& a, Persona& b){
if (a.prioridad < b.prioridad)
return true;
if (a.prioridad == b.prioridad && a.tiempo_llegada < b.tiempo_llegada)
return true;
return false;
}
class Heap {
public:
Heap();
Heap(int max_personas);
~Heap();
void insert(std::string nombre, int prioridad);
std::string top();
void pop();
bool empty();
private:
int max_personas;
Persona *heap_arr;
int time;
int HeapSize;
int Left(int i);
int Right(int i);
int Parent(int i);
void maxHeapify (int i);
void HeapIncreaseKey (int i, Persona nueva);
};