C++ function arguments

() func (int abc); //in header file.

() 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).

Tried it with xcode and code::blocks.
Last edited on
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;
}


Say, what's with the () before functions?
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.
closed account (z05DSL3A)

somefile.h
 
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)??

What's wrong.

I am using a Mac, haven't tried on Windows yet.

Last edited on
No, in this case relay refers to the class member relay, which you're assigning to itself.
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'.
Last edited on
closed account (z05DSL3A)
Sorry, you say it is not giving an error when you think that it should.

In the case above, SPDT::relay = relay;, there is a name relay that is within scope, so it is used. i.e. it is SPDT::relay = SPDT::relay;.

Try to avoid member names that are the same as parameter names.
Last edited on

SPDT::SPDT(bool relay) {
SPDT::condition = false;
SPDT::relay = relay; //Here, the right side 'relay'
}

Compiles.

O.K. Now, is the compiler referring to SPDT::relay or the 'relay' in the argument list?

What are the rules for the compiler?
Last edited on
closed account (z05DSL3A)
In that case, the compiler should match the relay without scope resolution to the parameter of that name as it is in local scope.

If it dose not find a name in the local scope it expands to the next enclosing scope to find the name and so on on until it gets to global scope.
Thank's a lot. For a second I thought that i had to rewrite my code (in case the compiler did [SPDT::relay = SPDT::relay; in the above case.)
Last edited on
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."
Last edited on
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.
Topic archived. No new replies allowed.