I
Immortal Nephi
C++ Compiler looks weird. It will succeed to compile if your source
code has some errors. I wrote an example of generic class. I
commented Errors.Print() in the main function body. Errors.Print()
has two variables _x and _y, but class definition only shows two
variables x and y.
Go ahead and try to compile. Read my comments below.
template< typename T >
class IgnoreErrors
{
public:
IgnoreErrors() : x( 1 ), y( 2 ) {}
~IgnoreErrors() {}
void Print();
T x;
T y;
};
template< typename T >
void IgnoreErrors< T >:rint()
{
_x = 5;
_y = 10;
}
int main()
{
IgnoreErrors<int> Errors;
// Errors.Print();
return 0;
}
------ Build started: Project: Errors, Configuration: Debug Win32
------
Compiling...
main.cpp
Linking...
Embedding manifest...
Build log was saved at "file://c:\My Projects\Errors\Errors\Debug
\BuildLog.htm"
Errors - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped
==========
Now, you uncomment Errors.Print(). C++ Compiler will fail to compile
and report two _x and _y undeclared identifier variables. You correct
to rename by removing underscore before variable name. Then, try to
compile. C++ Compiler reports no errors.
Do you have explanation why C++ Compiler ignores errors if you don’t
declare and define Errors.Print() in main function body?
------ Build started: Project: Errors, Configuration: Debug Win32
------
Compiling...
main.cpp
c:\my projects\errors\errors\main.cpp(20) : error C2065: '_x' :
undeclared identifier
c:\my projects\errors\errors\main.cpp(19) : while compiling
class template member function 'void IgnoreErrors<T>:rint(void)'
with
[
T=int
]
c:\my projects\errors\errors\main.cpp(27) : see reference to
class template instantiation 'IgnoreErrors<T>' being compiled
with
[
T=int
]
c:\my projects\errors\errors\main.cpp(21) : error C2065: '_y' :
undeclared identifier
Build log was saved at "file://c:\My Projects\Errors\Errors\Debug
\BuildLog.htm"
Errors - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
code has some errors. I wrote an example of generic class. I
commented Errors.Print() in the main function body. Errors.Print()
has two variables _x and _y, but class definition only shows two
variables x and y.
Go ahead and try to compile. Read my comments below.
template< typename T >
class IgnoreErrors
{
public:
IgnoreErrors() : x( 1 ), y( 2 ) {}
~IgnoreErrors() {}
void Print();
T x;
T y;
};
template< typename T >
void IgnoreErrors< T >:rint()
{
_x = 5;
_y = 10;
}
int main()
{
IgnoreErrors<int> Errors;
// Errors.Print();
return 0;
}
------ Build started: Project: Errors, Configuration: Debug Win32
------
Compiling...
main.cpp
Linking...
Embedding manifest...
Build log was saved at "file://c:\My Projects\Errors\Errors\Debug
\BuildLog.htm"
Errors - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped
==========
Now, you uncomment Errors.Print(). C++ Compiler will fail to compile
and report two _x and _y undeclared identifier variables. You correct
to rename by removing underscore before variable name. Then, try to
compile. C++ Compiler reports no errors.
Do you have explanation why C++ Compiler ignores errors if you don’t
declare and define Errors.Print() in main function body?
------ Build started: Project: Errors, Configuration: Debug Win32
------
Compiling...
main.cpp
c:\my projects\errors\errors\main.cpp(20) : error C2065: '_x' :
undeclared identifier
c:\my projects\errors\errors\main.cpp(19) : while compiling
class template member function 'void IgnoreErrors<T>:rint(void)'
with
[
T=int
]
c:\my projects\errors\errors\main.cpp(27) : see reference to
class template instantiation 'IgnoreErrors<T>' being compiled
with
[
T=int
]
c:\my projects\errors\errors\main.cpp(21) : error C2065: '_y' :
undeclared identifier
Build log was saved at "file://c:\My Projects\Errors\Errors\Debug
\BuildLog.htm"
Errors - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========