friend functions

I have declared the prototype of the friend function in the class Stocks. When I call such class in main i get:
stock.cpp: In function ‘int main(int, char**)’:
stock.cpp:38: error: ‘readFile’ was not declared in this scope

I have done this before in other programs and this one just seems to hate me. I cant take that kind of rejection :(

This is a snippet of the program:
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
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;

class Stocks
{
   friend void readFile(int& argc, char* argv[]);
public:
private:
   string transType;
   int shares;
   double price;
};

//void readFile(int& argc, char* argv[]);

int main(int argc, char* argv[])
{
   readFile (argc, argv);
}

void readFile (int& argc, char* argv[])
{
}


Haha I tried using my shorcut to save this, muscle memory i guess. Any help would be appreciated because I have looked at this for TOO long. Thanks.

oh on the output when it says stock.cpp:38: error: ‘readFile’ was not declared in this scope, it was referring to line 20 in the snippet.
The problem is that while the function will not be defined or declared it is invisible. So you can either to place the definition before the main or declare the function before the main. For example

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
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;

class Stocks
{
   friend void readFile(int& argc, char* argv[]);
public:
private:
   string transType;
   int shares;
   double price;
};

void readFile(int& argc, char* argv[]);

int main(int argc, char* argv[])
{
   readFile (argc, argv);
}

void readFile (int& argc, char* argv[])
{
}


So you shall remove the comment before already present declaration of your function.
Last edited on
well I wanted to use the prototype already in the Stocks class so I can access private member variables in my readFile() function. The commented out part was just in case I had to do a workaround.
nvm you win I just tried it and it works, im an idiot. Thanks I appreciate your help.
Topic archived. No new replies allowed.