matuco1998
matuco1998

Reputation: 53

why is it that my object is not declared in the scope?

Im having the typical c++ error of "x" is not declared in this scope.

This is my main.cpp:

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

using namespace std;


class Materia{

private:
    int nroMateria;
    char nombreMateria[40];
    int cantAlumnos;
    int cantDocentes;

public:

    void Cargar(){

        int nro;

        cout<<"NUMERO DE MATERIA: ";
        cin>>nro;
        setNroMateria(nro);
        cout<<"NOMBRE DE MATERIA: ";
        cin.ignore();
        cin.getline(nombreMateria,35);
        cout<<"CANTIDAD DE ALUMNOS: ";
        cin>>cantAlumnos;
        cout<<"CANTIDAD DE DOCENTES: ";
        cin>>cantDocentes;        
        
    }

    void Mostrar();


    void setNroMateria(int nm){if(nm>0)nroMateria=nm;}
    void setNombreMateria(const char *nm){strcpy(nombreMateria,nm);}
    int getNroMateria(){return nroMateria;}
    char *getNombreMateria(){return nombreMateria;}


};        
void Materia::Mostrar(){

        cout<<"NUMERO DE MATERIA: ";
        cout<<nroMateria<<endl;
        cout<<"NOMBRE DE MATERIA: ";
        cout<<nombreMateria<<endl;
        cout<<"CANTIDAD DE ALUMNOS: ";
        cout<<cantAlumnos<<endl;
        cout<<"CANTIDAD DE DOCENTES: ";
        cout<<cantDocentes<<endl;
        cout<<endl;
        cout<<"-------------------------"<<endl;
        cout<<endl;    
    
}



int main(){


    int opc;


    while(true){
        system("cls");
        cout<<"MENU ARTICULOS: "<<endl;
        cout<<"1. CARGAR MATERIAS: "<<endl;
        cout<<"2. MOSTRAR MATERIAS: "<<endl;
        cout<<"0. SALIR DEL PROGRAMA: "<<endl;
        cout<<"SELECCIONE UNA OPCCION: "<<endl;
        cin>>opc;
        system("cls");
        switch(opc){
        case 1:
            CargarMateria();
            break;
        case 2:
            MostrarMateria();
            break;

        case 0:
            return 0;
            break;

        }
        system("pause");
    }


return 0;
}

And i have my class Materia with all its properties and methods. And then i have a menu to trigger different options.

This is my funciones.h file:

void CargarMateria();
void MostrarMateria();

bool grabarEnDisco(Materia);

void CargarMateria(){


    Materia obj;

    obj.Cargar();

    if(grabarEnDisco(obj)==true){
        cout<<"REGISTRO AGREGADO";
    }else {


        cout<<"NO SE PUDO AGREGAR EL REGISTRO";

    }

    cout<<endl;

}


void MostrarMateria(){

    FILE *pMateria;
    Materia obj;

    pMateria = fopen("materias.dat", "rb");

    ///wb abre el archivo en modo escritura -> SIEMPRE CREA UN ARCHIVO VACIO
    ///ab abre el archivo y permite que se le agreguen registros.
    ///rb abre el archivo y permite leer los registros. Si no existe da error.

    if(pMateria == NULL){
        cout<<"ERROR DE ARCHIVO";
        return;
    }

    fread(&obj,sizeof(Materia),1,pMateria);
    fclose(pMateria);
    obj.Mostrar();       
    

}


bool grabarEnDisco(Materia obj){

    FILE *pMateria;

    pMateria = fopen("materias.dat", "wb");

    ///wb abre el archivo en modo escritura -> SIEMPRE CREA UN ARCHIVO VACIO
    ///ab abre el archivo y permite que se le agreguen registros.
    ///rb abre el archivo y permite leer los registros. Si no existe da error.

    if(pMateria == NULL){
        cout<<"ERROR DE ARCHIVO";
        return false;
    }

    fwrite(&obj,sizeof(Materia),1,pMateria);
    fclose(pMateria);
    return true;


}
#endif // FUNCIONES_H_INCLUDED

And above i have a few functions to generate write and read a file. But where i first have a prototype function:

bool grabarEnDisco(Materia);

I get

'Materia' was not declared in this scope

Any clue way this is happening?

Upvotes: 0

Views: 625

Answers (3)

user11717481
user11717481

