Object oriented: "Undefined Reference to" in main.cpp

Asked

Viewed 448 times

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:

inserir a descrição da imagem aqui

.

recognizing in vs code the attributes in main:

inserir a descrição da imagem aqui

1 answer

0


Good.. I managed to solve in two methods:

- Primeiro (errado):

Just take the #include "Carro.h" of the file . cpp and put at the end of the file . h #include "Carro.cpp"

But this is not recommended.

The #include basically pulls what’s written in that file/library and puts it in place. When we include the definitions of our attributes below the created attributes two things happen:

  • Loses the essence of creating another file. you can do this in one file, without having to separate in others;

  • In larger projects you may have conflicts; When you put the arquivo.h (ou .hpp) at the beginning of arquivo.cpp vc is placing its defined attributes of the class on top of the code, so that below you can define each attribute. And that you can do again on arquivo2.cpp, define the attributes and dps configure them again without interference.

.

- Segundo (certo)

Just put the #include "Carro.h" at the beginning of arquivo.cpp and when compiling should have compiled the two files: main.cpp and arquivo.cpp.

.

.

In this video (insert link description here) shows how to debug a program in C/C++ simply by pressing F5 in VS Code, I used it to configure my compilation.

To compile two files, just add one more file in the task.json:

// Meu task.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "main.cpp", 
                "-g",
                "Vetor.cpp",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\MinGW\\bin"
            }
        }
    ]
}

Note that in my arguments ("args") I put two files, so it runs normally :)

PS: Every time you go to debug (press F5) you need to place the location of the file(s) in the "args" (the program will run in an external (terminal) window). If you just want to run the program you need to download an extension ("C/C++ Compile Run") and press F6 (the program will run on the VS Code terminal itself)

WARNING: The extension only runs a single file. To use VS Code to compile you need the task.json where vc tells which files should be compiled.

If you don’t want all this work, just download an IDE that it already does all this for you.

  • 1

    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?

  • 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.

  • 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)

  • 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.

  • 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.

  • 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.

  • 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!

  • 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

Show 3 more comments

Browser other questions tagged

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