Error: sortLastNames was not declared in this scope.

Hi there,
I have to create a class named book which will read from a file containing the book info and store them into a book array. one of the tasks is to sort the array by author's last name and output the sorted file into another fstream object. I have used the simple bubble sort but I am getting an error something like this:
Error: sortLastNames was not declared in this scope.
YOU WILL SEE THE FUNCTION CALL THIRD LINE AFTER THE FIRST WHILE LOOP. THAT IS WHERE I AM GETTING THE ERROR. (LINE 61)

I have combined everything into one file.
Note: I am able to figure out everything else. It is just this error that giving me hard time.
Thank you in advance.
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class book
{
public:
    book();
    book(string catalog, string lname, string fname, string title, string genre, string available);
    //Accesor Functions
    string getCatalogNumber();
    string getLastName();
    string getFirstName();
    string getBookTitle();
    string getBookGenre();
    string getAvailability();
    //Mutator Functions
    void setCatalogNumber(string catalog);
    void setLastName(string lname);
    void setFirstName(string fname);
    void setBookTitle(string title);
    void setBookGenre(string genre);
    void setAvailability(string available);
    //other member functions
    void sortLastName(book array[], int length);
    void sortGenre(book array[], int length);

private:
    string catalogNumber,
               lastName,
               firstName,
               booktitle,
               bookgenre,
               availability;

};

int main(int argc, char* argv[])
{
    ifstream inputFile1, inputFile2;
    ofstream outputFile1, outputFile2, outputFile3;
    inputFile1.open("input1.txt");
    string catalog, lname, fname, title, genre, available; // placeholder for contents in inputFile1.
    int size = 0;
    book arr[100]; // creating a book array so as to store the objects we extract form the inputFile1.

    while(inputFile1 >> catalog >> lname >> fname >> title >> genre >> available)
    {
        arr[size].setCatalogNumber(catalog);
        arr[size].setLastName(lname);
        arr[size].setFirstName(fname);
        arr[size].setBookTitle(title);
        arr[size].setBookGenre(genre);
        arr[size].setAvailability(available);

        size++;
    }
    inputFile2.close();
    // the following code computes computational tasks 1 and 2.
    sortLastName(arr, size);
    outputFile1.open("output1.txt");
    for(int i = 0; i < size; i++)
    {
        outputFile1 << arr[i].getCatalogNumber() << " " << arr[i].getLastName()
                    << " " << arr[i].getFirstName() << " " << arr[i].getBookTitle()
                    << " " << arr[i].getBookGenre() << " " << arr[i].getAvailability() << endl;
    }
    outputFile1.close();

    sortGenre(arr,size);
    outputFile2.open("output2.txt");
    for(int i = 0; i < size; i++)
    {
        outputFile2 << arr[i].getCatalogNumber() << " " << arr[i].getLastName()
                    << " " << arr[i].getFirstName() << " " << arr[i].getBookTitle()
                    << " " << arr[i].getBookGenre() << " " << arr[i].getAvailability() << endl;
    }
    outputFile2.close();

    // the following code computes the computational task 3.
    inputFile2.open("input2.txt");
    outputFile3.open("output3.txt");
    string requestCatalog;
    bool found;
    while(inputFile2 >> requestCatalog)
    {
        for(int p = 0; p < size; p++)
        {
            if((arr[p].getCatalogNumber() == requestCatalog) && (arr[p].getAvailability() == "available"))
            {
                outputFile3 << requestCatalog << " " << arr[p].getBookTitle() << " available" << endl;
                found = true;
                break;
            }
            else if((arr[p].getCatalogNumber() == requestCatalog) && (arr[p].getAvailability() == "unavailable"))
            {
                outputFile3 << requestCatalog << " " << arr[p].getBookTitle() << " unavailable" << endl;
                found = true;
                break;
            }
            else
                found = false;
        }
        if(!found)
            outputFile3 << requestCatalog << " unavailable" << endl;
    }
    inputFile2.close();
    outputFile3.close();

    return 0;
}

book::book()
{
     string catalogNumber = "",
               lastName = "",
               firstName = "",
               booktitle = "",
               bookgenre = "";
               availability= "";
}

