473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

function default arguments from other arguments

Hello,

to make it short, why does this not compile?

class B {
public:
int b(void) { return 1;}
}
void f( B x, int i = x.b()) { }

in gcc 3.3 I get error: 'x' was not declared in this scope

But...I just declared x before. Why can't I use it?

Henning

Oct 17 '06 #1
4 2364
* tutmann:
Hello,

to make it short, why does this not compile?

class B {
public:
int b(void) { return 1;}
}
void f( B x, int i = x.b()) { }

in gcc 3.3 I get error: 'x' was not declared in this scope

But...I just declared x before. Why can't I use it?
You mean, why does the language forbid that?

Presumably to simplify things for the compilerm, but ask in [comp.std.c++].

Simple workaround is to overload f.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 17 '06 #2
tutmann wrote:
Hello,

to make it short, why does this not compile?

class B {
public:
int b(void) { return 1;}
}
void f( B x, int i = x.b()) { }

in gcc 3.3 I get error: 'x' was not declared in this scope

But...I just declared x before. Why can't I use it?
GGG's message is extremely misleading actually. x is already declared
at that point (and hides any other 'x' that may have been declared
before), but cannot be used in a default argument expression. The
reason is that the order of evaluation of function arguments is
unspecified.

An obvious workaround is a one-argument overload that calls the
two-argument function.

Regards,
Bart.

Oct 17 '06 #3

tutmann wrote:
Hello,

to make it short, why does this not compile?

class B {
public:
int b(void) { return 1;}
}
semicolon required - B is a class declaration (and definition here)
Also, int b() is attempting to return a literal - a const integer so...

class B
{
public:
int b() const { return 1; }
}; // <- semicolon
void f( B x, int i = x.b()) { }
the first x is an instance of B, the second x does not exist, your
compiler might accept the function if you reverse the parameters but
its still a bad idea and officially undefined behaviour. Isn't the goal
to pass an instance of B into the function enough? Why would you need
to add an uneccessary integer parameter?

#include <iostream>

class B
{
public:
int b() const { return 1; }
};

void f(B x)
{
int i = x.b(); // i needs not be in the parameter list
std::cout << "i = " << i << std::endl;
}

int main()
{
B b;
f(b);

return 0;
}

/* output:
i = 1
*/
>
in gcc 3.3 I get error: 'x' was not declared in this scope

But...I just declared x before. Why can't I use it?
because gcc happens to process the parameters from right to left. So
that x is undeclared. Note that the sequence with which the parameters
gets processed doesn't matter. A Parameter should never, ever be a
dependant of another parameter.

You could have written something like:
void f( B x, int i = B z.b()) { }
but even that defies any logic here.

Maybe if you wrote class B differently you might understand when more
than one parameter makes sense.

#include <iostream>

class B
{
int b; // private member
public:
B(int n) : b(n) { } // ctor
int get() const { return b; } // accessor
};

void bar( B xval, B yval = B(22) ) // 2 seperate values or instances
{ // one default
param provided via ctor + copy
std::cout << "\nx = " << xval.get() << std::endl;
std::cout << "y = " << yval.get() << std::endl;
int i = yval.get();
std::cout << "i = " << i << std::endl;
}

int main()
{
B x(4);
B y(9);
bar(x, y); // pass by value
bar(x); // yval is default initialized

return 0;
}

/*
x = 4
y = 9
i = 9

x = 4
y = 22
i = 22
*/

Consider getting a book like this one http://www.acceleratedcpp.com
There is enough material there to last you a lifetime.

Oct 17 '06 #4
Salt_Peter wrote:
tutmann wrote:
void f( B x, int i = x.b()) { }

the first x is an instance of B, the second x does not exist, your
compiler might accept the function if you reverse the parameters but
its still a bad idea and officially undefined behaviour.
That is incorrect. According to official C++ rules, parameters declared
before the default argument expression are in scope. The **order of
evaluation** is unspecified, but it has nothing to do with scoping
rules.
in gcc 3.3 I get error: 'x' was not declared in this scope

But...I just declared x before. Why can't I use it?

because gcc happens to process the parameters from right to left.
That is not the reason for the error. GCC is right to give an error,
but the error message is a lie.

Regards,
Bart.

Oct 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
8
by: Jon Harrop | last post by:
I am trying to learn C# and .NET programming in general but I am finding it very hard going. To start with, I'd like to translate some trivial functions from other languages that I am familiar with...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.