vector class help

I'm trying to change my basic array to a vector. (in main.cpp) My vector is in line 10. After I changed it from a basic array to a vector, I am now getting errors, for example, 'unable to resolve identifier setRecNum' errors at every book[I].set line. (lines 40, 44, 48, 52, 56, 66, 69, 72, 75, and 78) and I do not know how to code those lines so as to populate the vector with input.

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

#ifndef RECORD_H
#define RECORD_H

class Record
{
private:
    string recNum;
    string firstName;
    string lastName;
    string age;
    string phoneNum;
public:
    Record()
    {
    }
    
    Record(string recIn) :recNum(recIn)
    {
    }
    
    Record(string recIn, string fnameIn, string lnameIn, string ageIn, string phoneIn)
            :recNum(recIn), firstName(fnameIn), lastName(lnameIn), age(ageIn), phoneNum(phoneIn)
    {
    }
    //set functions
    void setRecNum(string recIn);
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setAge(string ageIn);
    void setTelephone(string phoneIn);
    //get functions
    string getRecNum();
    string getFirstName();
    string getLastName();
    string getAge();
    string getTelephone();
};

#endif /* RECORD_H */  



Record.cpp
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
#include "Record.h"
#include <string>
using namespace std;

void Record::setRecNum(string recIn)
{
    recNum = recIn;
}

void Record::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Record::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Record::setAge(string ageIn)
{
    age = ageIn;
}

void Record::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

string Record::getRecNum()
{
return recNum;
}

string Record::getFirstName()
{
return firstName;
}

string Record::getLastName()
{
return lastName;
}

string Record::getAge()
{
return age;
}

string Record::getTelephone()
{
return phoneNum;
}



main.cpp
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "Record.h"
using namespace std;

class Recordsbook
{
    vector<string> Record book;
public:
    void Menu()
    {
        int choice = 0;
        cout << "---Main Menu---\n";
        cout << "1. Input information\n";
        cout << "2. Display records\n";
        cout << "3. Exit\n";
        cin >> choice;
        switch (choice)
        {
            case 1:
                InputInfo();
                break;
            case 2:
                Display();
                break;
            case 3:
                Exit();
                break;
        }
    }
    void InputInfo()
    {
        for(int i = 0; i<10; i++)
            {
                cout << "What is the record number? ";
                string r = "";
                cin >> r;
                book[i].setRecNum(r);
                cout << "What is the first name? ";
                string f = "";
                cin >> f;
                book[i].setFirstName(f);
                cout << "What is the last name? ";
                string l = "";
                cin >> l;
                book[i].setLastName(l);
                cout << "What is the age? ";
                string a = "";
                cin >> a;
                book[i].setAge(a);
                cout << "What is the telephone number? ";
                string t = "";
                cin >> t;
                book[i].setTelephone(t);
            }
        Menu();
    };
    void Display()
    {
        int i = 0;
        for(i=0; i<10; i++)
            {
                cout<<"Record number: ";
                cout<<book[i].getRecNum();
                cout<<"\n";
                cout<< "First name: ";
                cout<<book[i].getFirstName();
                cout<<"\n";
                cout<< "Last name: ";
                cout<<book[i].getLastName();
                cout<<"\n";
                cout<< "Age: ";
                cout<<book[i].getAge();
                cout<<"\n";
                cout<< "Telephone number: ";
                cout<<book[i].getTelephone();
                cout<<"\n";
                cout<<"\n";
            }
        Menu();
    };
    void Exit()
    {
        cout << "Closing...";
    };
} Recordsbook;

int main()
{
    Recordsbook.Menu();
}

Last edited on
Why the global variable Recordsbook?

Why are you recursively calling Menu? Wouldn't a simple loop in Menu() be a better choice.

Do you realize that in C++ not everything must be in a class?

What is the significance of the magic number 10 in your functions? Do you realize that your vector is empty, and that you can't access elements that don't exist?

Please post your complete error message, all of them, exactly as they appear in your development environment.

