Re: Problem with temporary object instantiation

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Chris Yoedhana wrote:
> Hi,
>
> I cannot figure out why this code fails to compile. I use 2 files, main.h
> and main.cpp to separate class declaration and definition.
>
> in main.h
>
> #define MAIN_H
>
> #include <iostream>
>
> class TestClass {
> public:
> TestClass(int val);
> TestClass(float, int);
> };
>
> class WrapperClass {
> public:
> WrapperClass(const TestClass& testa);
> int j;
> };
> #endif
>
> in main.cpp
>
> #include "main.h"
>
> TestClass::TestClass(float a, int b)
> {
> std::cout<<"Invoking test class ctor with 2 param\n";
> }
>
> TestClass::TestClass(int val)
> {
> std::cout<<"TestClass val = "<<val<<std::endl;
> }
>
> WrapperClass::WrapperClass(const TestClass &aTest):j(0)
> {
> std::cout<<"Calling wrapper class constructor\n";
> }
>
> int main()
> {
> int i;
> WrapperClass wrapper0(TestClass(i));
> std::cout<<"wrapper0.j = "<<wrapper0.j<<std::endl;
> return 0;
> }
>
> The error message is:
> main.cpp(22) : error C2228: left of '.j' must have class/struct/union type
> type is 'overloaded-function'

Visual C++ is correctly flagging an error here, because the statement
"WrapperClass wrapper0(TestClass(i));" declares a function named
"wrapper0" which takes a parameter of type "TestClass" named "i" and
returns to its caller an object of type "WrapperClass."

You can convince yourself of that by adding the following code in your
main():

WrapperClass wrapper1(TestClass(i));
TestClass ti(444);
WrapperClass wc = wrapper1(ti);

If you compile, you will see that the new code does not produce an
error, unless you try to link, since no implementation is provided for
wrapper1.

It's a bit obscure, granted. It happens because your code is ambiguous,
forcing the compiler to interpet the statement as a declaration, as
required in section 8.2 of the standard.


> I can make the code compile by doing the following :
> 1. Replace i with an integer constant
> WrapperClass wrapper0(TestClass(i)) --> WrapperClass
> wrapper0(TestClass(100))

Yes, because that statement is not ambiguous (and thus doesn't have to
be resolved in favor of a declaration) because 'i' can be the name of a
parameter, but 100 cannot since names of parameters may not start with a
digit.

I hope this helps,

Nik B.
.



Relevant Pages

  • Re: Defining variable in C header file related doubt
    ... And it didn't compile, obviously. ... with int g declared in both files (and apparently ... "An external def1nition is an external declaration that is also a ... If an identif1er declared with external linkage is used in an ...
    (comp.lang.c)
  • Re: Implicit int
    ... compile or implement, or even would have been in the 1970s. ... note that if you can't declare an array of size "n", ... could not already do with long int, had they chosen so to do. ... I recognise that this is not a popular view, but if the declaration is ...
    (comp.std.c)
  • Re: extern vars and linking
    ... > LOCAL int secondary_func{ ... > Compile and link libA.cpp as part of a library libA.a ... Including a declaration in any number of translation units is not going ... > C and CPP program any help or pointers would be much apprecaited. ...
    (comp.lang.cpp)
  • Re: building linklist in java
    ... Unfortunately, it doesn't compile. ... foo.c:1: parse error before `UTGLLElement' ... foo.c:63: warning: type defaults to `int' in declaration of `current' ...
    (comp.lang.c)
  • Re: How does the compiler do with this code?
    ... an int. ... That's why you should always compile asking for errors and warnings: ... p.c:3: warning: implicit declaration of function `printf' ...
    (comp.os.linux.development.apps)