‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected

May 7, 2020 at 9:36am
error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected

hi to all, I've centos7.5 in VM, and codeblocks17.12, i am builing small project in C++. In this I am trying to used a vector to prrintmenu in console. header file is :-

getchoice :-
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
#ifndef GETCHOICE_H_INCLUDED
#define GETCHOICE_H_INCLUDED

#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <X11/Xlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <iostream>
#include <ios>
#include <cstdio>

//using namespace std;
using std::vector;
using std::string;
using std::istream;
using std::ostream;

class GetChoice
{

public:

    vector<string> *choice;
    vector<string> *menu;

    istream in;
    ostream out;


    GetChoice();
    ~GetChoice();
    void gotoxy(int x,int y);
    void clrscr();
    void drawrect();

    int getchoice_r(vector<string>&choices,istream *in ,ostream *out);
};

#endif // GETCHOICE_H_INCLUDED 

and in getchoice.cppp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "getchoice.h"

using namespace std;


GetChoice::GetChoice()
{
    choice = nullptr;
    menu   = nullptr;

    menu->push_back("1.  Show All Products");
    menu->push_back("2.  Add New Product.");
    menu->push_back("3.  Add New Members.");
    menu->push_back("4.  View An Existing Product Records.");
    menu->push_back("5.  View An Existing Member's Record.");
    menu->push_back("6.  Billing");
    menu->push_back("7.  Today's Sail.");
    menu->push_back("8.  Modify Product Record.");
    menu->push_back("9.  Modify Member's Record.");
    menu->push_back("10. Instructions.");
    menu->push_back("11. Exit.");
}

...


in main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "getchoice.h"

using namespace std;

int main()
{

    vector<string> *choice;
    vector<string> *menu;

    istream in;
    ostream out;

    GetChoice *cho = new GetChoice;
    cho->getchoice_r(cho->menu, &(cho->in), &(cho->out));
    return 0;
}


this gives following error :-
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

g++ -Wall -fexceptions -O2 -std=c++11 -m64 -fpermissive  -c /opt/projects/cb/cbs2/cbs/getchoice.cpp -o obj/Release/getchoice.o
g++ -Wall -fexceptions -O2 -std=c++11 -m64 -fpermissive  -c /opt/projects/cb/cbs2/cbs/main.cpp -o obj/Release/main.o
g++  -o bin/Release/cbs obj/Release/getchoice.o obj/Release/main.o  -s -m64  
In file included from /usr/include/c++/4.8.2/sstream:38:0,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/ccomplex:38,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:52,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/istream: In constructor ‘GetChoice::GetChoice()’:
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
In file included from /usr/include/c++/4.8.2/istream:39:0,
                 from /usr/include/c++/4.8.2/sstream:38,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/ccomplex:38,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:52,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/ostream:384:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp: In member function ‘int GetChoice::getchoice_r(std::vector<std::basic_string<char> >&, std::istream*, std::ostream*)’:
/opt/projects/cb/cbs2/cbs/getchoice.cpp:67:33: warning: invalid user-defined conversion from ‘std::istream {aka std::basic_istream<char>}’ to ‘FILE* {aka _IO_FILE*}’ [-fpermissive]
             getline(&ch, &n, *in);
                                 ^
In file included from /usr/include/c++/4.8.2/ios:44:0,
                 from /usr/include/c++/4.8.2/istream:38,
                 from /usr/include/c++/4.8.2/sstream:38,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/ccomplex:38,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:52,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/bits/basic_ios.h:115:7: note: candidate is: std::basic_ios<_CharT, _Traits>::operator void*() const [with _CharT = char; _Traits = std::char_traits<char>] <near match>
       operator void*() const
       ^
/usr/include/c++/4.8.2/bits/basic_ios.h:115:7: note:   no known conversion for implicit ‘this’ parameter from ‘void*’ to ‘FILE* {aka _IO_FILE*}’
/opt/projects/cb/cbs2/cbs/getchoice.cpp:31:24: warning: unused variable ‘len’ [-Wunused-variable]
     int i, j, k, l, m, len;
                        ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:35:11: warning: unused variable ‘p’ [-Wunused-variable]
     char *p, *wp;
           ^
