473,808 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Copy Constructor and overloaded assignment operator with templates

Hi everyone,

I'm writing a class that inherits from std::vector in order to add
some additional functionality. I'm having a compiler problem, however,
when I have a function that returns this class. I've drilled down and
been able to create a very simple example of the problem. Here's my
code:

<code>
#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

template <typename T>
class B : public std::vector<T>
{
public:
B(){ cout << "In B ctor" << endl; }

B(B<T&b) { cout << "In B copy ctor" << endl; }

B<T>& operator=(const B<T&b) { cout << "In B assign." << endl;
return *this; }

~B(){ cout << "In B destructor" << endl; }

};

B<inttest()
{
B<intreturnVal ;
return returnVal;
}

int main()
{
B<intb;
b = test();
return 0;
}
</code>

I get the following error when compiling:
test.cpp: In function `int main()':
test.cpp:31: error: no matching function for call to
`B<int>::B(B<in t>)'
test.cpp:14: note: candidates are: B<T>::B(B<T>&) [with T = int]

Does anyone know of a solution?

Some additional information: any of the following code in 'main'
compiles fine:
<code>
B<intb;
B<intb2 = b;
return 0;
</code>

or

<code>
B<intb;
B<intb2;
b2 = b;
return 0;
</code>

or

<code>
B<intb;
B<intb2(b);
return 0;
</code>

Any help would be greatly appreciated.

Jun 12 '07 #1
2 1906
On 12 Juni, 16:08, saxman <erll...@gmail. comwrote:
Hi everyone,

I'm writing a class that inherits from std::vector in order to add
some additional functionality. I'm having a compiler problem, however,
when I have a function that returns this class. I've drilled down and
been able to create a very simple example of the problem. Here's my
code:

<code>
#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

template <typename T>
class B : public std::vector<T>
{
public:
B(){ cout << "In B ctor" << endl; }

B(B<T&b) { cout << "In B copy ctor" << endl; }

B<T>& operator=(const B<T&b) { cout << "In B assign." << endl;
return *this; }

~B(){ cout << "In B destructor" << endl; }

};

B<inttest()
{
B<intreturnVal ;
return returnVal;

}

int main()
{
B<intb;
b = test();
The problem is that the compiler converts this to a copy-constructor
call like this 'B<int>(test()) ', however since your copy-constructor
takes a reference as a parameter this can not compile (since a
reference can not bind to the temporary returned by test()). To solve
this simply change the copy-constructor to 'B(const B<T&b)'. In
general the parameters to copy-constructors should be const.

--
Erik Wikström

Jun 12 '07 #2
On Jun 12, 10:16 am, Erik Wikström <eri...@student .chalmers.sewro te:
On 12 Juni, 16:08, saxman <erll...@gmail. comwrote:
Hi everyone,
I'm writing a class that inherits from std::vector in order to add
some additional functionality. I'm having a compiler problem, however,
when I have a function that returns this class. I've drilled down and
been able to create a very simple example of the problem. Here's my
code:
<code>
#include <vector>
#include <iostream>
using std::vector;
using std::cout;
using std::endl;
template <typename T>
class B : public std::vector<T>
{
public:
B(){ cout << "In B ctor" << endl; }
B(B<T&b) { cout << "In B copy ctor" << endl; }
B<T>& operator=(const B<T&b) { cout << "In B assign." << endl;
return *this; }
~B(){ cout << "In B destructor" << endl; }
};
B<inttest()
{
B<intreturnVal ;
return returnVal;
}
int main()
{
B<intb;
b = test();

The problem is that the compiler converts this to a copy-constructor
call like this 'B<int>(test()) ', however since your copy-constructor
takes a reference as a parameter this can not compile (since a
reference can not bind to the temporary returned by test()). To solve
this simply change the copy-constructor to 'B(const B<T&b)'. In
general the parameters to copy-constructors should be const.

--
Erik Wikström
Thanks for the help and the explanation, that worked perfectly

Jun 12 '07 #3

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

Similar topics

4
1827
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a memory problem that I'm not readily seeing that lies within the overloaded operator - I think pertaining to the temporary class object that is created and used to return a value of the operands and the pointer variable of the class. Could someone...
3
3047
by: ganesh.tambat | last post by:
Hi, Please see below a piece of code. Here I am trying to create a linked list by attaching two linked list together. I have overloaded operator + for this. Now the output always says that the list is empty. I added copy constructor and overloaded assignment operator but still the behavior remain same. Can anyone guide me on this ? Thanks Ganesh
10
2585
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects, this bypasses the problem or it seems to me like that. Could any one give me some simple examples...
13
5027
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts to make it more manageable. Below is a draft of a document that I plan to give to my introductory C++ class. Please note that I have purposely left out any mention of safety issues in the ctors which could be resolved thru some combination...
9
1681
by: blangela | last post by:
2.0 Sample Code class ABC // dummy class used below {}; class Example2 { public: Example2(); // default ctor Example2( const Example2 &); // copy ctor
1
2129
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a reference member, the compiler will not be able to synthesize an assignment operator for your class. It actually helps to think of a reference member as a const member (since it cannot be made to reference any other object once it has been initialized). ...
2
6254
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for manipulation. Now A has a copy constructor and assignment operator. What will happen if copying goes on in B? Do I need to have an overloaded set of functions which call the same copy functions that A has available? Or are they virtual in the...
16
4355
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include <string>
5
2285
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the overloaded operator function performs the necessary copy of obj's member in to obj1. Can't the same be done using copy
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.