Results 1 to 12 of 12

Thread: compiler error in calling friend function!!??

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default compiler error in calling friend function!!??

    i declared a friend function in one class...as shown below...

    classa.h

    Qt Code:
    1. class A
    2. {
    3. public:
    4. friend void friendFunction(void);
    5. void somefunction(void);
    6. .................
    7. }
    To copy to clipboard, switch view to plain text mode 


    classa.cpp
    Qt Code:
    1. void class A::somefunction()
    2. {
    3. ......
    4. friendFunction();
    5. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. ..........
    2. .............
    3. void frienFunction(void)
    4. {
    5. /* function implementation...*/
    6. }
    To copy to clipboard, switch view to plain text mode 





    but i'm getting compiler error saying that "friendFunction not declared in this class"
    please help me whats the wrong here!??

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: compiler error in calling friend function!!??

    You don't need friend functions to do what you are trying to do.

    friend (friendship relation) is required when void friendFunction(void) calls A::someFunction(), which you are not doing.

    Qt Code:
    1. void class A::somefunction()
    2. {
    3. ......
    4. friendFunction();
    5. }
    To copy to clipboard, switch view to plain text mode 
    by the way the class keyword is not required here.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: compiler error in calling friend function!!??

    Thank u Santosh...
    But my actual problem is i have three different classes....i want a function which can access private variables and functions of all three classes....
    Thats why i thought of using friend function... but i'm getting error....is the format in which i declared is correct?

    The error u identified "class " in member function implementation is not in actual code, here i made mistake while typing...

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: compiler error in calling friend function!!??

    so show how u are doing it, just copy & paste don't re-type.

  5. The following user says thank you to Santosh Reddy for this useful post:

    aurora (14th November 2011)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: compiler error in calling friend function!!??

    From declaration:
    Qt Code:
    1. friend void friendFunction(void);
    To copy to clipboard, switch view to plain text mode 
    and implementation:
    Qt Code:
    1. void frienFunction(void)
    To copy to clipboard, switch view to plain text mode 

    These function names are not the same.

  7. The following user says thank you to ChrisW67 for this useful post:

    aurora (14th November 2011)

  8. #6
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: compiler error in calling friend function!!??

    Quote Originally Posted by Santosh Reddy View Post
    so show how u are doing it, just copy & paste don't re-type.
    window.h

    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QDialog>
    5. #include <QDir>
    6.  
    7.  
    8.  
    9.  
    10.  
    11. class Window : public QDialog
    12. {
    13. Q_OBJECT
    14. friend void friendFunction(void);
    15.  
    16. public:
    17.  
    18. QString string;
    19. QString searchKey;
    20. Window(QWidget *parent = 0);
    21.  
    22. private slots:
    23. void browse();
    24. void find();
    25. };
    To copy to clipboard, switch view to plain text mode 


    window.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "window.h"
    4. #include<iostream>
    5. void Window::browse()
    6. {
    7. QString directory = QFileDialog::getExistingDirectory(this,
    8. tr("Find Files"), QDir::currentPath());
    9.  
    10. friendFunction();
    11.  
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 



    main.cpp
    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. QString global_var("ABCD");
    5. QString filename;
    6. QString searchKey;
    7. QStringList filenameList;
    8. void friendFunction(void);
    9.  
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. Q_INIT_RESOURCE(mdi);
    14.  
    15. QApplication app(argc, argv);
    16. Window window;
    17. window.show();
    18. return app.exec();
    19.  
    20.  
    21.  
    22.  
    23. }
    24.  
    25.  
    26.  
    27. void friendFunction(void)
    28. {
    29. cout<<"FRIEND CALLED "<<endl;
    30. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: compiler error in calling friend function!!??

    You don't need friend function to do so. Just declare as a regular function

    Qt Code:
    1. //window.h
    2. #ifndef WINDOW_H
    3. #define WINDOW_H
    4.  
    5.  
    6. #include <QDialog>
    7. #include <QDir>
    8.  
    9.  
    10. extern void friendFunction(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    11.  
    12.  
    13. class Window : public QDialog
    14. {
    15. Q_OBJECT
    16. public:
    17.  
    18.  
    19. QString string;
    20. QString searchKey;
    21. Window(QWidget *parent = 0);
    22.  
    23.  
    24. private slots:
    25. void browse();
    26. void find();
    27. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //window.cpp
    2. #include <QtGui>
    3. #include "window.h"
    4. #include<iostream>
    5.  
    6.  
    7. void Window::browse()
    8. {
    9. QString directory = QFileDialog::getExistingDirectory(this,
    10. tr("Find Files"), QDir::currentPath());
    11. friendFunction();
    12. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Santosh Reddy for this useful post:

    aurora (14th November 2011)

  11. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: compiler error in calling friend function!!??

    Assuming that friendFunction() will ultimately need access to private parts of your Window class (it doesn't at the moment as Santosh Reddy points out)...

    Line 14 of window.h is a grant of privileges, not a forward declaration of a function friendFunction(), which is what is needed in order for your implementation to compile. Add this line:
    Qt Code:
    1. void friendFunction();
    To copy to clipboard, switch view to plain text mode 
    to the top of window.cpp. This forward declaration would typically be in a header included by the implementations that need to know that the function exists.

  12. The following user says thank you to ChrisW67 for this useful post:

    aurora (14th November 2011)

  13. #9
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: compiler error in calling friend function!!??

    Quote Originally Posted by Santosh Reddy View Post
    You don't need friend function to do so. Just declare as a regular function

    Qt Code:
    1. //window.h
    2. #ifndef WINDOW_H
    3. #define WINDOW_H
    4.  
    5.  
    6. #include <QDialog>
    7. #include <QDir>
    8.  
    9.  
    10. extern void friendFunction(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    11.  
    12.  
    13. class Window : public QDialog
    14. {
    15. Q_OBJECT
    16. public:
    17.  
    18.  
    19. QString string;
    20. QString searchKey;
    21. Window(QWidget *parent = 0);
    22.  
    23.  
    24. private slots:
    25. void browse();
    26. void find();
    27. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //window.cpp
    2. #include <QtGui>
    3. #include "window.h"
    4. #include<iostream>
    5.  
    6.  
    7. void Window::browse()
    8. {
    9. QString directory = QFileDialog::getExistingDirectory(this,
    10. tr("Find Files"), QDir::currentPath());
    11. friendFunction();
    12. }
    To copy to clipboard, switch view to plain text mode 

    ok thak u santosh...
    but what if my function needs to access the private variables of the class?

  14. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: compiler error in calling friend function!!??

    Then it also needs an object of the class to be passed as argument (or if you have some global stuff)

  15. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: compiler error in calling friend function!!??

    Quote Originally Posted by nish View Post
    Then it also needs an object of the class to be passed as argument (or if you have some global stuff)
    Or it could only access private static members of the class.

  16. #12
    Join Date
    Nov 2011
    Location
    Belgium
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: compiler error in calling friend function!!??

    I agree to declare the function as an extern, but not in the class header, rather in the cpp, no?

Similar Threads

  1. Calling external DLL function with Qt
    By yaba in forum Newbie
    Replies: 2
    Last Post: 29th September 2011, 07:47
  2. fatal error C1001: An internal error has occurred in the compiler
    By noodles in forum Installation and Deployment
    Replies: 0
    Last Post: 12th August 2010, 11:24
  3. Compiler error when calling QObject::connect. What am I missing?
    By themanwiththequestion in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2010, 14:33
  4. friend function in QT
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2009, 14:59
  5. QTextBrowser and friend function
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2006, 17:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.