How to compile c++ classes in Makefile?

Asked

Viewed 162 times

0

I was making a program in c++(normally I was using an IDE to compile and everything), but this time I needed to do it without an IDE, anyway when I did Makefile to compile the program, it gave an error with something related to the class constructor

g++ -c main.cpp g++ -c graphs.cpp In file included from graphs.cpp:1:0: /usr/include/c++/5/bits/stl_construct. h: In instantiation of ' void Std::_Destroy(_Tp*) [with _Tp = Vertice]': /usr/include/c++/5/bits/stl_construct. h:103:19: required from ‘static void std::_Destroy_aux< >::__destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = Vertice*; bool = false]’ /usr/include/c++/5/bits/stl_construct. h:127:11: required from ' void Std::_Destroy(_Forwarditerator, _Forwarditerator) [with _Forwarditerator = Vertice*]' /usr/include/c++/5/bits/stl_construct. h:151:15: required from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>&) [with _ForwardIterator = Vertice*; _Tp = Vertice]’ /usr/include/c++/5/bits/stl_vector. h:424:22: required from ' Std::vector<_Tp, _Alloc>:~vector() [with _Tp = Vertice; _Alloc = Std::allocator]' graphs.cpp:3:14: required from here graphs. h:11:2: error: >Vertice::~Vertice()' is private ~Vertice(); In file included from /usr/include/c++/5/vector:62:0, from graphs. h:5, from graphs.cpp:1: /usr/include/c++/5/bits/stl_construct. h:93:7: error: Within this context { __Pointer->~_Tp(); } Makefile:15: Recipe for target 'graph. o' failed make: *** [graph. o] Error 1

graphs. h

#ifndef GRAFOS_H
#define GRAFOS_H
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Vertice{
private:
    ~Vertice();
    int valor;
    int peso;
    vector<Vertice> listaAdjArest;
};

class Grafo{
private:
    vector<Vertice> lista_v;
public:
    Grafo();
    ~Grafo();
    void InsereVertice(int valor);
    void InsereAresta(int valor,int peso);
    void ImprimirGrafo();
    void ImprimirTopologicamente();
    void ImprimirCaminhoCritico();
};

#endif

graphs.cpp

#include "grafos.h"

Grafo::Grafo()
{

}

Grafo::~Grafo(){

}

void Grafo::InsereVertice(int valor){

}

void Grafo::InsereAresta(int valor,int peso){

}

void Grafo::ImprimirGrafo(){

}

void Grafo::ImprimirTopologicamente(){

}

void Grafo::ImprimirCaminhoCritico(){

}

main.cpp

#include "grafos.h"
#include "dados.h"

int main(){
    return 0;
}

Makefile

all:Executar limpar
Executar: main.o grafos.o dados.o
    g++ -Wall -o Projeto2 main.o grafos.o dados.o

main.o:main.cpp grafos.cpp grafos.h dados.cpp dados.h
    g++ -c main.cpp

grafos.o:grafos.cpp grafos.h
    g++ -c grafos.cpp

dados.o:dados.cpp dados.h
    g++ -c dados.cpp

limpar:
    rm -rf *.o

I don’t know for sure what’s going on with the program,I’ve tried to get a few things out of Makefile,but still keep having this problem,Does anyone know of any solution that can make Makefile compile C++ files along with classes and constructors? Whoever can help me I thank you

OBS.:the git link of the program, in case any of you want to test to better analyze the problem https://github.com/RenanZX/Projeto2-Grafos , it can be observed that the code is empty, only classes and methods have been defined in libraries and nothing else

OBS2.:no c never had this problem,compiled without any error or anything like that, but only in the c++ that is giving this problem,and the IDE’s compile this program without any problems

  • 1

    It has how to place the relevant parts of main.cpp and of grafos.cpp? I also believe there is a grafos.h with relevant data

  • Also, usually when doing partial compilation, it is usually focused on one source file at a time, not two. Usually there is the presence of two source files to compile them together and soon generate the executable, thing you do not see in the error output

  • already added, including includes the Makefile

  • i think I understood what you meant, but that’s not the problem in question,in file generation . o,I’m putting both the file. cpp implementation for headers . h

  • From the error message, maybe it has something to do with the destructor’s statement of Vertice (Vertice::~Vertice) be private. I also saw no declaration of the method in .cpp of their respective header

  • No, it’s not,I did some tests here,and when I took everything I had put with <vector>,it worked! turned out fine, but when I leave the vector it gives this problem that you saw

  • i put the Makefile that worked, what remains is to figure out how to compile the <vector> using the Makefile

Show 2 more comments

1 answer

1

I solved the problem (simpler than I imagined),for those who have the same problem at compilation time, <vector> is automatically destroyed when terminating the program when it is generated, so the compiler was accusing errors, because in the class Vertice there was a destroyer ~Vertice(),and the vector itself already has a destructor and this was generating a conflict at the time of compilation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.