This error happens if a unknown object is used.
Not compiling:
#include <iostream>
int main(int argc, char *argv[])
{
{
int i = 2;
}
std::cout << i << std::endl; // i is not in the scope of the main function
return 0;
}
Fix:
#include <iostream>
int main(int argc, char *argv[])
{
{
int i = 2;
std::cout << i << std::endl;
}
return 0;
}
Most of the time this error occurs if the needed header is not included (e.g. using
std::cout
without#include <iostream>
)
Not compiling:
#include <iostream>
int main(int argc, char *argv[])
{
doCompile();
return 0;
}
void doCompile()
{
std::cout << "No!" << std::endl;
}
Fix:
#include <iostream>
void doCompile(); // forward declare the function
int main(int argc, char *argv[])
{
doCompile();
return 0;
}
void doCompile()
{
std::cout << "No!" << std::endl;
}
Or:
#include <iostream>
void doCompile() // define the function before using it
{
std::cout << "No!" << std::endl;
}
int main(int argc, char *argv[])
{
doCompile();
return 0;
}
Note: The compiler interprets the code from top to bottom (simplification). Everything must be at least declared (or defined) before usage.