0

I'm using Visual Code Studio, so please, answer me in a way I can do it with that platform.

I'm getting the following error while trying to compile:

[main.cpp 2023-04-05 04:55:22.517]
,,C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MyName\AppData\Local\Temp\ccWOnMhK.o:main.cpp:(.text+0x18): undefined reference to `Aux_main::Aux_main()'
collect2.exe: error: ld returned 1 exit status

main.cpp:

int main(){

    Aux_main Objeto_Aux_main;

    return 0;

}

Aux_main.h:

#include "Pessoa.h"

#ifndef AUX_MAIN_H
#define AUX_MAIN_H

class Aux_main{
    
    private:
        Pessoa Einstein;
        Pessoa Newton;
        int diaAtual, mesAtual, anoAtual;

    public:
        Aux_main();
        void Executar();

};

#endif

Aux_main.cpp:

#include "Aux_main.h"

Aux_main::Aux_main(){

    Einstein.Inicializa(14,3,1879, "Albert Einstein");
    Newton.Inicializa(4,1,1643, "Isaac Newton");

    std::cout << "Informe o dia/mes/ano." << endl;
    std::cin >> diaAtual >> mesAtual >> anoAtual;

    Executar();

}

void Aux_main::Executar(){

    Einstein.CalcIdade(diaAtual,mesAtual,anoAtual);
    Newton.CalcIdade(diaAtual,mesAtual,anoAtual);

}

Pessoa.cpp:

#include "Pessoa.h"
using namespace std;
#include <iostream>

using std::cout;
using std::endl;

Pessoa::Pessoa(int diaN, int mesN, int anoN, const char* nomeN){
    Inicializa(diaN, mesN, anoN);
}

Pessoa::Pessoa(){
    Inicializa(0,0,0);  
}

void Pessoa::Inicializa(int diaN, int mesN, int anoN, const char* nomeN){
    dia = diaN;
    mes = mesN;
    ano = anoN;
    idade = -1; 
    strcpy(nome, nomeN);
}

void Pessoa::CalcIdade(int ano_atual, int mes_atual, int dia_atual){  
    idade = ano_atual - ano;
    if(mes_atual<mes){
        idade--;
    }
    else if(mes_atual==mes){
        
        if(dia_atual<dia){
            idade--;
        }
    }

    cout << "A idades de " << nome << " no ano de 2023 seria de " << idade << " anos.\n" << endl;

}

int Pessoa::getIdade(){
    return idade;
}

Pessoa.h:

#include <stdio.h>
#include <string.h>
#include <iostream>

using std::cout;
using std::endl;

#ifndef PESSOA_H
#define PESSOA_H

class Pessoa{

    private:
   
        int dia, mes, ano, idade;
        char nome[30];

    public:

        Pessoa(int diaN, int mesN, int anoN, const char* nomeN = "");
        Pessoa();
        void Inicializa(int diaN, int mesN, int anoN, const char* nomeN = "");
        void CalcIdade(int ano_atual, int mes_atual, int dia_atual);
        int getIdade();

};

#endif
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
arnf
  • 1
  • You are getting a linker error, not a compiler error. You are not linking the compiled object files correctly. On a side note, not related to the error, your `main.cpp` is missing `#include "Aux_main.h"`, and your `.h` files are not using header guards correctly. And, your `using` statements in `Pessoa.h` don't belong there. – Remy Lebeau Apr 05 '23 at 05:25
  • You need to edit `tasks.json` so that it compiles all your .cpp files not just `main.cpp`. The VSCode documentation shows you how to do this. – john Apr 05 '23 at 05:36
  • I edited my tasks.json following vs code documentation and I still get the same error. .json args: "args": [ "-fdiagnostics-color=always", "-g", "${workspaceFolder}/*.cpp", "-o", "${workspaceFolder}\\app.exe" ], – arnf Apr 05 '23 at 05:57

0 Answers0