book :: book(string catalog, string fname, string lname, string title, string genre, string available)
{
    catalogNumber = catalog;
    lastName = lname;
    firstName = fname;
    booktitle = title;
    bookgenre = genre;
    availability = available;

}

string book :: getCatalogNumber()
{
    return catalogNumber;
}

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

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

string book :: getBookTitle()
{
    return booktitle;
}

string book :: getBookGenre()
{
    return bookgenre;
}

string book :: getAvailability()
{
    return availability;
}

void book :: setCatalogNumber(string catalog)
{
    catalogNumber = catalog;
}
void book :: setLastName(string lname)
{
    lastName = lname;
}
void book :: setFirstName(string fname)
{
    firstName = fname;
}
void book :: setBookTitle(string title)
{
    booktitle = title;
}
void book :: setBookGenre(string genre)
{
    bookgenre = genre;
}
void book :: setAvailability(string available)
{
    availability = available;
}

void book :: sortLastName(book array[], int length)
{
    for(int i = 0; i < length-1; i++)
    {
        for(int j = i+1; j < length; j++)
        {
            if(array[j].lastName < array[i].lastName)
            {
                string temp = array[i].lastName;
                array[i].lastName = array[j].lastName;
                array[j].lastName = temp;
            }
        }
    }
}

void book :: sortGenre(book array[], int length)
{
    for(int i = 0; i < length-1; i++)
    {
        for(int j = i+1; j < length; j++)
        {
            if(array[j].bookgenre < array[i].bookgenre)
            {
                string temp = array[i].bookgenre;
                array[i].bookgenre = array[j].bookgenre;
                array[j].bookgenre = temp;
            }
        }
    }

}
Last edited on
sortLastName is a member function, so you have to call it from an instance of book. By the way, why have you made a member function that takes a group of objects to operate on, rather than operating on the object of which it is a member?
I see what you mean. But, let me just make sure that I have understood or not.
So, it will be like : bookObject.sortLastName(); ????
By the way, thank you for the reply.
Any member function has to be called like you said. I would advise that you stop it being a member function as it is not related to the particular instance, as a rule, any function where you have to pass books into the function so as to operate on them, should not be a member function of book, unless it needs access to private/protected variables.
I see, let me define it the way you said then.
It compiles now, but when I checked the output files, the list is not sorted.
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class book
{
public:
    book();
    book(string catalog, string lname, string fname, string title, string genre, string available);
    //Accesor Functions
    string getCatalogNumber();
    string getLastName();
    string getFirstName();
    string getBookTitle();
    string getBookGenre();
    string getAvailability();
    //Mutator Functions
    void setCatalogNumber(string catalog);
    void setLastName(string lname);
    void setFirstName(string fname);
    void setBookTitle(string title);
    void setBookGenre(string genre);
    void setAvailability(string available);

private:
    string catalogNumber,
               lastName,
               firstName,
               booktitle,
               bookgenre,
               availability;

};

void sortLastName(book array[], int length);
void sortGenre(book array[], int length);


