Need help

Hi,

I have a problem, i made something, but when i compile i receive an error, something like this : "error : int Bus::_lineMap is private".

I don't know why, because the _lineMap variable is accessed using Bus() constructor.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>

using namespace std;

class Bus
{
private:
    int _lineMap;
    int _columnMap;
public:
    Bus(int lH, int cH);
    int GiveLineMap() const;
    int GiveColumnMap() const;
};

Bus::Bus(int lH, int cH)
{
    _lineMap = lH;
    _columnMap = cH;
}

int Bus::GiveLineMap() const
{
    return _lineMap;
}

int Bus::GiveColumnMap() const
{
    return _columnMap;
}

class Sector
{
private:
    int _line;
    int _column;
public:
    Sector(const Bus& bus);
    void Print();
};

Sector::Sector(const Bus &bus)
{
    cout << "\n Conversion constructor!" <<endl;
    _line = bus._lineMap;
    _column = bus._columnMap;
}

void Sector::Print()
{
    cout << "\n Line : " << _line <<endl;
    cout << "\n Column : " << _column <<endl;
}

void PrintOnMap(Sector sect)
{
    cout << "\n Print on map!" <<endl;
    sect.Print();
}

int main()
{
    Bus oneBus(3, 4);
    PrintOnMap(oneBus);
    return 0;
}
The error you just gave is not the error you are getting, please always post the entire error code.

In constructor 'Sector::Sector(const Bus&)': 8:9: error: 'int Bus::_lineMap' is private 45:17: error: within this context 9:9: error: 'int Bus::_columnMap' is private 46:19: error: within this context

Thats the error you're getting. And its because of this -

1
2
3
4
5
6
Sector::Sector(const Bus &bus)
{
    cout << "\n Conversion constructor!" <<endl;
    _line = bus._lineMap;
    _column = bus._columnMap;
}


using the object bus, you can only access public functions. lineMap and columnMap are private and you cant acces them.
Yes, you have right, so i understand that the solution is to make this replace :

Sector::Sector(const Bus & bus)
{
cout << "\n Conversion constructor!" <<endl;
_line = bus.GiveLineMap();
_column = bus.GiveColumnMap();
}


In this way works;
Thank you.
Yes! That will work if you change one thing. The const.

Remove the const.

1
2
3
Sector::Sector(Bus &bus)

Sector(Bus& bus);
Topic archived. No new replies allowed.