jlb,
1. global variable Recordsbook? The only global variable, in my beginners perspective (so it is probably wrong) is the vector.
2. I'm a beginner and am still learning (and am also more comfortable with Java than C++.) That way may be more efficient, but the way I wrote it made sense to me and worked fine when I was using Record book[10] instead of vector.
3. Yes? I'm still learning, but the way I coded it made sense to me. I'm still trying to get used to using classes.
4. 10 is the number of records I want to input. Yes, it has nothing in it initially because I want to store the user inputs, that are in the for loop, into the vector.
5.
'Unable to resolve identifier setRecNum'
'Unable to resolve identifier setFirstName'
'Unable to resolve identifier setLastName'
'Unable to resolve identifier setAge'
'Unable to resolve identifier setTelephone'
'Unable to resolve identifier getRecNum'
'Unable to resolve identifier getFirstName'
'Unable to resolve identifier getLastName'
'Unable to resolve identifier getAge'
'Unable to resolve identifier getTelephone'
Last edited on
1. global variable Recordsbook? The only global variable, in my beginners perspective (so it is probably wrong) is the vector.

Look at this snippet:
1
2
3
4
class Recordsbook
{
...
} Recordsbook; 

See that last line? That is creating a global variable called Recordsbook.

2. I'm a beginner and am still learning (and am also more comfortable with Java than C++.) That way may be more efficient, but the way I wrote it made sense to me and worked fine when I was using Record book[10] instead of vector.
C++ is not the same as Java, in Java everything must be in a class. In C++ it is sometimes better not to have everything in a class, having non-class functions is quite normal and usually will simplify the programs.

3. Yes? I'm still learning, but the way I coded it made sense to me. I'm still trying to get used to using classes.

Using recursion usually adds a lot of unnecessary complications, use it only when absolutely necessary.

4. 10 is the number of records I want to input. Yes, it has nothing in it initially because I want to store the user inputs, that are in the for loop, into the vector.

Well you can't access elements of a vector that don't exist. You will need to insert an element into your vector before you try to use that element.

5.
'Unable to resolve identifier setRecNum'

This is not the complete error message. There should be several more pieces of information embedded within your compiler error messages, the line where the error occurs for one.

Oh. I have Recordsbook there because when I learned of classes, at the end of a class the object name of it would be there. To call to Menu in it like I am, I'd call Recordsbook.Menu(). That is what I learned, anyway.

I understand. I will experiment with that in future programs, and try to get accustomed to not needing everything in classes.

Would I write it as vector<string> Record book(10); then?

Also, now it is also returning an error 'error: 'book' was not declared in this scope', but only for the lines book[I].setRecNum(r); and book[I].getRecNum(); and also an error of 'error: expected ';' at end of member declaration' and 'error: 'book' does not name a type'

I go to the line where the errors are and that's all it says. When I compile it this is everything it prints out. There is no other information about the errors except what I already put.

I just keep getting more errors I don't know how to fix when I'm trying to fix one. I really want to just give up on it because of it. :/

