object not dynamically allocating

when i try to allocate new memory of an object of structure type, it gives a "read access violation" error.

ftn that does that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Voter* create_voter_node(string line) {
    istringstream ss(line);

    string name, registered, constituency, password, vote_casted, cnic;
    ss >> name >> registered >> constituency >> cnic >> password >> vote_casted;

    Voter* temp = NULL;
    temp = new Voter;//gives an error here
    
    temp->name = name;
    temp->password = password;
    temp->already_registered = registered;
    temp->CNIC = cnic;
    temp->constituency_to_register_from = constituency;
    temp->vote_casted = vote_casted;
    return temp;

}
What error do you get ? Does you class / struct have a default constructor ?

BTW returning a pointer isn't good practice. Just return by value. If you have to return a pointer then us a std::unique_ptr. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-unique

Also passing a string by value isn't good practice either. Better to pass by const string& or even string&&
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Voter create_voter_node(const string& line) {
    istringstream ss(line);

    string name, registered, constituency, password, vote_casted, cnic;
    ss >> name >> registered >> constituency >> cnic >> password >> vote_casted;

    Voter temp;
    temp.name = name;
    temp.password = password;
    temp.already_registered = registered;
    temp.CNIC = cnic;
    temp.constituency_to_register_from = constituency;
    temp.vote_casted = vote_casted;

    return temp;
}
No there's no default comstructor.
And im creatimg a linked list so i do need a return address
Amd thanks for the string suggestion

And the error basiclay, at line no 8, gives that, there's a read access violation, and the prgram breaks, i guess its failing to make a new Voter object(at assigning memory)
Last edited on
The problem is probably with the constructor for Voter. If you post the code for Voter class, we'll have a look.
Need to see your Voter class. All of it - declarations and definitions. Better still, supply "complete" code.
voter structur:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Voter {
    string name;
    //a Voter can be registerd in only 1 constituency
    string already_registered = "false";

    string constituency_to_register_from = "";
    string CNIC = 0;
    string password;

    string vote_casted = "false";

    Voter* next = NULL;
};



the ftn im using to create a link list and return its adrress;
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

Voter* vo = NULL;
Voter* create_voter_LL() {
    ifstream A;
    A.open("voters.txt");
    int size = 0;
    string line;
    string* lines;
    
    while (getline(A, line)) { 
        size++;// to get total snumber of voters
    }

    lines = new (nothrow) string[size + 1];
    if (lines == NULL) {
        cerr << "lines memory allocation failed.\n";
    }

    A.clear();
    A.seekg(0, ios::beg);

    int it = 0;
    while (1) {

        getline(A, lines[it]);
        cout << lines[it] << endl;
        if (A.eof()) {
            break;
        }
        it++;
    }

    Voter* ptr = vo;
    //Voter* temp = NULL;
    
    it = 1;
    while (it < size) {

        if (vo == NULL) {
            //temp = create_voter_node(lines[it]);
            ptr = create_voter_node(lines[it]);
            vo = ptr;
        }
        else {
            //temp = create_voter_node(lines[it]);
            ptr->next = create_voter_node(lines[it]);
            ptr = ptr->next;
        }
        it++;
    }
    A.close();
    return vo;
}








What's Voter L7 supposed to do?

The file read can also be simplified to:

 
while (getline(A, lines[it++]));


Why not a std::vector for lines?
Theres no such thing as voter l7.
And im not supposed to use vectors
I assume @seeplus is asking about line 7 of the first snippet of code you posted, which reads
string CNIC = 0;
You should change that to
string CNIC;
Topic archived. No new replies allowed.