关于友元函数,不能访问类私有的有关问题,求解答
关于友元函数,不能访问类私有的问题,求解答啊
#include"stdafx.h"
#ifndef SET_H
#define SET_H
#include<iostream>
#include<algorithm>
#include<stddef.h>
template<class T>
class Set
{
public:
Set();
bool contains(const T&) const;
void insert(const T&);
void remove(const T&);
void print(std::ostream&) const;
friend std::ostream& operator<<(std::ostream os, const Set<T>&s );
private:
enum {LIMIT = 64};
T elems[LIMIT];
size_t nelems;
};
template<class T>
Set<T>::Set()
{
nelems = 0;
}
template<class T>
bool Set<T>::contains(const T& x) const
{
const T* eof = elems + nelems;
return std::find(elems,eof,x) != eof;
}
template<class T>
void Set<T>::insert(const T& x)
{
if (nelems < LIMIT && !contains(x))
elems[nelems++] = x;
}
template<class T>
void Set<T>::remove(const T& x)
{
T* eof = elems + nelems;
if (std::remove(elems,eof,x) != eof)
--nelems;
}
template<class T>
void Set<T>::print(std::ostream& os) const
{
os << '{';
for (int i = 0; i< nelems; ++i)
{
if(i > 0)
os << ',';
os << elems[i];
}
os << '}';
}
template<class T>
std::ostream& operator<<(std::ostream os, const Set<T>& s)
{
s.print(os);
return os;
}
#endif
以下是错误提示:
>d:\Visual Studio2010\VC\include\ostream(604): error C2248: “std::basic_ios<_Elem,_Traits>::basic_ios”: 无法访问 private 成员(在“std::basic_ios<_Elem,_Traits>”类中声明)
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\Visual Studio2010\VC\include\ios(176) : 参见“std::basic_ios<_Elem,_Traits>::basic_ios”的声明
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> 此诊断出现在编译器生成的函数“std::basic_ostream<_Elem,_Traits>::basic_ostream(const std::basic_ostream<_Elem,_Traits> &)”中
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>
1>生成失败。
搞了半天没搞定,很是纠结,求帮助,万分感谢
------解决思路----------------------
std::ostream os
使用引用!
因为流不可以进行复制和复制,因为拷贝构造函数和赋值操作符是private的
------解决思路----------------------
-->
#include"stdafx.h"
#ifndef SET_H
#define SET_H
#include<iostream>
#include<algorithm>
#include<stddef.h>
template<class T>
class Set
{
public:
Set();
bool contains(const T&) const;
void insert(const T&);
void remove(const T&);
void print(std::ostream&) const;
friend std::ostream& operator<<(std::ostream os, const Set<T>&s );
private:
enum {LIMIT = 64};
T elems[LIMIT];
size_t nelems;
};
template<class T>
Set<T>::Set()
{
nelems = 0;
}
template<class T>
bool Set<T>::contains(const T& x) const
{
const T* eof = elems + nelems;
return std::find(elems,eof,x) != eof;
}
template<class T>
void Set<T>::insert(const T& x)
{
if (nelems < LIMIT && !contains(x))
elems[nelems++] = x;
}
template<class T>
void Set<T>::remove(const T& x)
{
T* eof = elems + nelems;
if (std::remove(elems,eof,x) != eof)
--nelems;
}
template<class T>
void Set<T>::print(std::ostream& os) const
{
os << '{';
for (int i = 0; i< nelems; ++i)
{
if(i > 0)
os << ',';
os << elems[i];
}
os << '}';
}
template<class T>
std::ostream& operator<<(std::ostream os, const Set<T>& s)
{
s.print(os);
return os;
}
#endif
以下是错误提示:
>d:\Visual Studio2010\VC\include\ostream(604): error C2248: “std::basic_ios<_Elem,_Traits>::basic_ios”: 无法访问 private 成员(在“std::basic_ios<_Elem,_Traits>”类中声明)
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\Visual Studio2010\VC\include\ios(176) : 参见“std::basic_ios<_Elem,_Traits>::basic_ios”的声明
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> 此诊断出现在编译器生成的函数“std::basic_ostream<_Elem,_Traits>::basic_ostream(const std::basic_ostream<_Elem,_Traits> &)”中
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>
1>生成失败。
搞了半天没搞定,很是纠结,求帮助,万分感谢
------解决思路----------------------
std::ostream os
使用引用!
因为流不可以进行复制和复制,因为拷贝构造函数和赋值操作符是private的
------解决思路----------------------
friend std::ostream& operator<<(std::ostream os, const Set<T>&s );
template<class T>
std::ostream& operator<<(std::ostream os, const Set<T>& s)
{
s.print(os);
return os;
}
-->
friend std::ostream& operator<<(std::ostream &os, const Set<T>&s );
template<class T>
std::ostream& operator<<(std::ostream &os, const Set<T>& s)
{
s.print(os);
return os;
}