-1

Ive been getting an undefined reference error even though ive checked and found no discrepancies with header file inclusion, wrong function name in either the header file or .cpp file. Ive also checked weather its my function call thats wrong but it seems all correct. I've never had to deal with this kind of problem before and i'm completely stumped. I imagine this might be a ton of code, so if you have any tips for me to treat it myself is already good enough as long as you take my previous tries in consideration.

g++ main.cpp republica.cpp morador.cpp tarefa.cpp conta.cpp -o SRO.o
/usr/bin/ld: /tmp/cc1XrC11.o: in function `gerenciarMoradores(tarefa**, morador**)':
main.cpp:(.text+0x1579): undefined reference to `pessoa::getNome[abi:cxx11](morador*)'
/usr/bin/ld: main.cpp:(.text+0x1694): undefined reference to `pessoa::setNome(morador*, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0x16cd): undefined reference to `pessoa::setIdade(morador*, 
int)'
/usr/bin/ld: main.cpp:(.text+0x16fa): undefined reference to 
`pessoa::setHierarquia(morador*, int)'
/usr/bin/ld: main.cpp:(.text+0x1739): undefined reference to 
`pessoa::setAniversario(morador*, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0x1a4f): undefined reference to `pessoa::getNome[abi:cxx11] 
(morador*)'
/usr/bin/ld: /tmp/cc1XrC11.o: in function `gerenciarRepublica(int, morador**, tarefa**, 
republica*)':
main.cpp:(.text+0x20e6): undefined reference to `rep::setNome(republica*, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0x2109): undefined reference to 
`rep::setQtd_morador(republica*, int)'
/usr/bin/ld: main.cpp:(.text+0x2122): undefined reference to `rep::setSaldo(republica*, 
float)'
/usr/bin/ld: main.cpp:(.text+0x2139): undefined reference to 
`rep::setQtd_morador(republica*, int)'
/usr/bin/ld: /tmp/cc1XrC11.o: in function `controledeTarefas(int, tarefa**)':
main.cpp:(.text+0x26e9): undefined reference to `task::getNome[abi:cxx11](tarefa*)'
/usr/bin/ld: main.cpp:(.text+0x27d6): undefined reference to `task::setNome(tarefa*,  
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0x2809): undefined reference to `task::setHorario(tarefa*, 
int)'
/usr/bin/ld: /tmp/cc1NzJ6p.o: in function `imprimeRep(republica*)':
republica.cpp:(.text+0x170): undefined reference to `rep::getNome[abi:cxx11](republica*)'
/usr/bin/ld: republica.cpp:(.text+0x1d4): undefined reference to 
`rep::getQtd_morador(republica*)'
/usr/bin/ld: republica.cpp:(.text+0x238): undefined reference to `rep::getSaldo(republica*)'
/usr/bin/ld: /tmp/ccRWurEO.o: in function `imprimeMoradores(morador**)':
morador.cpp:(.text+0xd8): undefined reference to `pessoa::getNome[abi:cxx11](morador*)'
/usr/bin/ld: morador.cpp:(.text+0x11c): undefined reference to `pessoa::getIdade(morador*)'
/usr/bin/ld: /tmp/ccVsueeD.o: in function `salvaLista(lista*)':
conta.cpp:(.text+0x673): undefined reference to `conta::getOp(movimentacao*)'
/usr/bin/ld: /tmp/ccVsueeD.o: in function `editaMovm(int, lista*)':
conta.cpp:(.text+0x1394): undefined reference to `conta::setCod(movimentacao*, int)'
/usr/bin/ld: conta.cpp:(.text+0x13bd): undefined reference to `conta::setNome(movimentacao*, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: conta.cpp:(.text+0x13e2): undefined reference to 
`conta::setValor(movimentacao*, float)'
/usr/bin/ld: conta.cpp:(.text+0x13fd): undefined reference to `conta::setOp(movimentacao*, 
bool)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:2: all] Error 1

The header file is

#include <iostream>
#include <string>
#include "tarefa.hpp"
using namespace std;

typedef struct morador Morador;

Morador* alocaMorador(string nome, int idade, int hierarquia, Tarefa** tarefa, string 
anniversario);
void imprimeMoradores(Morador** a);
void deletaMorador(Morador* a);

namespace pessoa{

string getNome(Morador* a);
void setNome(Morador* a, string b);
int getIdade(Morador* a);
void setIdade(Morador* a, int b);
int getHierarquia(Morador* a);
void setHierarquia(Morador* a, int hierarquia);
string getAniversario(Morador*a);
void setAniversario(Morador* a, string aniversario);

}

