错误:“ int Combination :: n”在此上下文中是私有的

我有这个文件.hpp:

#include <vector>
#include <iostream>

#ifndef MASTERMIND.HPP
#define MASTERMIND.HPP

class Combination{
    private:
        int n;
        std::vector<int> values;
    public:
        Combination();
        Combination(const std::vector<int>& v){n=v.size(); values=v;}
        int size() const{ return n;}
        int operator[](int i) const{return values[i];}
        friend std::ostream& operator<<(std::ostream&, const Combination&);
        friend std::ostream& operator>>(std::ostream&, const Combination&);
};

#endif

而这个.cpp:

#include "mastermind.hpp"
#include <iostream>

std::ostream& operator<<(std::ostream& o, const Combination& c){
    for(int i=0; i<c.n-1; i++){o << c.values[i] << " ";}
    o << c.values[c.n-1];
    return o;
}

我有这个错误:

mastermind.cpp:5:19:错误:“ int Combination :: n”在   这种情况

对于该行:

for(int i=0; i<c.n-1; i++){o << c.values[i] << " ";}

我把<<当作朋友,我不应该可以使用班上所有私人课程吗?

评论