In file included from /usr/include/c++/4.8.2/iostream:40:0,
                 from /opt/projects/cb/cbs2/cbs/main.cpp:1:
/usr/include/c++/4.8.2/istream: In function ‘int main()’:
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/main.cpp:12:13: error: within this context
     istream in;
             ^
In file included from /usr/include/c++/4.8.2/iostream:39:0,
                 from /opt/projects/cb/cbs2/cbs/main.cpp:1:
/usr/include/c++/4.8.2/ostream:384:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream()
       ^
/opt/projects/cb/cbs2/cbs/main.cpp:13:13: error: within this context
     ostream out;
             ^
/opt/projects/cb/cbs2/cbs/main.cpp:16:56: error: no matching function for call to ‘GetChoice::getchoice_r(std::vector<std::basic_string<char> >*&, std::istream*, std::ostream*)’
     cho->getchoice_r(cho->menu, &(cho->in), &(cho->out));
                                                        ^
/opt/projects/cb/cbs2/cbs/main.cpp:16:56: note: candidate is:
In file included from /opt/projects/cb/cbs2/cbs/main.cpp:2:0:
/opt/projects/cb/cbs2/cbs/getchoice.h:38:9: note: int GetChoice::getchoice_r(std::vector<std::basic_string<char> >&, std::istream*, std::ostream*)
     int getchoice_r(vector<string>&choices,istream *in ,ostream *out);
         ^
/opt/projects/cb/cbs2/cbs/getchoice.h:38:9: note:   no known conversion for argument 1 from ‘std::vector<std::basic_string<char> >*’ to ‘std::vector<std::basic_string<char> >&’
/opt/projects/cb/cbs2/cbs/main.cpp:9:21: warning: unused variable ‘choice’ [-Wunused-variable]
     vector<string> *choice;
                     ^
/opt/projects/cb/cbs2/cbs/main.cpp:10:21: warning: unused variable ‘menu’ [-Wunused-variable]
     vector<string> *menu;
                     ^
Process terminated with status 1 (0 minute(s), 0 second(s))
 
/opt/projects/cb/cbs2/cbs/getchoice.cpp:35:15: warning: unused variable ‘wp’ [-Wunused-variable]
     char *p, *wp;
               ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:40:1: warning: label ‘LABEL1’ defined but not used [-Wunused-label]
 LABEL1: do {
 ^
Process terminated with status 1 (0 minute(s), 0 second(s))
9 error(s), 7 warning(s) (0 minute(s), 0 second(s))
Last edited on May 7, 2020 at 9:37am
May 7, 2020 at 10:07am
#include <X11/Xlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
Avoid unnecessary #include "the entire world" programming style.

> vector<string> *choice;
> vector<string> *menu;
Just remove all the *'s from your code.
There's no reason at all for any manual memory allocation.

> int getchoice_r(vector<string>&choices,istream *in ,ostream *out);
Make the streams references.
int getchoice_r(vector<string>&choices,istream &in ,ostream &out);


Then the call becomes
cho.getchoice_r(cho.menu, in, out);
May 7, 2020 at 4:01pm
I did as you said but still have this error :
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

g++ -Wall -fexceptions -O2 -std=c++11 -m64 -fpermissive  -c /opt/projects/cb/cbs2/cbs/getchoice.cpp -o obj/Release/getchoice.o
g++ -Wall -fexceptions -O2 -std=c++11 -m64 -fpermissive  -c /opt/projects/cb/cbs2/cbs/main.cpp -o obj/Release/main.o
g++  -o bin/Release/cbs obj/Release/getchoice.o obj/Release/main.o  -s -m64  
In file included from /usr/include/c++/4.8.2/sstream:38:0,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/ccomplex:38,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:52,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/istream: In constructor ‘GetChoice::GetChoice()’:
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
In file included from /usr/include/c++/4.8.2/istream:39:0,
                 from /usr/include/c++/4.8.2/sstream:38,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/ccomplex:38,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:52,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/ostream:384:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp: In member function ‘int GetChoice::getchoice_r(std::vector<std::basic_string<char> >&, std::istream&, std::ostream&)’:
/opt/projects/cb/cbs2/cbs/getchoice.cpp:79:32: warning: invalid user-defined conversion from ‘std::istream {aka std::basic_istream<char>}’ to ‘FILE* {aka _IO_FILE*}’ [-fpermissive]
             getline(&ch, &n, in);
                                ^
In file included from /usr/include/c++/4.8.2/iostream:40:0,
                 from /opt/projects/cb/cbs2/cbs/main.cpp:1:
/usr/include/c++/4.8.2/istream: In function ‘int main()’:
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/main.cpp:12:13: error: within this context
     istream in;
             ^
In file included from /usr/include/c++/4.8.2/iostream:39:0,
                 from /opt/projects/cb/cbs2/cbs/main.cpp:1:
/usr/include/c++/4.8.2/ostream:384:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream()
       ^
/opt/projects/cb/cbs2/cbs/main.cpp:13:13: error: within this context
     ostream out;
             ^
Process terminated with status 1 (0 minute(s), 0 second(s))
Last edited on May 7, 2020 at 4:17pm
May 7, 2020 at 4:34pm
Start simpler.
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
#include <iostream>
#include <vector>
#include <string>
#include <iostream>
#include <ios>

//using namespace std;
using std::vector;
using std::string;
using std::istream;
using std::ostream;

class GetChoice
{

public:

    vector<string> choice;
    vector<string> menu;

    GetChoice();
    ~GetChoice() {}
    void gotoxy(int x,int y);
    void clrscr();
    void drawrect();

    int getchoice_r(istream &in, ostream &out);
};

GetChoice::GetChoice()
{
    menu.push_back("1.  Show All Products");
    menu.push_back("2.  Add New Product.");
    menu.push_back("3.  Add New Members.");
    menu.push_back("4.  View An Existing Product Records.");
    menu.push_back("5.  View An Existing Member's Record.");
    menu.push_back("6.  Billing");
    menu.push_back("7.  Today's Sail.");
    menu.push_back("8.  Modify Product Record.");
    menu.push_back("9.  Modify Member's Record.");
    menu.push_back("10. Instructions.");
    menu.push_back("11. Exit.");
}

int GetChoice::getchoice_r(istream &in, ostream &out) {
  return 0;
}

int main()
{
    GetChoice cho;
    cho.getchoice_r(std::cin, std::cout);
    return 0;
}



> cho->getchoice_r(cho->menu, &(cho->in), &(cho->out));
It makes no sense to pass pointers to your own member variables.
May 10, 2020 at 4:37am
now i did like this :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "getchoice.h"

using namespace std;

int main()
{

//    vector<string> *choice;
//    vector<string> *menu;

//    istream in;
//    ostream out;

    GetChoice cho;
    cho.getchoice_r(cho.menu, std::cin, std::cout);
    return 0;
}

but still having following error. i am not getting soluiton of this.

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

g++ -Wall -fexceptions -g  -c /opt/projects/cb/cbs2/cbs/getchoice.cpp -o obj/Debug/getchoice.o
g++  -o bin/Debug/cbs obj/Debug/getchoice.o obj/Debug/main.o   
In file included from /usr/include/c++/4.8.2/sstream:38:0,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:66,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/istream: In constructor ‘GetChoice::GetChoice()’:
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
In file included from /usr/include/c++/4.8.2/istream:39:0,
                 from /usr/include/c++/4.8.2/sstream:38,
                 from /usr/include/c++/4.8.2/complex:45,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/stdc++.h:66,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:4,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/ostream:384:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
Last edited on May 10, 2020 at 4:56am
May 10, 2020 at 5:15am
> but still having following error. i am not getting soluiton of this.
Posting the .h would help.

> cho.getchoice_r(cho.menu, std::cin, std::cout);
Again, why are you passing your own member variables to yourself?

May 10, 2020 at 7:45am
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
#ifndef GETCHOICE_H_INCLUDED
#define GETCHOICE_H_INCLUDED

#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <X11/Xlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <iostream>
#include <ios>
#include <cstdio>

//using namespace std;
using std::vector;
using std::string;
using std::istream;
using std::ostream;

class GetChoice
{


public:

    vector<string> choice;
    vector<string> menu;

    istream in;
    ostream out;


    GetChoice();
    ~GetChoice();
    void gotoxy(int x,int y);
    void clrscr();
    void drawrect();

    int getchoice_r(vector<string>& choices,istream &in ,ostream &out);
};


actually i want to print a menu defined in GetChoice constructor as follows
constructor is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
GetChoice::GetChoice()
{

    menu.push_back("1.  Show All Products");
    menu.push_back("2.  Add New Product.");
    menu.push_back("3.  Add New Members.");
    menu.push_back("4.  View An Existing Product Records.");
    menu.push_back("5.  View An Existing Member's Record.");
    menu.push_back("6.  Billing");
    menu.push_back("7.  Today's Sail.");
    menu.push_back("8.  Modify Product Record.");
    menu.push_back("9.  Modify Member's Record.");
    menu.push_back("10. Instructions.");
    menu.push_back("11. Exit.");

}


but question is :- why this errors :-
1
2
3
4
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context

mine getchoice.cpp is
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

int GetChoice::getchoice_r(vector<string>& choices,istream &in ,ostream &out)
{

    int chosen = 0;
    char *ch, selection[3], choicechar;
    vector<string> &option = choices;
    int i, j, k, l, m, len;
    string strch;

    size_t n = 5;

    char *p, *wp;

    ch = new char[5];

LABEL1: do {
        drawrect();

        gotoxy(25, 3);
        cout <<"MENU";
        gotoxy(23, 4);

        for( i = 23; i < 31; i++)
            cout << "-";

        l = 5;
        gotoxy(15, l);
        option = choices;
        l++;
        vector<string> ::const_iterator i = option.begin();
        while(i != option.end())
        {
            gotoxy(15, l);
            cout << *i;
            l++;
        }

        gotoxy(15, ++l);
        cout << "Enter choice : ";

            getline( in, strch);

            m = atoi(strch.c_str());

        option = choices;

        j = 0;
        i = option.begin();
        while(i != option.end())
        {
            k = 0;
            while((choicechar = option[j][k]) != '.')
            {
                selection[k] = choicechar;
                k++;
            }
            selection[k] = '\0';
            if(m == atoi(selection))
            {
                chosen = 1;
                strcpy(ch, "");
                break;
            }

            j++;


        }
    } while(!chosen);

	return m;
}

void GetChoice::gotoxy(int x,int y)
{
    printf("%c[%d;%df",0x1B,y,x);
}

void GetChoice::clrscr()
{
    gotoxy(0,0);

    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    for (int x = 0; x < w.ws_row; x++)
    {
        for(int y = 0; y < w.ws_col; y++)
        {
            cout << " ";
        }
    }
    gotoxy(0, 0);
}
void GetChoice::drawrect()
{
    winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    for(int i = 0; i <= w.ws_col; i++)
    {
        gotoxy(i, 2);
        cout << "-";

        gotoxy(i, w.ws_row);
        cout  << "-";
    }
    for ( int i = 2; i <= w.ws_row; i++)
    {
        gotoxy(1, i);
        cout << "|";
        gotoxy(w.ws_col, i);
        cout << "|";
    }
    gotoxy(0,0);
}
May 10, 2020 at 9:26am
Consider....
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <vector>
#include <string>
#include <iostream>
#include <ios>
#include <unistd.h>
#include <sys/ioctl.h>
#include <cstdio>  // find c++ ways of doing printf
#include <cstdlib>  // and atoi

//using namespace std;
using std::vector;
using std::string;
using std::istream;
using std::ostream;

class GetChoice {

public:

  vector < string > choice;
  vector < string > menu;

  GetChoice();
  ~GetChoice() {
  } void gotoxy(int x, int y);
  void clrscr();
  void drawrect();

  int getchoice_r(istream & in, ostream & out);
};

// deleted pointless choices parameter
int GetChoice::getchoice_r(istream & in, ostream & out)
{
  int i, l, m;                  // better names
  string strch;
  do {
    drawrect();

    gotoxy(25, 3);
    out << "MENU";
    gotoxy(23, 4);

    for (i = 23; i < 31; i++)
      out << "-";

    l = 5;
    gotoxy(15, l);
    l++;
    vector < string >::const_iterator i = menu.begin();
    while (i != menu.end()) {
      gotoxy(15, l);
      out << *i;
      i++;
      l++;
    }

    gotoxy(15, ++l);
    out << "Enter choice : ";

    getline(in, strch);

    m = atoi(strch.c_str());

  } while (!(m >= 1 && m <= 11));

  return m;
}

void GetChoice::gotoxy(int x, int y)
{
  printf("%c[%d;%df", 0x1B, y, x);
}

void GetChoice::clrscr()
{
  gotoxy(0, 0);

  struct winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

  for (int x = 0; x < w.ws_row; x++) {
    for (int y = 0; y < w.ws_col; y++) {
      std::cout << " ";
    }
  }
  gotoxy(0, 0);
}

void GetChoice::drawrect()
{
  winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

  for (int i = 0; i <= w.ws_col; i++) {
    gotoxy(i, 2);
    std::cout << "-";

    gotoxy(i, w.ws_row);
    std::cout << "-";
  }
  for (int i = 2; i <= w.ws_row; i++) {
    gotoxy(1, i);
    std::cout << "|";
    gotoxy(w.ws_col, i);
    std::cout << "|";
  }
  gotoxy(0, 0);
}

GetChoice::GetChoice()
{
  menu.push_back("1.  Show All Products");
  menu.push_back("2.  Add New Product.");
  menu.push_back("3.  Add New Members.");
  menu.push_back("4.  View An Existing Product Records.");
  menu.push_back("5.  View An Existing Member's Record.");
  menu.push_back("6.  Billing");
  menu.push_back("7.  Today's Sail.");
  menu.push_back("8.  Modify Product Record.");
  menu.push_back("9.  Modify Member's Record.");
  menu.push_back("10. Instructions.");
  menu.push_back("11. Exit.");
}

int main()
{
  GetChoice cho;
  cho.getchoice_r(std::cin, std::cout);
  return 0;
}

May 10, 2020 at 1:07pm
thanks a lot, but one last question please explain. I did as you said. your program is working fine. but in my program why this error and how to avoid it for future reference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/usr/include/c++/4.8.2/istream:606:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_istream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
                      ^
In file included from /usr/include/c++/4.8.2/iostream:39:0,
                 from /opt/projects/cb/cbs2/cbs/getchoice.h:14,
                 from /opt/projects/cb/cbs2/cbs/getchoice.cpp:1:
/usr/include/c++/4.8.2/ostream:384:7: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
       basic_ostream()
       ^
/opt/projects/cb/cbs2/cbs/getchoice.cpp:6:22: error: within this context
 GetChoice::GetChoice()
May 10, 2020 at 2:14pm
> istream in;
> ostream out;
https://www.cplusplus.com/reference/istream/istream/istream/
https://www.cplusplus.com/reference/ostream/ostream/ostream/

It's complaining because you don't provide a proper construction for these two members.
You have to explicitly initialise them to something within the constructor of your class.

May 11, 2020 at 10:26am
please tell me how do I initialize in my program.
May 11, 2020 at 11:41am
You initialize data members using an initializer list:

https://en.cppreference.com/w/cpp/language/initializer_list
Topic archived. No new replies allowed.