473,408 Members | 2,027 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,408 software developers and data experts.

Help to explain this error?

The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;
return 0 ;
}

And the g++ give the error:
no matching call to : Test::Test()

I know this is because of my don't provide the Test::Test() function.

But when I change the array initialization as below:

Test array[10] = { Test(1) };
g++ say:
no matching call to : Test::Test(Test)

I can't understand where I call Test::Test(Test)?
Nov 29 '06 #1
7 1151
Bo Yang wrote:
The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;
return 0 ;
}

And the g++ give the error:
no matching call to : Test::Test()

I know this is because of my don't provide the Test::Test() function.

But when I change the array initialization as below:

Test array[10] = { Test(1) };
g++ say:
no matching call to : Test::Test(Test)

I can't understand where I call Test::Test(Test)?
You're creating a temporary Test(1) which is then used to construct the
first element in your array via the copy constructor. However, you have
provided a copy constructor from reference to a non-const object of
type Test, and since you can't bind temporaries to non-const references
and since the presence of that function means that the compiler won't
generate the implicit copy constructor from a reference to a const
object, you get the second error. Make the parameter in your copy
constructor const and you'll be back to your original error since
you're still trying to default-initialize the other nine elements of
the array.

BTW, you could eliminate some unnecessary code:

Test array[] = { 1, 2 }; // Invokes Test::Test(int) for each element

Cheers! --M

Nov 29 '06 #2
Bo Yang wrote:
The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;
return 0 ;
}

And the g++ give the error:
no matching call to : Test::Test()

I know this is because of my don't provide the Test::Test() function.

But when I change the array initialization as below:

Test array[10] = { Test(1) };
g++ say:
no matching call to : Test::Test(Test)

I can't understand where I call Test::Test(Test)?
You create a temporary Test object with 'Test(1)'. This object has to be
copied into the array. For that, the compiler doesn't find a suitable copy
constructor.

Nov 29 '06 #3
Bo Yang wrote:
The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;
return 0 ;
}

And the g++ give the error:
no matching call to : Test::Test()

I know this is because of my don't provide the Test::Test() function.

But when I change the array initialization as below:

Test array[10] = { Test(1) };
g++ say:
no matching call to : Test::Test(Test)

I can't understand where I call Test::Test(Test)?
Test array[10] = { Test(1) };
^^^^^^^
This is a temporary object. The first element of the array is copy-
constructed from it. Since your copy-constructor does not have 'const'
in the argument type, it cannot be used. In order to copy-construct
from a temporary, the copy constructor should be 'Test(Test const&)'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 29 '06 #4
Bo Yang <st******@mail.nankai.edu.cnwrites:
The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
^^^^^
Test( const Test & test ) { std::cout << "Copy" << std::endl; }
~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;
Test array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ;
return 0 ;
}

And the g++ give the error:
no matching call to : Test::Test()

I know this is because of my don't provide the Test::Test() function.
Yep.
But when I change the array initialization as below:

Test array[10] = { Test(1) };
g++ say:
no matching call to : Test::Test(Test)
You must provide intialization/construction for every object in the
array.

Cheers,
Rudiger
Nov 29 '06 #5
Rud1ger Sch1erz :
Bo Yang <st******@mail.nankai.edu.cnwrites:
>The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
^^^^^
Test( const Test & test ) { std::cout << "Copy" << std::endl; }
> ~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;

Test array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ;
> return 0 ;
}
I have change the copy constructor to the right form.
I think the output must to be:

Test Constructor!
Copy!
Destroy!

But, to my surprise, there is no Copy! output.
Why ?
>>
And the g++ give the error:
no matching call to : Test::Test()

I know this is because of my don't provide the Test::Test() function.

Yep.
>But when I change the array initialization as below:

Test array[10] = { Test(1) };
g++ say:
no matching call to : Test::Test(Test)

You must provide intialization/construction for every object in the
array.

