How to split Files in c++

Asked

Viewed 530 times

0

I’m starting my studies in c++, and I’m not able to divide them into different files, if you can help me

Master code

#include <iostream> 
#include "Celular.h"
using namespace std;
int main(int argc, char** argv)
{
   Celular motorola();
   motorola.ligar();
  return 0;
}

My point file. h

class Celular
{
 public:
    void ligar();
}

My cpp file, which implements the function

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

 Celular:: void ligar()
 {
    cout << "consegui" >>;
 }

In main this error appears inserir a descrição da imagem aqui

All 3 files are in the same folderinserir a descrição da imagem aqui

  • Huh!? cout << "consegui" >>;!? I think this one >> shouldn’t be there. Or rather, use cout << "consegui" << endl;

  • It’s me confused, it’s just as a test, I’m starting today

  • 1

    The error does not seem to have to do with separating files, and the question is without relevant information to help, colco that the names of all files, including those that do not appear there in the code. Could be just distraction and typo.

1 answer

1


Your file division is correct, but some details should be noted:

Your class Celular does not have none constructor. In the main code, you try to instill the class Celular by means of a builder that does not exist!

Change your main code to:

#include "Celular.h"

int main(int argc, char** argv)
{
   Celular motorola; //Removido a chamada do construtor
   motorola.ligar();
  return 0;
}

In your file .h, the definition of the class Celular does not have ; (semicolon) at the end and also no sentinels to prevent redeclarations during the compilation stage.

Your file .h should be something like:

#ifndef CELULAR_H
#define CELULAR_H

class Celular
{
    public:
        void ligar();
};

#endif

And finally, your file .cpp, containing the implementation of the class Celular, makes two syntax errors, which fixed would make the file look like this:

#include <iostream>
#include "Celular.h"

using namespace std;

void Celular::ligar()   //O tipo de retorno estava no lugar errado!
{
   cout << "consegui" << endl;   //Havia um erro de sintaxe aqui!
}

Browser other questions tagged

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