Skip to content
'...' was not declared in this scope

'...' was not declared in this scope

DodaTech 3 min read

The '...' was not declared in this scope error means the compiler found a name it does not recognize. The identifier was not declared before use in this scope.

What It Means

The compiler maintains a symbol table of all declared names. When you use a variable, function, or type, the compiler checks this table. If the name is missing, you get this error at the point of use.

Why It Happens

  • Missing #include directive for the header that declares the symbol
  • Typo in the identifier name (case-sensitive in C++)
  • Missing using declaration or namespace qualification
  • Variable used before its declaration in the same scope
  • Accessing a member from the wrong scope (e.g., ClassName::member vs object.member)

How to Fix It

Step 1: Add the missing #include

// bad.cpp
int main() {
    std::cout << "Hello" << std::endl; // error: 'cout' not declared
    return 0;
}
// fixed.cpp
#include <iostream>

int main() {
    std::cout << "Hello" << std::endl;
    return 0;
}

Step 2: Check for typos and case

#include <iostream>

int main() {
    int myVariable = 10;
    std::cout << myvariable << std::endl; // error: 'myvariable' not declared
    return 0;
}

Fix: myvariablemyVariable. C++ identifiers are case-sensitive.

Step 3: Fix namespace issues

#include <iostream>

int main() {
    cout << "Hello" << std::endl; // error: 'cout' not declared
    return 0;
}

Fix: Use std::cout or add using std::cout; or using namespace std;.

#include <iostream>
using std::cout;

int main() {
    cout << "Hello" << std::endl;
    return 0;
}

Step 4: Declare variables before use

#include <iostream>

int main() {
    std::cout << value << std::endl; // error: 'value' not declared
    int value = 42;
    return 0;
}
// fixed
int main() {
    int value = 42;
    std::cout << value << std::endl;
    return 0;
}

Step 5: Correct scope when accessing class members

struct Point {
    int x, y;
};

int main() {
    Point p;
    p.x = 10;
    p.y = 20;
    int sum = Point::x + Point::y; // error: 'x' not declared in this scope
    return 0;
}

Fix: Use p.x and p.y (instance members), not Point::x (which requires static).

What is the difference between declaration and definition?
A declaration introduces a name into scope (e.g., int x; or void foo();). A definition provides the storage or implementation. You must declare a name before using it. A declaration can appear multiple times; a definition appears once (the One Definition Rule).
Why does the error mention 'in this scope'?
C++ uses block scopes delimited by {}. A variable declared inside a block is only visible within that block and nested blocks. If you try to access it outside, you get “not declared in this scope”. Declare variables at the minimum scope needed.
How do I check what header includes a specific function?
Search for the function in C++ reference docs or use grep -r "functionName" /usr/include/ on Linux. Most standard functions are documented with their required header. For example, std::vector needs #include <vector>, std::string needs #include <string>.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro