Why am I getting the compile error code 2248 for a friend function to a nested class?

w.t.s 0 Reputation points
2024-09-17T16:17:28.97+00:00

Hello,

I am developing a cross platform library and I am running into a compile error which I only get in Windows and not Linux. I have declared the operator<<() as a friend inside of a class which has a nested struct within it. However, the compiler is throwing an error stating that the object is private to the class(which is true), disregarding what seems to me to be the required allowance to grant access to the nested struct.

This is the basics of the code. Hopefully you can see what is causing the error, as I am not able to. I have reasons to nest the struct within the class which I would prefer not to work around.

In the header file:

namespace sa {
	class Format {
		struct Bound_form {
			const Format& format;
			std::string string;
		};
		friend std::ostream& operator<< ( 
					std::ostream&, const Format::Bound_form&);
		// other private members
	public:
		// public members
	};
	std::ostream& operator<< (
			std::ostream& out, const Format::Bound_form& formed_string);
}

In the .cpp source file:

 //This is where the error throws
std::ostream& sa::operator<< (
    std::ostream& out, const sa::Format::Bound_form& formed_string)
{
	// print out the formed string using the Bound_form's Format
}

I know that this setup can be used to expose private members (I have done this myself) as well as private classes (as described here in Microsoft documentation). I'm just not quite sure what I'm missing here.

Any help will be much appreciated.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,059 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,957 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,478 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 11,506 Reputation points Microsoft Vendor
    2024-09-18T02:25:09.8533333+00:00

    Hi, @w.t.s

    I am able to reproduce the problem.
    If not split the definition into .cpp file, the error will not occur.
    g++ compiles well.

    Please report the error to Developer Community and post report link here so that we can follow up.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.