Unreclaimed functions?

0

I have these 3 functions that I use to change the colors of my program:

#include <cstdlib>

void color_correcto()
system ("color 4a");
}
void color_error()
{
system ("color 4e");
} 
void color_original()
{
system ("color 4f");
}

They are declared in a Header called "Functions.h". My problem is when I go to another header (Let's call it Test), in which I should include Functions.h if I wanted to use the functions to declare in it, right?:

#include "Funciones.h"

color_correcto();
color_error();
color_original();

The main would look like this:

#include <iostream>
using namespace std;
#include "Funciones.h"
#include "Test.h"
#include <cstdlib>

int main ()

{

color_correcto();
color_error();
color_original();

 return 0;
}

Well, when I do this and compile I get an error saying that these functions were not declared in the header Test, even though I included the header "Functions.h" in advance. Does anyone know what the problem could be? ?

    
asked by FranciscoFJM 07.05.2017 в 17:39
source

2 answers

1

C ++ does not understand file formats. What happens is that to try to reduce confusion it has been customary for header files to have the extension .h or .hpp and the code files .c or .cpp .

The header files must include the declaration of functions and classes that are going to be public , that is, those that are going to be used in different parts of the application (to begin with assume that they are all of this type). and in the code or implementation files, the implementation of these functions and classes is usually put.

Then your code should be structured like this:

Functions.h

// Esto es una guarda, ya entenderás su función más adelante.
#ifndef FUNCIONES_H
#define FUNCIONES_H

color_correcto();
color_error();
color_original();

#endif 

Functions.c

#include <cstdlib>
#include "funciones.h"

void color_correcto()
{ // Te falta esta llave en tu codigo
  system ("color 4a");
}

void color_error()
{
  system ("color 4e");
} 
void color_original()
{
  system ("color 4f");
}

With these changes you should already run your program ... although if you do not print anything in the console I do not think you can verify if the commands work or not.

    
answered by 07.05.2017 в 18:49
0

You do it the other way around, in the .h the function declarations go (without the implementation, only ending in the ; ) and in the .c the definition (with the implementation)

A declaration or definition of a function includes the type that they return (or void if they do not return anything). As they are missing, the compiler tries to interpret the code, and thinks that they are invocations to these functions, instead of its declaration.

int color_correcto();
int color_error();
void color_original();

or how to tap.

And, in another order of things, in C there is no "loose" code as there may be in Javascript or PHP. Everything must be part of a function (the initial function is main ).

    
answered by 07.05.2017 в 18:07