int main(int argc, char* argv[])
{
    ifstream inputFile1, inputFile2;
    ofstream outputFile1, outputFile2, outputFile3;
    inputFile1.open("input1.txt");
    string catalog, lname, fname, title, genre, available; // placeholder for contents in inputFile1.
    int size = 0;
    book arr[100]; // creating a book array so as to store the objects we extract form the inputFile1.

    while(inputFile1 >> catalog >> lname >> fname >> title >> genre >> available)
    {
        arr[size].setCatalogNumber(catalog);
        arr[size].setLastName(lname);
        arr[size].setFirstName(fname);
        arr[size].setBookTitle(title);
        arr[size].setBookGenre(genre);
        arr[size].setAvailability(available);

        size++;
    }
    inputFile2.close();
    // the following code computes computational tasks 1 and 2.
    sortLastName(arr, size);
    outputFile1.open("output1.txt");
    for(int i = 0; i < size; i++)
    {
        outputFile1 << arr[i].getCatalogNumber() << " " << arr[i].getLastName()
                    << " " << arr[i].getFirstName() << " " << arr[i].getBookTitle()
                    << " " << arr[i].getBookGenre() << " " << arr[i].getAvailability() << endl;
    }
    outputFile1.close();

    sortGenre(arr,size);
    outputFile2.open("output2.txt");
    for(int i = 0; i < size; i++)
    {
        outputFile2 << arr[i].getCatalogNumber() << " " << arr[i].getLastName()
                    << " " << arr[i].getFirstName() << " " << arr[i].getBookTitle()
                    << " " << arr[i].getBookGenre() << " " << arr[i].getAvailability() << endl;
    }
    outputFile2.close();

    // the following code computes the computational task 3.
    inputFile2.open("input2.txt");
    outputFile3.open("output3.txt");
    string requestCatalog;
    bool found;
    while(inputFile2 >> requestCatalog)
    {
        for(int p = 0; p < size; p++)
        {
            if((arr[p].getCatalogNumber() == requestCatalog) && (arr[p].getAvailability() == "available"))
            {
                outputFile3 << requestCatalog << " " << arr[p].getBookTitle() << " available" << endl;
                found = true;
                break;
            }
            else if((arr[p].getCatalogNumber() == requestCatalog) && (arr[p].getAvailability() == "unavailable"))
            {
                outputFile3 << requestCatalog << " " << arr[p].getBookTitle() << " unavailable" << endl;
                found = true;
                break;
            }
            else
                found = false;
        }
        if(!found)
            outputFile3 << requestCatalog << " unavailable" << endl;
    }
    inputFile2.close();
    outputFile3.close();

    return 0;
}

book::book()
{
     string catalogNumber = "",
               lastName = "",
               firstName = "",
               booktitle = "",
               bookgenre = "";
               availability= "";
}

book :: book(string catalog, string fname, string lname, string title, string genre, string available)
{
    catalogNumber = catalog;
    lastName = lname;
    firstName = fname;
    booktitle = title;
    bookgenre = genre;
    availability = available;

}

string book :: getCatalogNumber()
{
    return catalogNumber;
}

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

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

string book :: getBookTitle()
{
    return booktitle;
}

string book :: getBookGenre()
{
    return bookgenre;
}

string book :: getAvailability()
{
    return availability;
}

void book :: setCatalogNumber(string catalog)
{
    catalogNumber = catalog;
}
void book :: setLastName(string lname)
{
    lastName = lname;
}
void book :: setFirstName(string fname)
{
    firstName = fname;
}
void book :: setBookTitle(string title)
{
    booktitle = title;
}
void book :: setBookGenre(string genre)
{
    bookgenre = genre;
}
void book :: setAvailability(string available)
{
    availability = available;
}

void sortLastName(book array[], int length)
{
    for(int i = 0; i < length-1; i++)
    {
        for(int j = i+1; j < length; j++)
        {
            if(array[j].getLastName() < array[i].getLastName())
            {
                string temp = array[i].getLastName();
                array[i].getLastName() = array[j].getLastName();
                array[j].getLastName() = temp;
            }
        }
    }
}

void sortGenre(book array[], int length)
{
    for(int i = 0; i < length-1; i++)
    {
        for(int j = i+1; j < length; j++)
        {
            if(array[j].getBookGenre() < array[i].getBookGenre())
            {
                string temp = array[i].getBookGenre();
                array[i].getBookGenre() = array[j].getBookGenre();
                array[j].getBookGenre() = temp;
            }
        }
    }

}


You are both passing and returning copies so you are not modifying anything permanent. Have a look at passing by reference and make setLastName instead of getLastName() =, because that is setting the value of a copy of lastName.
can you please provide an example of how the setLastName should look? And, I thought array is always passed by reference.
Thanks
No, I'm afraid you have to pass arrays by reference, it isn't automatic. book::setLastname(string newLastName) would look something like this:
1
2
3
4
void book::setLastName(string newLastName)
{
lastName = newLastName;
}

If I remember rightly, the syntax for passing arrays by reference is
 
void sortGenre(book (&array)[], int length)
it worked. I used the mutator functions as you said, but the array did need to pass by reference.
Now, I have run into a big problem. I have separated the header, implementation and main file and it is giving me bunch of errors and they are same:
undefined reference to book::book() and same for all other variables.
Topic archived. No new replies allowed.