cd 'C:\Users\Mouse\Documents\NetBeansProjects\CppApplication_1'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/CppApplication_1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/cppapplication_1.exe
make[2]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/CppApplication_1'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
main.cpp:10:20: error: expected ';' at end of member declaration
vector<string> Record book;
^~~~~~
main.cpp:10:27: error: 'book' does not name a type
vector<string> Record book;
^~~~
main.cpp: In member function 'void Recordsbook::InputInfo()':
main.cpp:40:17: error: 'book' was not declared in this scope
book[i].setRecNum(r);
^~~~
main.cpp: In member function 'void Recordsbook::Display()':
main.cpp:66:23: error: 'book' was not declared in this scope
cout<<book[i].getRecNum();
^~~~
make[2]: *** [nbproject/Makefile-Debug.mk:74: build/Debug/Cygwin-Windows/main.o] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/CppApplication_1'
make[1]: *** [nbproject/Makefile-Debug.mk:60: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/CppApplication_1'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

BUILD FAILED (exit value 2, total time: 3s)


and my code even though it didn't change. (main.cpp)
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "Record.h"
using namespace std;

class Recordsbook
{
    vector<string> Record book;
public:
    void Menu()
    {
        int choice = 0;
        cout << "---Main Menu---\n";
        cout << "1. Input information\n";
        cout << "2. Display records\n";
        cout << "3. Exit\n";
        cin >> choice;
        switch (choice)
        {
            case 1:
                InputInfo();
                break;
            case 2:
                Display();
                break;
            case 3:
                Exit();
                break;
        }
    }
    void InputInfo()
    {
        for(int i = 0; i<10; i++)
            {
                cout << "What is the record number? ";
                string r = "";
                cin >> r;
                book[i].setRecNum(r);
                cout << "What is the first name? ";
                string f = "";
                cin >> f;
                book[i].setFirstName(f);
                cout << "What is the last name? ";
                string l = "";
                cin >> l;
                book[i].setLastName(l);
                cout << "What is the age? ";
                string a = "";
                cin >> a;
                book[i].setAge(a);
                cout << "What is the telephone number? ";
                string t = "";
                cin >> t;
                book[i].setTelephone(t);
            }
        Menu();
    };
    void Display()
    {
        int i = 0;
        for(i=0; i<10; i++)
            {
                cout<<"Record number: ";
                cout<<book[i].getRecNum();
                cout<<"\n";
                cout<< "First name: ";
                cout<<book[i].getFirstName();
                cout<<"\n";
                cout<< "Last name: ";
                cout<<book[i].getLastName();
                cout<<"\n";
                cout<< "Age: ";
                cout<<book[i].getAge();
                cout<<"\n";
                cout<< "Telephone number: ";
                cout<<book[i].getTelephone();
                cout<<"\n";
                cout<<"\n";
            }
        Menu();
    };
    void Exit()
    {
        cout << "Closing...";
    };
} Recordsbook;

int main()
{
    Recordsbook.Menu();
}
Last edited on
There is no other information about the errors except what I already put.

What? Look at the compiler error messages you provided in your last post:
1
2
3
4
main.cpp
main.cpp:10:20: error: expected ';' at end of member declaration
vector<string> Record book;
^~~~~~

See those two numbers? The first is the line number of the error, the second is the location within that line and that last line should actually be pointing to the exact place. I believe that it should be pointing up at the end of "Record", in future post your error messages inside code tags to preserve formatting. What you probably want is something more like:

vector<Record> book;

Fixing this error will probably fix quite a few of the other errors.

Would I write it as vector<string> Record book(10); then?

Well you could provide a fixed size in the definition, but then it would make more sense to use a std::array rather than a std::vector. Another way would be to create a "temporary" Record and then push that temporary into your vector.

1
2
3
4
5
6
            for(int i = 0; i < 10; i++)
            {
                cout << "What is the record number? ";
                Record current;
                string temp;
                current.setRecNum(temp);

...
book.push_back(current);
}
[/code]

I would also think about using a named constant to replace that "magic number" 10 so that if you decide you want more or less "Records" you can just change that constant in one place.

And I'd also consider doing without that Recordsbook class and replace it with non-class functions and variables called from main(). And instead of that global variable make that variable local to main(), when you do you will probably run into some other problems that will need to be addressed.

Last edited on
Okay, I changed vector<string> Record book; to vector<Record> book; like you said. The output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd 'C:\Users\Mouse\Documents\NetBeansProjects\Mpierce_Mod2Asmt1_071617_2'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/mpierce_mod2asmt1_071617_2.exe
make[2]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
mkdir -p dist/Debug/Cygwin-Windows
g++     -o dist/Debug/Cygwin-Windows/mpierce_mod2asmt1_071617_2 build/Debug/Cygwin-Windows/Record.o build/Debug/Cygwin-Windows/main.o 
make[2]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
make[1]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'

BUILD SUCCESSFUL (total time: 5s)


1
2
3
4
5
6
7
8
1. Input information
2. Display records
3. Exit
1
What is the record number? 1

RUN FAILED (exit value 1, total time: 10s)

Still gives all of the errors I stated on every set and get function line.
Last edited on
Tried your other suggestion, and that fixed it. Thank you so much for the help, and I'm very sorry I am not very attuned to C++.
Topic archived. No new replies allowed.