0

I can't catch this error, it must be something very easy. I have a header file (snim.h):

#ifndef SNIM_CLASS_HH_
#define SNIM_CLASS_HH_

#include <iostream>

namespace snim {

class SnimModel {

    int communitySize;           // Total size of the community


public:
  SnimModel(int c) : communitySize(c) {};

  friend std::ostream& operator<<(std::ostream&,  const SnimModel&);
};


} /* end namespace */

#endif

and an implementation file:

#include "snim.h"

using namespace snim; 


std::ostream& operator<<(std::ostream& os, const SnimModel& s) {

  os << "[Total Size]\n[";
  os << s.communitySize << "]\n";

  return os;
};

Thus when I tried to compile it gives

 In function ‘std::ostream& operator<<(std::ostream&, const snim::SnimModel&)’:

 snim.cpp:9:11: error: ‘int snim::SnimModel::communitySize’ is private within this context
  os << s.communitySize << "]\n";
Leosar
  • 2,010
  • 4
  • 21
  • 32

1 Answers1

5

You define an other operator in global namespace, it should be in namespace snim

std::ostream& snim::operator<<(std::ostream& os, const SnimModel& s)
{
    // ...
}

or

namespace snim
{
    std::ostream& operator<<(std::ostream& os, const SnimModel& s)
    {
        // ...
    }
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • To add to this reply, friend definitions inside a class that is inside a namespace refers to a (function/operator) definition that is inside the namespace, not in global namespace. In this case `snim` namespace – Raxvan Feb 09 '17 at 18:06
  • I don't think you can define a function as part of a namespace simply by prefixing it, can you? Don't you actually have to surround it in a namespace block? – Benjamin Lindley Feb 09 '17 at 18:08
  • @BenjaminLindley: The 2 methods are possible. I add a link demo for the one in which you have a doubt. – Jarod42 Feb 09 '17 at 18:11
  • If this is allowed, then I guess it's a GCC bug to report an error with the `-pedantic-errors` flag. – Benjamin Lindley Feb 09 '17 at 18:19
  • @BenjaminLindley: clang, msvc and gcc accept it. gcc only produces warning as it doesn't find a declaration of the operator in the given namespace (as the function declaration comes from `friend`) and providing the declaration remove the warning. – Jarod42 Feb 09 '17 at 18:25
  • Well it produces a warning with `-pedantic`, but an error with `-pedantic-errors`. `-pedantic-errors` is supposed to generate errors on non-standard code. So if this is allowed by the standard, then that's a bug. – Benjamin Lindley Feb 09 '17 at 18:31
  • @BenjaminLindley: [Demo](http://coliru.stacked-crooked.com/a/8d2fd1d3542513b8) of possible warning, which is in fact related to friend declaration. – Jarod42 Feb 09 '17 at 18:38
  • @Jarod42: Yes, I understand this. I understand that GCC requires this. I am saying that it shouldn't require this. Or are you saying that a non-friend declaration is required by the standard in order for this to work (in which case clang is not reporting an error)? – Benjamin Lindley Feb 09 '17 at 18:41
  • @BenjaminLindley: I think clang is correct. Better to ask a separated question for that. – Jarod42 Feb 09 '17 at 18:46