Private accessing problem when overloading(friends)

I've never had this issue before, it's got to be a really simple mistake im missing here but i just cant figure it out. Here is the error report im getting:

F:\C++\stockType\stockType.h||In function 'std::istream& operator>>(std::istream&, stockType&)':|
F:\C++\stockType\stockType.h|8|error: 'char* stockType::symbol' is private|
F:\C++\stockType\stockType.cpp|28|error: within this context|
F:\C++\stockType\stockType.h|10|error: 'double stockType::openingPrice' is private|
F:\C++\stockType\stockType.cpp|29|error: within this context|
F:\C++\stockType\stockType.h|11|error: 'double stockType::closingPrice' is private|
F:\C++\stockType\stockType.cpp|30|error: within this context|
F:\C++\stockType\stockType.h|12|error: 'double stockType::todayHigh' is private|
F:\C++\stockType\stockType.cpp|31|error: within this context|
F:\C++\stockType\stockType.h|13|error: 'double stockType::todayLow' is private|
F:\C++\stockType\stockType.cpp|32|error: within this context|
F:\C++\stockType\stockType.h|14|error: 'double stockType::prevClose' is private|
F:\C++\stockType\stockType.cpp|33|error: within this context|
F:\C++\stockType\stockType.h|15|error: 'int stockType::volume' is private|
F:\C++\stockType\stockType.cpp|34|error: within this context|
F:\C++\stockType\stockType.h||In function 'std::ostream& operator<<(std::ostream&, const stockType&)':|
F:\C++\stockType\stockType.h|8|error: 'char* stockType::symbol' is private|
F:\C++\stockType\stockType.cpp|42|error: within this context|
F:\C++\stockType\stockType.h|10|error: 'double stockType::openingPrice' is private|
F:\C++\stockType\stockType.cpp|43|error: within this context|
F:\C++\stockType\stockType.h|11|error: 'double stockType::closingPrice' is private|
F:\C++\stockType\stockType.cpp|44|error: within this context|
F:\C++\stockType\stockType.h|12|error: 'double stockType::todayHigh' is private|
F:\C++\stockType\stockType.cpp|45|error: within this context|
F:\C++\stockType\stockType.h|13|error: 'double stockType::todayLow' is private|
F:\C++\stockType\stockType.cpp|46|error: within this context|
F:\C++\stockType\stockType.h|14|error: 'double stockType::prevClose' is private|
F:\C++\stockType\stockType.cpp|47|error: within this context|
F:\C++\stockType\stockType.cpp|17|error: 'float stockType::percent_calc()' is private|
F:\C++\stockType\stockType.cpp|48|error: within this context|
F:\C++\stockType\stockType.cpp|48|error: passing 'const stockType' as 'this' argument of 'float stockType::percent_calc()' discards qualifiers|
F:\C++\stockType\stockType.h|15|error: 'int stockType::volume' is private|
F:\C++\stockType\stockType.cpp|49|error: within this context|
||=== Build finished: 31 errors, 0 warnings ===|


so i obviously cant access my private members, yet i made my functions friends to the class so they would be able to access them.

stockType.h
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
26
#ifndef STOCKTYPE_H
#define STOCKTYPE_H
#include <iostream>

class stockType
{
    private:
        char * symbol;
        enum{BUFFER = 20};
        double openingPrice;
        double closingPrice;
        double todayHigh;
        double todayLow;
        double prevClose;
        int volume;
        float percent_calc();
    public:
        friend std::ifstream & operator>>(std::ifstream infile, stockType & st);
        friend std::ostream & operator<<(std::ostream os,const stockType & st);
        stockType();
        ~stockType();

};

#endif // STOCKTYPE_H


stockType.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stockType.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#define TAB 4

stockType::stockType()
{
    symbol = new char[BUFFER];
}

stockType::~stockType()
{
    delete [] symbol;
}

float stockType::percent_calc()
{
    float temp = closingPrice / prevClose;
    temp--;
    temp *= 100;
    return temp;
}

std::ifstream & operator>>(std::ifstream & infile,stockType & st)
{
    infile
    >> st.symbol
    >> st.openingPrice
    >> st.closingPrice
    >> st.todayHigh
    >> st.todayLow
    >> st.prevClose
    >> st.volume;
    return infile;
}

std::ostream & operator<<(std::ostream & os,const stockType & st)
{

    os
    << std::setw(TAB) << st.symbol
    << std::setw(TAB) << st.openingPrice
    << std::setw(TAB) << st.closingPrice
    << std::setw(TAB) << st.todayHigh
    << std::setw(TAB) << st.todayLow
    << std::setw(TAB) << st.prevClose
    << std::setw(TAB) << st.percent_calc() << "%"
    << std::setw(TAB) << st.volume;
    return os;
}


i appreciate any help, thanks!
Last edited on
stockType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ifndef STOCKTYPE_H
#define STOCKTYPE_H
#include <iostream>

class stockType
{
    private:
        ...
    public:
        std::ifstream & operator>>(std::ifstream & infile);
        std::ostream & operator<<(std::ostream & os);
        stockType();
        ~stockType();

};

#endif // STOCKTYPE_H



stockType.cpp
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
26
27
28
29
...

std::ifstream & stockType::operator>>(std::ifstream & infile)
{
    infile
    >> symbol
    >> openingPrice
    >> closingPrice
    >> todayHigh
    >> todayLow
    >> prevClose
    >> volume;
    return infile;
}

std::ostream & stockType::operator<<(std::ostream & os)
{

    os
    << std::setw(TAB) << symbol
    << std::setw(TAB) << openingPrice
    << std::setw(TAB) << closingPrice
    << std::setw(TAB) << todayHigh
    << std::setw(TAB) << todayLow
    << std::setw(TAB) << prevClose
    << std::setw(TAB) << percent_calc() << "%"
    << std::setw(TAB) << volume;
    return os;
}
Try moving the stream overloads implementation into the header.
Edit: And change percent_calc to float stockType::percent_calc() const;
Edit2: Actually, just remove the const from the ostream and add the reference operator to the istream declarations.
Last edited on
Topic archived. No new replies allowed.