0
I’m starting POO (Object Oriented Programming) and am separating my attributes and methods from the class into different files.
They are reading each but in main.cpp is giving the error "Undefined Reference to.. "
The strange thing is that when I start typing to use some method, it appears for me to choose as shortcut
I’m using the VS Code;
ps: I’m using tag as int for testing, to see if it runs; If I write normal code everything in a file works normal. .
main.cpp:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Carro.h"
using namespace std;
int main(){
Carro c1;
c1.setMarca(3);
c1.setAno(2018);
Carro c2;
c2.setMarca(4);
c2.setAno(2020);
cout << c1.getAno() ;
return 0;
}
Car.cpp:
#include <stdlib.h>
#include "Carro.h"
// Marca do carro:
int Carro::setMarca (int marca) {
this->marca = marca;
}
int Carro::getMarca () {
return marca;
}
// Ano do carro:
int Carro::setAno (int ano) {
this-> ano = ano;
}
int Carro::getAno () {
return ano;
}
Car. h:
#ifndef CARRO_H
#define CARRO_H
#include <stdlib.h>
class Carro {
private:
int ano, marca;
public:
int setMarca(int marca);
int getMarca();
int setAno(int ano);
int getAno();
};
#endif // CARRO_H
.
error:
.
recognizing in vs code the attributes in main:
The solution you found is wrong. In a little bigger thing project you will get the problem of multiple definitions of the same function. By the way which compiler are you using (Clang, Mingw, Gcc)? Are you compiling with commands on the terminal or are using the
tasks.json
or something worth it?– user142154
I didn’t understand the problem I would have in multiple settings. My mistake was that this form of programming was not working, separated into three files. My question was how to read files with each other (basically) and this way it works, everything is being imported to where it should. To progress I use VS Code, I set up task.json to debug and compile and I also installed an extension that goes straight to the terminal. Compilation is done by Gcc (Mingw), so I set up VS Code to debug on top of that.
– Gui Reis
The only difference , basically, between the tutorials I found on the internet and in the online courses was to import the file . cpp in the file . hpp (or . h). . In the tutorials eh on the contrary, the . cpp imported the . hpp (or . h)
– Gui Reis
What
#include
does is basically take the text from one file and include it in another file. In practice, what the solution you find is to include all the methods definitions of the Car class below the class. This ends the whole purpose of Voce having two files, because for all intents and purposes you have only one. And why is that a problem? In a larger project, suppose you have more files of type . cpp you need to include the "Car. h", each such file with the inclusion will have its own copy of the method definitions.– user142154
The compiler will compile these definitions and when Linker gathers the project, it will find multiple method definitions and complain. Is that always the case? No, in your case, Linker took it well because there is only one translation unit. There are also two important exceptions: templates and classes with inline functions that must contain the declaration and definition in the same file.
– user142154
So, to properly resolve your original question, well, I know it’s a Linker problem, I know you need to set up tasks.json for Eke to understand that there is more than one file that needs to be compiled and linked, but to be honest, in C and C++, I prefer IDE. Text editors, make and now json files with Vscode are a little more complicated to configure and I avoid doing this when I can.
– user142154
Mmmmm, got it!!!!! Mt obr for the explanation. I’ll see what I do if I try to set up in my VS Code or move to an IDE. Even for the explanation!
– Gui Reis
I was able to compile the two files, tidied and left in the normal way, putting . hpp in .cpp. .. I left the code in my reply
– Gui Reis