variable was not declared in this scope

compiler give me this error in the while loop: _id,_text,_mapAns was not declared in this scope
I don't understand why!!


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 ANSWERCLASS_H
#define ANSWERCLASS_H

#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>

#include <iostream>
using namespace std;


class AnswerClass
{
    public:
        AnswerClass() {};
        AnswerClass(string id, string text);



        void setId(string id);
        string getId();
        void setText(string text);
        string getText();
        void readAnswer(string filename1);
        /*void stampa();*/




    private:
        string _id, _text;
        vector<string> _linkedQuest;
        map<string, string> _mapAns;

};

#endif // ANSWERCLASS_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
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
#include "AnswerClass.h"




AnswerClass::AnswerClass(string id, string text)
{
    this->_id=id;
    this->_text=text;
}

void AnswerClass::setId(string id)
{
    this->_id=id;
}

string AnswerClass::getId()
{
    return _id;
}

void AnswerClass::setText(string text)
{
    this->_text=text;
}

string AnswerClass::getText()
{
    return _text;
}

void readAnswer(string filename1)
{
    ifstream fin;
    string line,str(" ");
    istringstream is;


    fin.open(filename1.c_str());
    if (!fin.is_open())
    {
        cerr<<"Errore nell'apertura file risposte!"<<endl;
    }


    while(getline(fin,line))
    {
        //verificare presenza di uno spazio tra id e testo
        size_t found = line.find(str);
        if(found!= string::npos)
        {
            //se la stringa è conforme ai canoni sintattici, memorizzo in una mappa
            is.str(line);
            is>>_id>>_text;
            _mapAns[_id]=_text;

        }
        else
        {
            cout<<"Errore: file risposte non rispetta i canoni sintattici"<<endl;
            //bloccare il programma---> capire come fare;
        }
    }
}




Last edited on
The sope AnswerClass:: is missing in front of readAnswer(...). Currently it is a free function.
OMG! i'm a stupid!!!
thnak you and sorry for your time!!
No problem
About the habit of starting variable names with an underscore:

Brian W. Kernighan, Dennis M. Ritchie, The C programming Language second edition, 1988, Chapter 2.1 Variable Names:
Don’t begin variable names with underscore, however, since library routines often use such names


Bjarne Stroustrup, Programming: Principles and Practice Using C++, 2014, Chapter 3.7 names:
If you read system code or machine-generated code, you might see names starting with underscores, such as _foo. Never write those yourself; such names are reserved for implementation and system entities. By avoiding leading underscores, you will never find your names clashing with some name that the implementation generated.


http://en.cppreference.com/w/cpp/keyword:
Also, all identifiers that contain a double underscore __ in any position and each identifier that begins with an underscore followed by an uppercase letter is always reserved and all identifiers that begin with an underscore are reserved for use as names in the global namespace.

Topic archived. No new replies allowed.