'Class' was not declared in this scope

Hi, so i've got this error when i compile my program and i don't understand why

1
2
[build] ...In function ‘void make_db_corso()’:
[build] ...error: ‘Corso’ was not declared in this scope

here my files:

cmakelist.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cmake_minimum_required(VERSION 3.0.0)
project(progetto VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

include(CTest)
enable_testing()

add_executable(progetto 
               src/main.cpp
               src/albero.h
               src/albero.cpp
               src/aula.h
               src/aula.cpp
               src/personale.h
               src/personale.cpp
               src/materia.h
               src/materia.cpp
               src/utility.hpp
               src/utility.cpp
               )
              


set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

the function is inside a .hpp file
1
2
3
4
5
6
7
8
9
10
11
12
#include "materia.h"
    .
    .
    .
template <typename T>
void make_db_corso()
{
    Corso x;
}
    .
    .
    .


materia.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef MATERIA_H
#define MATERIA_H

#include <string>
#include <vector>

#include "albero.h"
#include "utility.hpp"

using namespace std;

class Corso
{
    private:
        string id;
        string titolo;

        int ore_lab;
        int ore_es;
        int ore_lez;
        int cfu; 

        //bool is_triennale;
        
        //vector<anno> lista_anni;

    public:
    Corso();
    Corso( NodoAlbero *radice );
    ~Corso();

    // metodi set
    void set_id( const string & s ){ id = s; };
    void set_titolo( const string & s ){ titolo = s; };
    void set_cfu( const int & x ){ cfu = x; };
    void set_ore_lab( const int & x ){ ore_lab = x; };
    void set_ore_es( const int & x ){ ore_es = x; };
    void set_ore_lez( const int & x ){ ore_lez = x; };
    //void set_is_triennale( const bool & b ){ is_triennale = b; };

    //metodi get
    string get_id(){ return id; };
    string get_titolo(){ return titolo; };
    int get_cfu(){ return cfu; };
    int get_ore_lab(){ return ore_lab; };
    int get_ore_es(){ return ore_es; };
    int get_ore_lez(){ return ore_lez; };
    //bool get_is_triennale(){ return is_triennale; };

    //METODI
    void nuovo_corso( string s ){};  

};

#endif 


materia.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "materia.h"

Corso::Corso(){}

Corso::Corso(NodoAlbero *radice )
{
    vector<string> tmp;

    get_chiavi_nodi( radice, &tmp );

    if( tmp[0] == "c" )
    {
        if( tmp.size() != 7 )
        {
            throw out_of_range( "NUMERO DI PARAMETRI ERRATO" );
            exit( EXIT_FAILURE );
        }

        set_id( tmp[1] );
        set_titolo( tmp[2] );
        set_cfu( stoi( tmp[3] ) );
        set_ore_lez( stoi( tmp[4] ) );
        set_ore_es( stoi( tmp[5] ) );
        set_ore_lab( stoi( tmp[6] ) );
    }
    else if( tmp[0]== "a" )
    {
        //anno ANNO( radice );
    }
    else
    {   
        cerr << "ERRORE STRINGA INVALIDA" << endl;
        //errore
    }
}

Corso::~Corso(){}



SOLVED BY GENADO:
Dependency problem.

I've include the .hpp file inside materia.h and materia.h inside .hpp file running into a circular dependency".

Solution: i delete "#include "utility.hpp"" in materia.h and move the function i need in another file.
Last edited on
the function inside a .hpp file
Which .hpp file? The only hpp file you seem to reference is utility.hpp. But you #include utility.hpp within materia.h before you define the Corso class.

Logically, you're running into a circular dependency because materia.h depends on utility.h, but utility.h depends on materia.h. At least, that is my guess based on the provided information.
Last edited on
At first i think it too, but inside .hpp file i use in this way also other class that don't make any errors.
Is it better don't make this kind of "circular dependency" beetween class and header files?
Does your utility.hpp file have header guards? That actually might be part of the problem if it's not a true circular dependency.

For example, the following will run into
Utility.hpp:6:5: error: 'Corso' was not declared in this scope
     Corso corso;


Utility.hpp
1
2
3
4
5
6
7
#include "Corso.hpp"

template <typename T>
void make_db_corso()
{
    Corso corso;
}


Corso.hpp
1
2
3
4
5
6
7
8
#ifndef CORSO_HPP
#define CORSO_HPP

#include "Utility.hpp"

class Corso { };

#endif 


main.cpp
1
2
3
4
5
6
#include "Utility.hpp"

int main()
{
	make_db_corso<int>();
}


#including something is essentially just a copy-paste mechanism. So main.cpp navigates into Utility.hpp, which then navigates into Corso.hpp, which then navigates into Utility.hpp, when then attempts to navigate into Corso.hpp, but now there are header guard preventing the infinite cycle, but here it sees "Corso x;" in Utility before it knows the definition of Corso.hpp

It works once you add include guards:
Utility.hpp
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef UTILITY_HPP
#define UTILITY_HPP

#include "Corso.hpp"

template <typename T>
void make_db_corso()
{
    Corso corso;
}

#endif 


The only reason this works is because there isn't a true circular dependency, because Corso doesn't actually need to #include Utility. So if there were a true circular dependency, the code wouldn't be able to compile.
Last edited on
Yes, i've include the #ifndef UTILITY_HPP #define UTILITY_HPP.

But you 'are right, now i commented the "get_chiavi_nodi( radice, &tmp );" inside materia.cpp (in fact the only reason why i need the "utility.h" file ) and the "#include "utility.hpp" inside materia.h and it compiles without any errors.

It's still really strange, i don't understand why with the other class compiler doesn't complains.
You'd have to show the full context of the other cases that don't give issues, and somebody could try to explain why it isn't a problem.
Nevermind, i've searching for the dependency with other class but in fact looking again the other functions i don't use any objects or methods of other class, i just forgot to delete the #including of the other class that mayby i had used someway during the writing.

THANK YOU GENADO, for you're fast response and for resolve my problem!
Topic archived. No new replies allowed.