() func (int ab) { //definition of the ABOVE function (in some other file).
int b;
b = abc;
}
In the definition, i wanted the argument to be int abc, but by mistake i entered as 'ab'. O.K., so the compiler will consider it to be an overloaded function, and C++ allows arguments to be declared without using them in the body.
But in the definition when I use 'abc', I get no error from the compiler, WHY??
'abc' does not exist in the definition and it isn't global (It just DOES NOT EXIST).
VC++ gives an error just fine. The code I compiled was
1 2 3 4 5 6 7 8 9 10 11
void func (int abc); //in header file.
void func (int ab) { //definition of the ABOVE function (in some other file).
int b;
b = abc;
}
int main() {
func( 5 );
return 0;
}
so the compiler will consider it to be an overloaded function
It won't, it has the same signature.
g++ also generates an error as it should. Are you sure you aren't using a different language? () func (int abc); isn't valid C++ syntax either.
void func (int abc); // The name 'abc' is ignored by the compiler.
somefile.cpp
1 2 3 4 5 6 7 8 9 10
void func (int ab) // the parameter name is 'ab'
{
int b;
b = abc; // Error: there is no variable named 'abc' in scope
}
int main() {
func( 5 );
return 0;
}
In the definition, i wanted the argument to be int abc, but by mistake i entered as 'ab'. O.K., so the compiler will consider it to be an overloaded function, and C++ allows arguments to be declared without using them in the body.
But in the definition when I use 'abc', I get no error from the compiler, WHY??
The name of the parameter in the declaration (in the .h file) is not used by the compiler, it is only there for the human reader. The only important name, from the compilers point of view, is the one in the definition (in the .cpp file). As abc doesn't match the any name in any visible scope, an error is raised.
Sorry for my error, '()', before the function, clumsy hands.
Here is my code:
SPDT::SPDT(bool relay) {
SPDT::condition = false;
SPDT::relay = relay;
}
Header has a class SPDT containing: public: SPDT(bool relay = false); //Constructor
private :
bool condition;
bool relay;
__________________
Now, if I do this:
SPDT::SPDT(bool rela) {
SPDT::condition = false;
SPDT::relay = relay; //Here, this line.
}
It Compiles, but in the third line, 'relay' does not exist. It would have existed if the argument was correct. Shouldn't the compiler give me a message (error)??
So, can i rewrite my code this way:
SPDT::SPDT(bool relay) {
condition = false;
relay = relay; //This looks wrong
}
Please explain Athar. If i wan't to refer to 'relay' in class SPDT, shouldn't i use 'SPDT::relay', why is the compiler accepting 'relay' as 'SPDT::relay'.
SPDT::SPDT(bool rela) { //had to be relay.
SPDT::condition = false;
SPDT::relay = relay; //Understood this line (assign to self, O.K.)
}
In the first line, I have given an identifier to the variable, BUT it has NOT been used (due to the error).
O.K. it will compile, BUT, shouldn't the compiler give me a warning that I have not used the variable 'rela'????
It does not give me any warning like, "unused variable 'rela'. If you don't want to use it, please do not give an identifier to the bool."
This is one of the warnings that is enabled by -Wextra.
It's not enabled by default because it generates tons of useless warnings for overridden virtual functions, where not using a parameter is rarely an error.