Reputation: 1602

 Based on @alex`s answer Your functions header file is included before you defined Materia, so you can use everything in one file, example Materia.cpp as the following code:

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;


class Materia{

private:
    int nroMateria;
    char nombreMateria[40];
    int cantAlumnos;
    int cantDocentes;

public:

    void Cargar(){

        int nro;

        cout<<"NUMERO DE MATERIA: ";
        cin>>nro;
        setNroMateria(nro);
        cout<<"NOMBRE DE MATERIA: ";
        cin.ignore();
        cin.getline(nombreMateria,35);
        cout<<"CANTIDAD DE ALUMNOS: ";
        cin>>cantAlumnos;
        cout<<"CANTIDAD DE DOCENTES: ";
        cin>>cantDocentes;        
        
    }

    void Mostrar();


    void setNroMateria(int nm){if(nm>0)nroMateria=nm;}
    void setNombreMateria(const char *nm){strcpy(nombreMateria,nm);}
    int getNroMateria(){return nroMateria;}
    char *getNombreMateria(){return nombreMateria;}


};        
void Materia::Mostrar(){

        cout<<"NUMERO DE MATERIA: ";
        cout<<nroMateria<<endl;
        cout<<"NOMBRE DE MATERIA: ";
        cout<<nombreMateria<<endl;
        cout<<"CANTIDAD DE ALUMNOS: ";
        cout<<cantAlumnos<<endl;
        cout<<"CANTIDAD DE DOCENTES: ";
        cout<<cantDocentes<<endl;
        cout<<endl;
        cout<<"-------------------------"<<endl;
        cout<<endl;    
    
}

void CargarMateria();
void MostrarMateria();

bool grabarEnDisco(Materia);


int main(){


    int opc;


    while(true){
        system("cls");
        cout<<"MENU ARTICULOS: "<<endl;
        cout<<"1. CARGAR MATERIAS: "<<endl;
        cout<<"2. MOSTRAR MATERIAS: "<<endl;
        cout<<"0. SALIR DEL PROGRAMA: "<<endl;
        cout<<"SELECCIONE UNA OPCCION: "<<endl;
        cin>>opc;
        system("cls");
        switch(opc){
        case 1:
            CargarMateria();
            break;
        case 2:
            MostrarMateria();
            break;

        case 0:
            return 0;
            break;

        }
        system("pause");
    }


    return 0;
}

void CargarMateria(){


    Materia obj;

    obj.Cargar();

    if(grabarEnDisco(obj)==true){
        cout<<"REGISTRO AGREGADO";
    }else {


        cout<<"NO SE PUDO AGREGAR EL REGISTRO";

    }

    cout<<endl;

}


void MostrarMateria(){

    FILE *pMateria;
    Materia obj;

    pMateria = fopen("materias.dat", "rb");

    ///wb abre el archivo en modo escritura -> SIEMPRE CREA UN ARCHIVO VACIO
    ///ab abre el archivo y permite que se le agreguen registros.
    ///rb abre el archivo y permite leer los registros. Si no existe da error.

    if(pMateria == NULL){
        cout<<"ERROR DE ARCHIVO";
        return;
    }

    fread(&obj,sizeof(Materia),1,pMateria);
    fclose(pMateria);
    obj.Mostrar();       
    
}

bool grabarEnDisco(Materia obj){

    FILE *pMateria;

    pMateria = fopen("materias.dat", "wb");

    ///wb abre el archivo en modo escritura -> SIEMPRE CREA UN ARCHIVO VACIO
    ///ab abre el archivo y permite que se le agreguen registros.
    ///rb abre el archivo y permite leer los registros. Si no existe da error.

    if(pMateria == NULL){
        cout<<"ERROR DE ARCHIVO";
        return false;
    }

    fwrite(&obj,sizeof(Materia),1,pMateria);
    fclose(pMateria);
    return true;

}

 or simply by declaring Materia class inside funciones.h and use as header file, like this:


class Materia{

private:
    int nroMateria;
    char nombreMateria[40];
    int cantAlumnos;
    int cantDocentes;

public:

    void Cargar(){

        int nro;

        std::cout<<"NUMERO DE MATERIA: ";
        std::cin>>nro;
        setNroMateria(nro);
        std::cout<<"NOMBRE DE MATERIA: ";
        std::cin.ignore();
        std::cin.getline(nombreMateria,35);
        std::cout<<"CANTIDAD DE ALUMNOS: ";
        std::cin>>cantAlumnos;
        std::cout<<"CANTIDAD DE DOCENTES: ";
        std::cin>>cantDocentes;        
        
    }

    void Mostrar();


    void setNroMateria(int nm){if(nm>0)nroMateria=nm;}
    void setNombreMateria(const char *nm){std::strcpy(nombreMateria,nm);}
    int getNroMateria(){return nroMateria;}
    char *getNombreMateria(){return nombreMateria;}


};        
void Materia::Mostrar(){

        std::cout<<"NUMERO DE MATERIA: ";
        std::cout<<nroMateria<<std::endl;
        std::cout<<"NOMBRE DE MATERIA: ";
        std::cout<<nombreMateria<<std::endl;
        std::cout<<"CANTIDAD DE ALUMNOS: ";
        std::cout<<cantAlumnos<<std::endl;
        std::cout<<"CANTIDAD DE DOCENTES: ";
        std::cout<<cantDocentes<<std::endl;
        std::cout<<std::endl;
        std::cout<<"-------------------------"<<std::endl;
        std::cout<<std::endl;    
    
}
void CargarMateria();
void MostrarMateria();

bool grabarEnDisco(Materia);

void CargarMateria(){


    Materia obj;

    obj.Cargar();

    if(grabarEnDisco(obj)==true){
        std::cout<<"REGISTRO AGREGADO";
    }else {


        std::cout<<"NO SE PUDO AGREGAR EL REGISTRO";

    }

    std::cout<<std::endl;

}


void MostrarMateria(){

    FILE *pMateria;
    Materia obj;

    pMateria = std::fopen("materias.dat", "rb");

    ///wb abre el archivo en modo escritura -> SIEMPRE CREA UN ARCHIVO VACIO
    ///ab abre el archivo y permite que se le agreguen registros.
    ///rb abre el archivo y permite leer los registros. Si no existe da error.

    if(pMateria == NULL){
        std::cout<<"ERROR DE ARCHIVO";
        return;
    }

    std::fread(&obj,sizeof(Materia),1,pMateria);
    std::fclose(pMateria);
    obj.Mostrar();       
    

}


bool grabarEnDisco(Materia obj){

    FILE *pMateria;

    pMateria = std::fopen("materias.dat", "wb");

    ///wb abre el archivo en modo escritura -> SIEMPRE CREA UN ARCHIVO VACIO
    ///ab abre el archivo y permite que se le agreguen registros.
    ///rb abre el archivo y permite leer los registros. Si no existe da error.

    if(pMateria == NULL){
        std::cout<<"ERROR DE ARCHIVO";
        return false;
    }

    std::fwrite(&obj,sizeof(Materia),1,pMateria);
    std::fclose(pMateria);
    return true;


}
#endif // FUNCIONES_H_INCLUDED

Upvotes: 1

JDługosz
JDługosz

Reputation: 5642

Let me elaborate on what Alex said.

The contents of functiones.h appears to be a CPP file, and should not be a header file. For this simple program, that would typically go in the same file as the main. For a project, that should be functions.cpp (remove the #ifdef guard) and make a separate functiones.h that contains just the declarations of the functions that will be called from the main program.

Your program should look like this:

main.cpp

#include <iostream>
#include <cstdlib>
#include <cstring>
#include "materia.h"
#include "funciones.h"

int main()
{
   ⋮
}

functiones.h

void CargarMateria();
void MostrarMateria();

functiones.cpp

#include < any needed system files >
#include "materia.h"
#include "functiones.h"  // always include "my own" header too.

// All the functions you defined in the original functiones.h

materia.h

#pragma once
#ifdef SOME_UUID_THAT_WILL_BE_GLOBALLY_UNIQUE
#define that same thing

#include < any needed system files >

class Materia
{
    ⋮
};


#endif

In addition, you have one member function of class Materia that is not defined inside the class body. That needs to go into a materia.cpp file, or if you put it in the header file (still outside the class body) you need to use the inline keyword.

meanwhile...

Don’t write using namespace std;.

You can, however, in a CPP file (not H file) or inside a function put individual using std::string; etc. (See SF.7.)

⧺SL.io.50 Don't use endl.

Prefer using the C++ library features rather than the old C library. You're using FILE* stuff, but you also use cin and cout in your program. Really really don't use strcpy and similar, or pass around const char* for strings.

Don't use the C macro for NULL. C++ has a proper typed nullptr. But, don't directly compare pointers against nullptr; rather, use the truth value directly. if(pMateria == NULL) should be if(pMateria). But if you were using fstream rather than FILE* you would not be doing that anyway.

The style in C++ is to put the * or & with the type, not the identifier. This is called out specifically near the beginning of Stroustrup’s first book, and is an intentional difference from C style.

Upvotes: 1

Alex
Alex

Reputation: 1924

Your functions header file is included before you defined Materia. Therefore, when the compiler starts compiling your main, it sees a function that takes a parameter of some undefined type, and tells you that this type hasn't been declared yet.

You can either move the include to be below the class definition, place a forward declaration of the class before the functiones.h include, or, my personal recommendation, move the Materia class into its own header file and include that from main and functiones.h.

Upvotes: 2

Related Questions