-1

OK so here's my question, I'm trying to understand the use of friend in C++ with a working example on my PC for reference. I have everything set up in different classes, which are connected to one another with the .h files etc. (I think anyways). Can someone show me where I'm going wrong please? Cause i keep getting compiler errors and I don't understand what it means.

Yes I've researched the C:xxxx errors online, but I can't link the problem to the code I've got... It's obviously wrong! just need a push in the right direction / better understanding of C++... thank you!

//First class

#include <iostream>
#include <string>
using namespace std;

class Tutorial48
{
private:
    int var;
    int secret = 5;
public:
    Tutorial48(int v);
    friend class Tutorial48UseFriend;
    void PrintVar();
};

// First class .cpp
#include "Tutorial48.h"

Tutorial48::Tutorial48(int v)
{
   var = v;
}

void Tutorial48::PrintVar()
{
   cout << var << endl;
   cout << "Scret variable = " << secret << endl;
}

// Second class, whole point is to demo the friend keyword in C++!
#include "Tutorial48.h"
#include <iostream>
#include <string>
using namespace std;
class Tutorial48UseFriend
{
public:
   Tutorial48UseFriend();
   void showSecret(Tutorial48 tut48F)
   {
      // Just trying to increment by 1 so i know it's worked correctly. 
      tut48F.secret++;
      cout << "My new private variabe = " << tut48F.secret << endl;
   };
};

// Main function for outputting the whole thing. 
#include "Tutorial48.h"
#include "Tutorial48UseFriend.h"
#include <string>
#include <iostream>
int main
{
    Tutorial48UseFriend fr;
    Tutorial48 t(24);

    fr.showSecret(t);


    getchar();
    return 0;
}

Errors being produced....

Errors

Yeah so that's everything... As i said i'm new to this, so trying to understand it. Thank you to anyone for help in advance, cheers guys.

P.s. I do kind of understand the concepts of friend in c++ how it's used to access private variables of other classes etc. but i have no idea how to code it properly...

Danny
  • 1
  • 1
  • 4
    If some online tutorial you're using actually `#include` anything *within the brace scope of* `main()`, I would suggest finding alternative resources for learning. – WhozCraig Aug 15 '16 at 15:50
  • Yeah sorry that was my mistake, quickly put it on, it should be outside, i'll edit it, thanks for the spot. – Danny Aug 15 '16 at 15:53
  • 4
    Great, now add [**include-guards**](https://en.wikipedia.org/wiki/Include_guard) to both headers. Unrelated, [avoid `using namespace std;` in headers](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). – WhozCraig Aug 15 '16 at 15:58

1 Answers1

0

You should define the constructor of Tutorial48UseFriend like:

Tutorial48UseFriend(){}

and also pass Tutorial48 object by reference (by using '&'), and instead of making the function void make it return an int and then print it later, so:

int showSecret(Tutorial48& tut48F) { return ++tut48F.secret; }

Fedor
  • 139
  • 6