PDA

View Full Version : compiler error in calling friend function!!??



aurora
14th November 2011, 04:08
i declared a friend function in one class...as shown below...

classa.h


class A
{
public:
friend void friendFunction(void);
void somefunction(void);
.................
}




classa.cpp

void class A::somefunction()
{
......
friendFunction();
}



main.cpp

..........
.............
void frienFunction(void)
{
/* function implementation...*/
}







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

Santosh Reddy
14th November 2011, 05:12
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.



void class A::somefunction()
{
......
friendFunction();
}

by the way the class keyword is not required here.

aurora
14th November 2011, 05:27
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...

Santosh Reddy
14th November 2011, 05:39
so show how u are doing it, just copy & paste don't re-type.

ChrisW67
14th November 2011, 05:57
From declaration:

friend void friendFunction(void);
and implementation:

void frienFunction(void)

These function names are not the same.

aurora
14th November 2011, 06:05
so show how u are doing it, just copy & paste don't re-type.

window.h


#ifndef WINDOW_H
#define WINDOW_H

#include <QDialog>
#include <QDir>





class Window : public QDialog
{
Q_OBJECT
friend void friendFunction(void);

public:

QString string;
QString searchKey;
Window(QWidget *parent = 0);

private slots:
void browse();
void find();
};



window.cpp

#include <QtGui>

#include "window.h"
#include<iostream>
void Window::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());

friendFunction();

}
}



main.cpp

#include <iostream>
using namespace std;

QString global_var("ABCD");
QString filename;
QString searchKey;
QStringList filenameList;
void friendFunction(void);


int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(mdi);

QApplication app(argc, argv);
Window window;
window.show();
return app.exec();




}



void friendFunction(void)
{
cout<<"FRIEND CALLED "<<endl;
}

Santosh Reddy
14th November 2011, 06:22
You don't need friend function to do so. Just declare as a regular function


//window.h
#ifndef WINDOW_H
#define WINDOW_H


#include <QDialog>
#include <QDir>


extern void friendFunction(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


class Window : public QDialog
{
Q_OBJECT
public:


QString string;
QString searchKey;
Window(QWidget *parent = 0);


private slots:
void browse();
void find();
};



//window.cpp
#include <QtGui>
#include "window.h"
#include<iostream>


void Window::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
friendFunction();
}

ChrisW67
14th November 2011, 06:34
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:

void friendFunction();
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.

aurora
14th November 2011, 06:46
You don't need friend function to do so. Just declare as a regular function


//window.h
#ifndef WINDOW_H
#define WINDOW_H


#include <QDialog>
#include <QDir>


extern void friendFunction(void); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


class Window : public QDialog
{
Q_OBJECT
public:


QString string;
QString searchKey;
Window(QWidget *parent = 0);


private slots:
void browse();
void find();
};



//window.cpp
#include <QtGui>
#include "window.h"
#include<iostream>


void Window::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
friendFunction();
}



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

nish
14th November 2011, 07:03
Then it also needs an object of the class to be passed as argument (or if you have some global stuff)

ChrisW67
15th November 2011, 00:13
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.

xtofl
15th November 2011, 22:25
I agree to declare the function as an extern, but not in the class header, rather in the cpp, no?