How to split files in c ++

0

I'm starting my studies in C ++, and I'm not able to split into different files if they can help me.

Primary code

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

My dot.h file

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" >>;
 }

On the main main this error appears

All3filesareinthesamefolder

    
asked by anonymous 06.07.2018 / 17:31

1 answer

1

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

Your Celular class has no none constructor. In the main code, you try to instantiate the Celular class through a constructor that does not exist!

Change your primary code to:

#include "Celular.h"

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

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

Your .h file should look something like:

#ifndef CELULAR_H
#define CELULAR_H

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

#endif

And finally, your .cpp file, containing the implementation of class Celular , makes two syntax errors, which 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!
}
    
06.07.2018 / 22:48