Cheers,
Rudiger
Nov 29 '06 #6
Bo Yang wrote:
Rud1ger Sch1erz :
>Bo Yang <st******@mail.nankai.edu.cnwrites:
>>The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
^^^^^
Test( const Test & test ) { std::cout << "Copy" <<
std::endl; }
>>~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;

Test array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ;
>>return 0 ;
}

I have change the copy constructor to the right form.
I think the output must to be:

Test Constructor!
Copy!
Destroy!

But, to my surprise, there is no Copy! output.
Why ?
Because the compiler might - as an optimization - remove the actual copy
operation and directly initialize the array members. However, a suitable
copy constructor must, even if not used, still be available, because the
compiler's optimization behavior doesn't influence the correctness of your
code.

Nov 29 '06 #7

Rolf Magnus wrote:
Bo Yang wrote:
Rud1ger Sch1erz :
Bo Yang <st******@mail.nankai.edu.cnwrites:

The test code is as below:

#include <iostream>
class Test{
public:
Test(int i ) { std::cout << "Test Constructor!" << std::endl ; }
Test( Test & test ) { std::cout << "Copy" << std::endl; }
^^^^^
Test( const Test & test ) { std::cout << "Copy" <<
std::endl; }

~Test() { std::cout << "Destroy!" << std::endl ; };
};
int main( int argc , char ** argv ){
Test array[10] = { } ;

Test array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } ;

return 0 ;
}
I have change the copy constructor to the right form.
I think the output must to be:

Test Constructor!
Copy!
Destroy!

But, to my surprise, there is no Copy! output.
Why ?

Because the compiler might - as an optimization - remove the actual copy
operation and directly initialize the array members. However, a suitable
copy constructor must, even if not used, still be available, because the
compiler's optimization behavior doesn't influence the correctness of your
code.
this code:

class C{
public:
int i;
C(){puts("default");i=1;};
C(const C& cc){puts("copy");i=2*cc.i;};
};

int _tmain(int argc, _TCHAR* argv[])
{
C cc[5]={C()};
printf("%d\n",cc[3]);
getch();
return 0;
}

outputs:

default
default
default
default
default
1 1 1 1 1
this has nothing to do with optimization.
elements are not copied .ohterwise last line
of output should have been 2 2 2 2 2.

u actually tell the compiler how to initialize
array elements in a code similar to above.
that is u tell the compiler which constructor
with which arguments should be invoked to initialize
the array.

Nov 29 '06 #8

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

Similar topics

2
by: brazilnut | last post by:
Hi. Let me explain the setup. I am using Visual Studio .NET to develop a sort of add-in (COM class) for Excel called SQLAddin. It basically queries a SQL server and pulls in data. Now within my...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
14
by: Ina Schmitz | last post by:
Hello all, I don't succeed in displaying the explain plan. I use IBM DB2 Universal Database 8.2. I tried to do the example given in the online help for "Visual Explain". The tables...
9
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use...
6
by: Amelyan | last post by:
Can anyone explain *why* this happens (not how to work around it)? An error has occurred because a control with auto-generated id '_ctl8' could not be located to raise a postback event. To...
10
by: Jeff Boes | last post by:
I'm hoping there's someone here with experience in building the Visual Explain tool from Red Hat. I downloaded it and the J2 SDK, but when I attempt to follow the build instructions, I get messages...
4
by: marklawford | last post by:
Not having earned my DBA badge from the scouts just yet I'm a little lost with an error I'm getting. We've just upgraded our development database from 7.2 to 8.2 as the first step in upgrading...
2
by: turkey65 | last post by:
Could someone please explain and help me fix the following error? "ADO error: MSDTC on server 'myServer' is unavailable" It came up when I tried to run a query. Thank you!!
5
by: kabotnet | last post by:
Hi, I'm new in db2, I'm trying to execute EXPLAIN command on some queries but i have error like: And message similar to: Token EXPLAIN is not valid, valid tokens ( END GET SET CALL DROP FREE...
2
by: zakaria2710 | last post by:
Would u help me with this task, I am new in C++ programming, this codes have alot of errors, I have never work with graphs before, I am using Borlard C++ Ver 5.02. I was told u can not run a graph...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.