error using getline

1
2
            cout << "Enter your full name: ";
            getline(cin, name);


if write it in main it works, but i need it in a class and when i write it i get an error

E:\C++\Level 3\semana 3\act1.cpp|19|error: no matching function for call to 'getline(std::istream&, std::string [0])'|

how can i fix it ?
thanks

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

class Alumno
{
    private:
        int id;
        string name[];
        int grade;
    public:
        void setId()
        {

        }
        void setName()
        {
            cout << "Enter your full name: ";
            getline(cin, name);                     // error here

        }
        void setGrade()
        {

        }
        void getId()
        {

        }
        void getName()
        {

        }
        void getGrade()
        {

        }
};

int main()
{
    Alumno alumn[35];



    return 0;
}
Last edited on
The compiler correctly reports the error because you declared variable name as an array of strings

string name[];

and are trying to use this array instead of a single object of type std::string in function

getline(cin, name);

There is no such a function with name getline that accepts an array of strings

Moreover the compiler shall issue an error for the statement

string name[];

because it is incomplete type of a non-static data member
Last edited on
thanks vlad you are always so helpful ,just corrected it and now it's working :)

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

class Alumno
{
    private:
        int id;
        string name;    // removed [] cuz was not needed and that fixed it
        int grade;
    public:
        void setId()
        {

        }
        void setName()
        {
            cout << "Enter your full name: ";
            getline(cin, name);

        }
        void setGrade()
        {

        }
        void getId()
        {

        }
        void getName()
        {

        }
        void getGrade()
        {

        }
};

int main()
{
    Alumno alumn[35];



    return 0;
}
Last edited on
Topic archived. No new replies allowed.