小编Dev*_*ley的帖子

朋友运营商<<重载问题,

我遇到了我的operator<<重载问题,无论我做什么都无法访问它所属类的私有变量,因为它会说变量作为编译器错误是私有的.这是我目前的代码:

#include "library.h"
#include "Book.h"

using namespace cs52;

Library::Library(){
   myNumberOfBooksSeenSoFar=0;
}
//skipping most of the functions here for space

Library operator << ( ostream &out, const Library & l ){
   int i=myNumberOfBooksSeenSoFar;
   while(i<=0)
   {
      cout<< "Book ";
      cout<<i;
      cout<< "in library is:";
      cout<< l.myBooks[i].getTitle();
      cout<< ", ";
      cout<< l.myBooks[i].getAuthor();
   }


   return (out);
}
Run Code Online (Sandbox Code Playgroud)

而函数原型和私有变量library.h都是

#ifndef LIBRARY_H
#define LIBRARY_H
#define BookNotFound 1
#include "Book.h"
#include <iostream>
#include <cstdlib>

using namespace std;

namespace cs52{

   class Library{
   public:
      Library(); …
Run Code Online (Sandbox Code Playgroud)

c++ private class operator-overloading friend

1
推荐指数
1
解决办法
281
查看次数

标签 统计

c++ ×1

class ×1

friend ×1

operator-overloading ×1

private ×1