1

Just now I'm started to writing in OOP C++. I want to include arithmetical operations in my class which represent 2D vector in physics.

Ok. End offtop I have a problem with access to private member form friend function. I write friend declaration in class block, but I still haven't got access to vector's private members and i don't know why.

Did I do not understand this?

This is a code:

vector2d.h:

    /* NAMESPACE */
 #define _NEDLIB_BEGIN namespace nedlib {
 #define _NEDLIB_END }

_NEDLIB_BEGIN
    #define COORD double // set type of coordinates

class vector2d
{
private:
    COORD x, y;

public:
        /* CONSTRUCTORS */
    // [...] - if it's important, i will show full class code

        /* DESTRUCTORS */
    ~vector2d();

        /* MEMBER FUNCTIONS*/
    // [...] - if it's important, i will show full class code


        /* Friend functions */
    friend vector2d operator *(const double & real, const vector2d & vector); // problem
    friend ostream & operator <<(ostream & screen, const vector2d & vector); // problem

}; /* class vector2d */


// ********************************************************************************
    /* operators */

    // vector2d operator *(const double & real, const vector2d & vector);
    // ostream & operator <<(ostream & screen, const vector2d & vector);

double RadToDeg(double);
double DegToRad(double);

_NEDLIB_END

vector2d.cpp

 using namespace nedlib;

    vector2d operator *(const double & real, const vector2d & vector)
    {
        return vector2d(vector.x * real, vector.y * real); // problem
    }

    ostream & operator <<(ostream & screen, const vector2d & vector)
    {
        screen << "(" << vector.x << ", " << vector.y << ")"; // problem
        return screen;
    }

double RadToDeg(double rad)
{
    return (180.0 * rad / M_PI);
}

double DegToRad(double deg)
{
    return (deg * M_PI / 180.0);
}

Visual error: (four errors, but all almost the same)

Severity Code Description Project File Line Error (active) member "nedlib::vector2d::x" (declared at line 21 of "c:\Users\Nedziu\Documents\Visual Studio 2015\Projects\Ned Library\Ned Library\vector2d.h") is inaccessible Ned Library c:\Users\Nedziu\Documents\Visual Studio 2015\Projects\Ned Library\Ned Library\vector2d.cpp 208

Nedveed
  • 23
  • 6
  • 1
    Did you try to prefix your namespace in the definition: `vector2d nedlib::operator *(const double & real, const vector2d & vector)`? – πάντα ῥεῖ Aug 08 '15 at 10:56
  • 2
    Unrelated to your problem, but [don't use symbols with leading underscore followed by an upper-case letter](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – Some programmer dude Aug 08 '15 at 10:57
  • More related to your problem, what happens if you comment out the declarations outside the class? Because first you declare the operator functions as friends, and then as *not* friends, and maybe the compiler gets confused by this? (I don't see why it should, but it's still worth testing.) – Some programmer dude Aug 08 '15 at 11:01
  • Dayum, that's some disgusting code. – Puppy Aug 08 '15 at 11:10
  • @πάνταῥεῖ Okey, it works! But why "using namespace nedlib" doesn't work here?? – Nedveed Aug 08 '15 at 11:20
  • @Joachim Thanks for link, it's interesting and it can be helpful When i comment declarations outside the class - it works. So, i shouldn't write declaration when i write declaration of "friend" in class?? – Nedveed Aug 08 '15 at 11:20
  • @Puppy It's really that bad :/ Have you got any source of inforiation how to write clear code?? – Nedveed Aug 08 '15 at 11:20

1 Answers1

3

You have declared your operator functions inside the nedlib namespace

    vector2d operator *(const double & real, const vector2d & vector);
    ostream & operator <<(ostream & screen, const vector2d & vector);

_NEDLIB_END // <<<<<<

Thus you have to qualify the namespace in the function definition:

vector2d nedlib::operator *(const double & real, const vector2d & vector);
      // ^^^^^^^^

using namespace nedlib;

doesn't affect the scope of definitions seen within the translation units where it is used.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Okey. When i write nedlib:: prefix, then all works good. But why "using namespace nedlib" doesn't work here? – Nedveed Aug 08 '15 at 11:17
  • Okey. Look my question post again. I'm added 'RadToDeg ' and 'DegToRad' functions outside the class. These functions works without nedlib:: prefix although im declared it inside the 'nedlib' namespace. Why they don't need prefix? – Nedveed Aug 08 '15 at 11:37
  • 1
    @Nedveed, it looks like `RadToDeg` and `DegToRad` are working. But they're not. You can multiple declarations in different namespaces. You have declared (but not defined) these in the `nedlib` namespace. If you try to call them, e.g. with `nedlib::RadToDeg`, you will get a linker error. The functions you have defined are in the global namespace `::` instead of `::nedlib::` – Aaron McDaid Aug 08 '15 at 11:42
  • 1
    @AaronMcDaid You are right. I got a linker error. It's simple, but for me very instructive. For now all works good. I will test my code more. Have you got source of information how i can learn how to code less ugly ? – Nedveed Aug 08 '15 at 11:50