Error A Class-key Must Be Used When Declaring A Friend
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to resolve “class must be used when declaring a friend” error? up vote 4 down vote favorite class two; class one { int a; public: one() { a = 8; } friend two; }; class two { public: two() { } two(one i) { cout << i.a; } }; int main() { one o; two t(o); getch(); } I'm getting this error from Dev-C++: a class-key must be used when declaring a friend But it runs fine when compiled with Microsoft Visual C++ compiler. c++ visual-c++ dev-c++ friend-function share|improve this question edited Aug 28 '11 at 11:38 Mat 135k21234273 asked Aug 28 '11 at 11:30 shubhendu mahajan 566516 2 Er, please can you fix the formatting! I tried, but it was too hard. –David Heffernan Aug 28 '11 at 11:32 1 Please could you fix your whitespace. –Oliver Charlesworth Aug 28 '11 at 11:33 One note, don't use Dev-C++, it's outdated. –Griwes Aug 28 '11 at 11:41 add a comment| 2 Answers 2 active oldest votes up vote 12 down vote accepted You need friend class two; instead of friend two; Also, you don't need to forward-declare your class separately, because a friend-declaration is itself a declaration. You could even do this: //no forward-declaration of two class one { friend class two; two* mem; }; class two{}; share|improve this answer answered Aug 28 '11 at 11:36 Armen Tsirunyan 76.4k32215343 1 thanxx for the help but y i was'nt getting error with visual c++ compiler –shubhendu mahajan Aug 28 '11 at 11:45 3 @desprado07: Well, because many compilers are not exactly strict with this rule (that the word class or struct be present in the friend declaration). It is however mandated by the standard as per 11.4. The accepted answer to another question may help you. –Armen Tsirunyan Aug 28 '11 at 11:47 5 It's al
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User Documentation Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [C++] Class-key error Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to 4 of http://stackoverflow.com/questions/7220661/how-to-resolve-class-must-be-used-when-declaring-a-friend-error 4 Thread: [C++] Class-key error Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode July 21st, 2009 #1 wbest View Profile View Forum Posts Private Message A Carafe of Ubuntu Join Date Jun 2009 Beans 91 [C++] Class-key error Riddle me this, my brethren! g++ gives me the error error: a class-key must be used when declaring a friend So I go to the https://ubuntuforums.org/showthread.php?t=1219353 line it refers to Code: class __XAuto; typedef XSingleton
Programming Boards C++ Programming When declaring freind classes, what happens without the class keywork? Getting started with C or http://cboard.cprogramming.com/cplusplus-programming/80832-when-declaring-freind-classes-what-happens-without-class-keywork.html C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: When declaring freind classes, what happens without the class http://en.cppreference.com/w/cpp/language/class keywork? Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 07-10-2006 #1 William.Xu View Profile View error a Forum Posts Registered User Join Date Jul 2006 Posts 3 When declaring freind classes, what happens without the class keywork? After reading the section on friend functions and claases, I know that when u declare a firend class, u must cite explicitely: Code: class A ; // forward declaration class B { friend class A ; public: ... ... } error a class-key ; But would anyone please tell me the difference between the flollowing lines of code while defining class B? Thanks a lot! Code: class B { friend class A ; public: ... ... } ; class B { friend A ; public: ... ... } ; 07-10-2006 #2 laserlight View Profile View Forum Posts Visit Homepage C++ Witch Join Date Oct 2003 Location Singapore Posts 25,447 I am surprised that you managed to leave out the class keyword and still get it to compile. It looks illegal to me. Originally Posted by Bjarne Stroustrup (2000-10-14) I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool. Look up a C++ Reference and learn How To Ask Questions The Smart Way 07-10-200
Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications [edit] C++ language Classes General overview class/struct types union types Members data members static members the this pointer nested classes member templates bit fields using-declarations member functions member access specifiers constructors and member initializer lists default member initializer(C++11) friend specifier explicit specifier converting constructor Special member functions default constructor copy constructor move constructor(C++11) copy assignment operator move assignment operator(C++11) destructor Inheritance base and derived classes virtual member functions override(C++11) final(C++11) pure virtual functions and abstract classes [edit] Classes and structs are user-defined types, defined by class-specifier, which appears in decl-specifier-seq of the declaration syntax. The class specifier has the following syntax: class-key attr class-head-name base-clause { member-specification } class-key - one of class or struct. The keywords are identical except for the default member access and the default base class access. attr(C++11) - optional sequence of any number of attributes, may include alignas specifier class-head-name - the name of the class that's being defined. Optionally qualified, optionally followed by keyword final. The name may be omitted, in which case the class is unnamed (note that unnamed class cannot be final) base-clause - optional list of one or more parent classes and the model of inheritance used for each (see derived class) member-specification - list of access specifiers, member object and member function declarations and definitions (see below) See classes for general overview of the syntax. If class-key is union, the declaration introduces a union type. Contents 1 Forward declaration 2 Member specification 3 Local classes 4 See also [edit] Forward declaration A declaration of the following form class-key attr identifier ; Declares a class type which will be defined later in this scope. Until the definition appears, this class name has incomplete type. This allows classes that refer to each other: class Vector; // forward declaration class Matrix { // ... friend Vector operator*(const Matrix&, const Vector&); }; class Vector { // ... friend Vector operator*(const Matrix&, const Vector&); }; and if a particular source file only uses pointers