What is wrong with this code?

K

kirk_korver

Group,

I'm trying to understand the problem with the following code. The
problem I get is associated with the line "int fmt = format(); " at
the bottom of the line. The error I get says that "format does not
take 0 arguments". It appears that the version of "format" that
returns an INT is NOT available to a derived class. Please notice that
there are 2 version of the format function.

Is this correct behavior? I get the same error with both the g++ and
the MSVC compilers.

Any help would be much appreciated.

--Kirk

<code>


class Parent {
public:
int format() { return m_format; }

int m_format;
};


class Child : public Parent {
public:
bool format(int fmt);
void foo();
};


bool Child::format(int frmt) {
m_format = frmt;
return true;
}

void Child::foo() {
format(3);
int fmt = format();
}

</code>
 
A

Alf P. Steinbach

* (e-mail address removed):
Group,

I'm trying to understand the problem with the following code. The
problem I get is associated with the line "int fmt = format(); " at
the bottom of the line. The error I get says that "format does not
take 0 arguments". It appears that the version of "format" that
returns an INT is NOT available to a derived class. Please notice that
there are 2 version of the format function.

Is this correct behavior? I get the same error with both the g++ and
the MSVC compilers.

Any help would be much appreciated.

--Kirk

<code>


class Parent {
public:
int format() { return m_format; }

int m_format;
};


class Child : public Parent {
public:
bool format(int fmt);
void foo();
};


bool Child::format(int frmt) {
m_format = frmt;
return true;
}

void Child::foo() {
format(3);
int fmt = format();
}

</code>

See the FAQ.
<url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9>
 
S

Salt_Peter

Group,

I'm trying to understand the problem with the following code. The
problem I get is associated with the line "int fmt = format(); " at
the bottom of the line. The error I get says that "format does not
take 0 arguments". It appears that the version of "format" that
returns an INT is NOT available to a derived class. Please notice that
there are 2 version of the format function.

Thats right, the version of format(...) found in Parent class is not
available. Thats by design.
The strategy in C++ to hide function names up an inheritance hierarchy
is further reinforced by your code.
Surely, format(...) shouldn't be employed for both setting and getting
some parameter. Thats just poor coding.
Try something like getformat() and setformat(int f) and always
initialize your members.
 
J

Jacek Dziedzic

Group,

I'm trying to understand the problem with the following code. The
problem I get is associated with the line "int fmt = format(); " at
the bottom of the line. The error I get says that "format does not
take 0 arguments". It appears that the version of "format" that
returns an INT is NOT available to a derived class. Please notice that
there are 2 version of the format function.

Is this correct behavior? I get the same error with both the g++ and
the MSVC compilers.

Any help would be much appreciated.

Parent::format()

HTH,
- J.
 
R

Richard Herring

Thats right, the version of format(...) found in Parent class is not
available. Thats by design.
The strategy in C++ to hide function names up an inheritance hierarchy
is further reinforced by your code.
Surely, format(...) shouldn't be employed for both setting and getting
some parameter. Thats just poor coding.

Not necessarily. Applied consistently and appropriately documented, it's
a reasonable naming convention for implementing property "get" and "set"
functions, since of necessity they always have different signatures.

But in any case it's not the issue here, which is that functions in
derived classes hide those of the same name in the parent class
regardless of signature.
Try something like getformat() and setformat(int f) and always
initialize your members.

If you really need to do this, just add

using Parent:;format;

in the child class.
 
R

Rolf Magnus

Richard said:
Not necessarily. Applied consistently and appropriately documented, it's
a reasonable naming convention for implementing property "get" and "set"
functions, since of necessity they always have different signatures.

It's even done in the standard library.

std::stringstream stream;
stream.str("Hello, world\n");
std::cout << stream.str();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top