S
saxman
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<int> test()
{
B<int> returnVal;
return returnVal;
}
int main()
{
B<int> b;
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<int> b;
B<int> b2 = b;
return 0;
</code>
or
<code>
B<int> b;
B<int> b2;
b2 = b;
return 0;
</code>
or
<code>
B<int> b;
B<int> b2(b);
return 0;
</code>
Any help would be greatly appreciated.
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<int> test()
{
B<int> returnVal;
return returnVal;
}
int main()
{
B<int> b;
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<int> b;
B<int> b2 = b;
return 0;
</code>
or
<code>
B<int> b;
B<int> b2;
b2 = b;
return 0;
</code>
or
<code>
B<int> b;
B<int> b2(b);
return 0;
</code>
Any help would be greatly appreciated.