My question is about the error: 'Function' "Was not declared in this Scope"

Asked

Viewed 3,892 times

1

I am creating a system of records and login as a form of practice, but I keep finding this error and although searched a little, I can not find the solution.

#include "registration.h"

//Outras porções do código

    if (password == "")

    {

    cout << "You aren't registered! \n\n";

    registration();

    }


//Registration.h

//Inicialização normal do header, iostream e namespace.

void registration()

{

    cout << "Choose a password: ";
    cin >> password;

    cout << "Successfully registered! \n\n";

    cout << "Now you must login. \n\n";

    login();

    prompt();

}

Error:

C: Program Files (x86) Codeblocks Projects authsystem login. h|22|error: 'Registration' was not declared in this Scope|

What I tried:

Declare that the function is void, but not manifest in the application. Declare function on itself .h, but does not work due to the order that the application is.

Another case where a virtually identical function is called, but it works:

    if (attempt == password)

    {

    cout << "Logged in successfully. \n \n";

    prompt();

    }
  • Do you understand Spanish? If so, take a look at this link: https://es.stackoverflow.com/a/54921

  • Not much, and translating the page doesn’t improve things. I updated the code on top.

  • Are you including code in a . h? This is usually not what you want.

  • That @Jeffersonquesado , so that everything did not stay in main.cpp, I decided to separate the functions in headers. Each header presents only the function, without any class, which does not seem to have given me problems in the past.

  • Perhaps I can help, perhaps I am even a duplicate of the question I answer here: https://answall.com/a/213804/64969

  • @jamescodec, one should avoid codes in header files (with the exception of template functions). There are exceptions beyond template functions, but your code is no exception. The correct one would be to have only the statement in the header and the code in another . cpp, doing the partial compilation of the source files. And, no, I don’t know how to do that in code::

Show 1 more comment

1 answer

0

You’re implementing functions and variables in the header files and they’re not exactly made for this.

The header files . h are like an interface that declares the functions, and the implementation of these functions are done in the files. cpp, this makes code more organized, easy to share and easy to reuse without having to reimplement the functions.

See this following example below:

sum. h

#ifndef SOMA_H
#define SOMA_H

// Declara a função.
int soma(int a, int b);

#endif

sum.cpp

#include "soma.h"

// Implementa a função.
int soma(int a, int b) {
    return a + b;
}

main.cpp

#include "soma.h"

#include <iostream>

int main() {
    int a = 10;
    int b = 5;

    // Utiliza a função soma declarada em soma.h
    int resultado = soma(a, b);

    std::cout << "Resultado: " << resultado << std::endl;

    return 0;
}

This link contains a question with answers precisely on this subject.

Creating your own header file

Browser other questions tagged

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