【发布时间】:2013-11-21 07:47:58
【问题描述】:
在执行此程序时,我收到以下编译错误:
template.cpp: In function ‘std::istream& operator>>(std::istream&, currency&)’:
template.cpp:8: error: ‘int currency::doller’ is private
template.cpp:25: error: within this context
template.cpp:9: error: ‘int currency::cents’ is private
template.cpp:25: error: within this context
这是c++程序:
#include <iostream>
using namespace std;
class currency
{
private:
int doller;
int cents;
public:
currency():doller(0),cents(0) {}
friend ostream& operator<< (ostream& stream, const currency& c );
friend istream& operator>> (istream& in, const currency& c);
/*friend istream& operator>> (istream& in, const currency& c)
{
in >> c.doller >> c.cents;
return in;
} */
};
istream& operator>> (istream& in, currency& c)
{
in >> c.doller >> c.cents;
return in;
}
ostream& operator<< (ostream& stream, const currency& c )
{
stream << "(" << c.doller << ", " << c.cents << ")";
return stream;
}
template <class T>
void function(T data)
{
cout << "i am in generalized template function: " << data << endl;
}
template<>
void function (int data)
{
cout << "This is: specialized for int" << data << endl;
}
int main()
{
currency c;
cin >> c;
function (c);
function (3.14);
function ('a');
function (12);
return 0;
}
同时 std::ostream& operator
另外,当我在类中给出定义时,这是错误:
template.cpp: In function ‘std::istream& operator>>(std::istream&, const currency&)’:
template.cpp:18: error: ambiguous overload for ‘operator>>’ in ‘in >> c->currency::doller’
【问题讨论】: