Friend Function

Hi. I am new to C++, coming from C# and Vb.NET. I have been trying to discover through book explanation and other online references exactly how to implement this friend functionality. I was able to declare and compile but now cannot access the friend function. Here is what I have.

TestClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>

namespace Test
{
	class TestClass
	{
	private:
		std::string hiddenVar;
	public:
		TestClass();
		~TestClass();
		friend std::string getHiddenVar(const TestClass& Inst);
	};
}


TestClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
        TestClass::TestClass()
	{
		this->hiddenVar = "Hidden";
	}
	
	TestClass::~TestClass()
	{
	}

        std::string getHiddenVar(const TestClass& Inst)
	{
		return Inst.hiddenVar;
	}


Entry Point:
1
2
3
4
5
6
7
8
#include "stdafx.h"
#include "TestClass.h"

int _tmain(int argc, _TCHAR* argv[])
{
  Test::TestClass t{};
  //No Access to t.getHiddenVar
}
You need to specify the namespace in front of your ctor, dtor and functions.
Also getHiddenvar is not a member function so you can't call it from an object.
How did you manage to compile ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace Test
{
  class TestClass
  {
  private:
    std::string hiddenVar;
  public:
    TestClass();
    ~TestClass();
    friend std::string getHiddenVar(const TestClass& Inst);
  };
}

Test::TestClass::TestClass()
{
  this->hiddenVar = "Hidden";
}

Test::TestClass::~TestClass()
{
}

std::string Test::getHiddenVar(const Test::TestClass& Inst)
{
  return Inst.hiddenVar;
}

int main()
{
  Test::TestClass t;
  cout << Test::getHiddenVar(t);
}
The following code works as expected, so if I had to guess, it's a namespace issue somewhere in there.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <iostream>


class TestClass
{
private:
  std::string hiddenVar;
public:
  TestClass();
  friend std::string getHiddenVar(const TestClass& Inst);
};


TestClass::TestClass()
{
  hiddenVar = "Hidden";
}
	
std::string getHiddenVar(const TestClass& Inst)
{
  return Inst.hiddenVar;
}

int main()
{
  TestClass t{};
  std::cout <<  getHiddenVar(t);
}

Last edited on
@Thomas1965 and @Repeater , I get that it works when the class and the implementations are all done inside the same file but I am trying to put my friend function inside TestClass.h. I get now that you don't implement it inside TestClass.Cpp but inside the file you wish to use it but when I remove the getHiddenVar from Testclass.Cpp and add it to my file containing my main() like so:

1
2
3
4
string getHiddenVar(const Test::TestClass& Inst)
{
	return Inst.hiddenVar;
}


I am getting the following error: member Test:TestClass::hiddenVar is inaccessible.
It appears I have found the solution. Apparently inside the implementation file you need to append the namespace to the function like so:

1
2
3
4
string Test::getHiddenVar(const Test::TestClass& Inst)
{
	return Inst.hiddenVar;
}


Now I no longer get an inaccessible errors and I am able to call it. Thank you both so much for you help!
You need to declare the function outside the class in TestClass.h, in addition to being declared as a friend inside the class, otherwise you won't be able to call it from outside the class.

TestClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace Test
{
	class TestClass
	{
	private:
		std::string hiddenVar;
	public:
		TestClass();
		~TestClass();
		friend std::string getHiddenVar(const TestClass& Inst);
	};
	
	std::string getHiddenVar(const TestClass& Inst);
}


You call the function by passing the object as an argument.
 
Test::getHiddenVar(t); 

Note that the namespace name is optional thanks to ADL (http://en.cppreference.com/w/cpp/language/adl).
 
getHiddenVar(t); // This will also work. 
Last edited on
Topic archived. No new replies allowed.