main.cpp is

#include <iostream>
#include <unistd.h>
#include <string>
#include "conta.hpp"
#include "tarefa.hpp"
#include "morador.hpp"
#include "republica.hpp"
using namespace std;

void menu(int a, int b); 
void gerenciarMoradores(Tarefa** tarefas, Morador** aux); 
void gerenciarRepublica(int flag, Morador** moradores, Tarefa** tarefas, Rep* aux); 
void controledeTarefas(int flag,  Tarefa** aux); 
void controleFinanceiro(int flag, Lista* aux); 

int main(int argc, char const *argv[]) {
Rep* rep;
Morador** moradores = new Morador*[12];
Tarefa** tarefas;
Lista* financeiro;
int flag;
cout << "\e[8;18;74t";
cout << "\033[2J\033[1;1H";
cout<<"Deseja abrir uma republica nova(1) ou deseja abrir uma republica salva(2)? (0 para 
sair): ";cin>>flag;
while(flag<0||flag>2){
  cout << "\e[8;18;74t";
  cout << "\033[2J\033[1;1H";
  cout<<"INVALIDO, por favor selecione  1-abrir uma republica nova||2-abrir uma republica 
salva||0-Sair : ";
  cin>>flag;
}
if(flag==0){
  cout << "\e[8;18;74t";
  cout << "\033[2J\033[1;1H";
  cout << "\t\t\tSAINDO!!!\n\n\n";
  sleep(1);
  cout << "\e[8;18;74t";
  cout << "\033[2J\033[1;1H";
  return 0;
}
else{
  gerenciarRepublica(flag, moradores, tarefas, rep);
  controleFinanceiro(flag, financeiro);
}
int op;
menu(0,0);
cin>>op;
while(op!=0){

  switch (op){
    case 1:
      gerenciarRepublica(0, moradores, tarefas,rep);
      menu(0,0);
      break;
    case 2:
      gerenciarMoradores(tarefas, moradores);
      menu(0,0);
      break;
    case 3:
      controledeTarefas(0, tarefas);
      menu(0,0);
      break;
    case 4:
      controleFinanceiro(0, financeiro);
      menu(0,0);
      break;
    default:
      menu(1, 0);
    break;
  }
  cin>>op;
}
cout << "\e[8;18;74t";
cout << "\033[2J\033[1;1H";
cout << "\t\t\tSAINDO!!!\n\n\n";
sleep(1);
cout << "\e[8;18;74t";
cout << "\033[2J\033[1;1H";
return 0;
}

morador.cpp (where the functions are implemented):

#include "morador.hpp"

struct morador{
string nome;
int idade;
int hierarquia;
Tarefa** tarefa;
string aniversario;
};

Morador* alocaMorador(string nome, int idade, int hierarquia, Tarefa** 
tarefa, string anniversario){
Morador* aux = new Morador;
aux->nome = nome;
aux->idade = idade;
aux->hierarquia = hierarquia;
aux->tarefa = tarefa;
aux->aniversario = anniversario;
return aux;
}
void imprimeMoradores(Morador** a){
for(int i=0; i<12; i++){
  cout<<pessoa::getNome(a[i])<<" Idade: "<<pessoa::getIdade(a[i]);
  if(a[i]->hierarquia == 12)
    cout<<" Bixo ";
  else if(a[i]->hierarquia == 11)
    cout<<" Semi-Bixo ";
  else if(a[i]->hierarquia <= 10&&a[i]->hierarquia>1)
    cout<<" Morador -"<<i;
  else
    cout<<" Decano";
  cout<<" | Nascimento "<<a[i]->aniversario<<endl;
}
}
void deletaMorador(Morador* a){
delete[] a;
}

string getNome(Morador* a){
return a->nome;
}
void setNome(Morador* a, string b){
a->nome = b;
}
int getIdade(Morador* a){
return a->idade;
}
void setIdade(Morador* a, int b){
a->idade = b;
}
int getHierarquia(Morador* a){
return a->hierarquia;
}
void setHierarquia(Morador* a, int hierarquia){
a->hierarquia = hierarquia;
}
string getAniversario(Morador*a){
return a->aniversario;
}
void setAniversario(Morador* a, string b){
a->aniversario = b;
}
lucas sa
  • 3
  • 1
  • 4

1 Answers1

1

You need to enclose your code in the .cpp file inside the same namespace declaration as in the header:

// your includes


namespace pessoa{
    // your cpp code here ...
}
Amir Kirsh
  • 12,564
  • 41
  • 74