473,385 Members | 1,782 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,385 software developers and data experts.

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<int>)'
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 1861
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.sewrote:
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
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...
3
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...
10
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, ...
13
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...
9
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
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...
2
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...
16
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...
5
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.