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.