只有在有影印机时,方可使用以下代码。
当我添加印刷说明(通过<代码>std:cout)并且提供复印件时,不使用(我假定,有这么多的编辑来删除不必要的复印件)。
但在产出<代码>operator <<和功能plop(<>plop(><>>>/code>(在我制造临时物体时)中,我看不出需要复印件。 有些人可以解释,为什么在我通过提及一切时,语言需要它(或者我做错的话)。
#include <iostream>
class N
{
public:
N(int) {}
private:
N(N const&);
};
std::ostream& operator<<(std::ostream& str,N const& data)
{
return str << "N
";
}
void plop(std::ostream& str,N const& data)
{
str << "N
";
}
int main()
{
std::cout << N(1); // Needs copy constructor (line 25)
plop(std::cout,N(1)); // Needs copy constructor
N a(5);
std::cout << a;
plop(std::cout,a);
}
Compiler:
[Alpha:~/X] myork% g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5646~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5646)[Alpha:~/X] myork% g++ t.cpp
t.cpp: In function ‘int main()’:
t.cpp:10: error: ‘N::N(const N&)’ is private
t.cpp:25: error: within this context
t.cpp:10: error: ‘N::N(const N&)’ is private
t.cpp:26: error: within this context
This is a simplified version of some real code.
In the real code I have a class that contains a std::auto_ptr. This means that a copy constructor that takes a const reference is not valid (without some work) and I was getting an error indicating that the copy constructor was not available because of it:
改变这一类别:
class N
{
public:
N(int) {}
private:
std::auto_ptr<int> data;
};
然后是:
t.cpp:25:差错:没有“N:N(N)”的配对功能