我目前正在为我的一类人工作。但是,我似乎在调用它的类方法时遇到了问题。每当我尝试调用一个方法时,让我们以 getCurrent() 为例,它最终会传递一个空的 Rolodex 对象来调用该函数。当我调试它时,它说我当前的 Rolodex 对象将调用该方法,但是当我遵循该方法时,对象内部是空的。有什么建议么?
这是我正在查看的片段:
if(!myRolodex.getCurrent() != 0) { // make sure its not empty
string s1 = "Name", s2 = "Address", s3 = "Phone Number" ,
s4 = "E-Mail", s5 = "Return to Menu";
这是我的 Rolodex 类的头文件和源文件:
#include "Rolodex.h"
Rolodex::Rolodex() : current(0) {
}
Rolodex::Rolodex(const Rolodex& r) : current(r.current) {
for(int i = 0; i < MAX_LENGTH; i++)
rdex[i] = r.rdex[i];
}
RolodexEntry Rolodex::get(long index) const {
if(index < MAX_LENGTH && index >= 0)
return rdex[index];
else {
cout << "ERROR: Index is out of bounds." << endl;
RolodexEntry entry;
return entry; // returns empty entry showing that it was unsuccessful
}//endelse
}
bool Rolodex::set(long index, const RolodexEntry& entry) {
if(index < MAX_LENGTH && index >= 0) {
rdex[index] = entry;
}//endif
else
return false;
}
bool Rolodex::add(const RolodexEntry& entry) {
if(!isfull()) {
rdex[current] = entry;
current++;
return true;
}//endif
return false;
}
bool Rolodex::remove(long index) {
if(index < MAX_LENGTH && index >= 0) {
return rdex[index].erase();
}
}
void Rolodex::print_all(bool xml) {
for(int i = 0; i < current; i++) // cycle through all created entries
if(!rdex[i].iserased()) // make sure it only prints valid entries
rdex[i].print_entry(xml); // print entry
}
RolodexEntry Rolodex::findEntry(string entry, long type) {
RolodexEntry anEntry; // empty entry
if(getCurrent() != 0) {
string str;
switch (type) { // pick how to search entry
case 1: // name
for(int i = 0; i < current; i++) {
anEntry = get(i); // get current entry
// string to represent full name
str = anEntry.get_fname() + " " + anEntry.get_lname();
if(findstr(str, entry) != -1) {
anEntry.print_entry(false);
cout << "Do you want this contact, or to continue searching?";
char input;
cin >> input;
input = tolower(input);
cin.ignore();
if(input == 'y')
return anEntry;
} //endif
}//endfor
break;
case 2: // address
for(int i = 0; i < current; i++) {
anEntry = get(i); // get current entry
// string to represent full name
str = anEntry.get_street() + " " + anEntry.get_town()
+ anEntry.get_state() + " " + anEntry.get_zip();
if(findstr(str, entry) != -1) {
anEntry.print_entry(false);
cout << "Do you want this contact, or to continue searching?";
char input;
cin >> input;
input = tolower(input);
cin.ignore();
if(input == 'y')
return anEntry;
}//endif
}//endfor
break;
case 3: // phone
for(int i = 0; i < current; i++) {
anEntry = get(i); // get current entry
// string to represent full name
str = anEntry.get_phone();
if(findstr(str, entry) != -1) {
anEntry.print_entry(false);
cout << "Do you want this contact, or to continue searching?";
char input;
cin >> input;
input = tolower(input);
cin.ignore();
if(input == 'y')
return anEntry;
}//endif
}//endfor
break;
case 4: //email
for(int i = 0; i < current; i++) {
anEntry = get(i); // get current entry
// string to represent full name
str = anEntry.get_email();
if(findstr(str, entry) != -1) {
anEntry.print_entry(false);
cout << "Do you want this contact, or to continue searching?";
char input;
cin >> input;
input = tolower(input);
cin.ignore();
if(input == 'y')
return anEntry;
}//endif
}//endfor
break;
default:
cout << "Error: Invalid search choice." << endl;
break;
}; // endswitch
}//end if
else
cout << "Rolodex is empty" << endl;
return anEntry;
}
int Rolodex::findstr(string str, string sub) {
//turn string to lower case
transform(str.begin(), str.end(), str.begin(), tolower);
transform(sub.begin(), sub.end(), sub.begin(), tolower);
size_t found = str.find(sub);
if(found != string::npos)
return (int)found; // return index of where it starts
else
return -1;
}
这是我的 Rolodex 标题
#ifndef ROLODEX_H
#define ROLODEX_H
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "RolodexEntry.h"
using namespace std;
const long MAX_LENGTH = 20;
class Rolodex {
private:
RolodexEntry rdex[MAX_LENGTH];
long current;
int findstr(string str, string sub);
public:
Rolodex();
Rolodex(const Rolodex& r);
bool isfull() const {return current == MAX_LENGTH;}
long getCurrent() const {return current;};
RolodexEntry get(long index) const;
bool set(long index, const RolodexEntry& entry);
bool add(const RolodexEntry& entry);
bool remove(long index);
void print_all(bool xml);
bool isdeleted();
RolodexEntry findEntry(string entry, long type);
};
#endif
这是我正在处理的驱动程序的源文件:
#include "Rolodex.h"
#include "RolodexEntry.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static void menu();
static void add();
static void edit();
static void removeEntry();
static void find();
static void print();
static void setInfo(RolodexEntry& r);
static void editEntry(RolodexEntry& r);
Rolodex myRolodex;
int main() {
menu();
}
static void menu() {
bool end = false;
string test1 = "Add Entry",
test2 = "Edit Entry",
test3 = "Remove Entry",
test4 = "Find Entry",
test5 = "Print All Entries",
test6 = "Quit";
while(!end) {
// print menu options
cout << setw(5) << right << "1) " << test1 << endl;
cout << setw(5) << right << "2) " << test2 << endl;
cout << setw(5) << right << "3) " << test3 << endl;
cout << setw(5) << right << "4) " << test4 << endl;
cout << setw(5) << right << "5) " << test5 << endl;
cout << setw(5) << right << "6) " << test6 << endl;
// receive user input
string input;
cout << "\n> ";
getline(cin, input);
cout << endl;
// menu options
if(input == "1" || input == "A" || input == "a" || input == test1)
add();
else if(input == "2" || input == "E" || input == "e" || input == test2)
edit();
else if(input == "3" || input == "R" || input == "r" || input == test3)
removeEntry();
else if(input == "4" || input == "F" || input == "f" || input == test4)
find();
else if(input == "5" || input == "P" || input == "p" || input == test5)
print();
else if(input == "6" || input == "Q" || input == "q" || input == test6)
end = true;
else
cout << "Error: Invalid input." << endl;
}//endwhile
}
static void add() {
string string1 = "Add", string2 = "Return to Previous Menu";
cout << "1)" << string1 << endl;
cout << "2)" << string2 << endl;
cout << "\n> ";
string choice;
getline(cin, choice);
if(choice == "2"||choice == "R"|| choice == "r" || choice == string2)
return; //user chose to return so quit
if(!myRolodex.isfull()) { // make sure rolodex is not full
bool correct = false;
RolodexEntry anEntry; // create entry
while(!correct) { // makes sure user has the correct entry before its added
// set all data for entry
setInfo(anEntry);
// print entry for user to authenticate
cout << "\nIs this correct?" << endl;
anEntry.print_entry(false);
// take user input and determine whether or not to add entry
cout << "\n>";
char input;
cin >> input;
input = tolower(input);
if(input == 'y') {
myRolodex.add(anEntry); // add entry
correct = true;
}//endif
else {
cout << "Would you like to redo this entry?";
cin >> input;
input = tolower(input);
if(input != 'y') {
correct = true;
cout << endl;
}//endif
}//endelse
cin.ignore();
}//endwhile
}//endif
else { //rolodex is full, ask to overwrite
cout << "Rolodex is full...Would you like to overwrite an existing entry? ";
char over;
cin >> over;
tolower(over);
cin.ignore();
if(over == 'y') { // overwrite (similar to find but it doesn't print
string s1 = "Name", s2 = "Address", s3 = "Phone Number" ,
s4 = "E-Mail", s5 = "Return to Menu";
string input, ifin;
cout << "How would you like to search?" << endl << "1) " << s1 << endl;
cout << "2) " << s2 << endl;
cout << "3) " << s3 << endl;
cout << "4) " << s4 << endl;
cout << "5) " << s5 << endl;
cout << "\n> ";
RolodexEntry anEntry;
getline(cin, input);
/* match input with option
print
get input
find specified entry
set info for entry
*/
if(input == "1" || input == "N" || input == "n" || input == s1) {
cout << "Enter first or last name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 1);
setInfo(anEntry);
}
if(input == "2" || input == "A" || input == "a" || input == s2) {
cout << "Enter street name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 2);
setInfo(anEntry);
}
if(input == "3" || input == "P" || input == "p" || input == s3) {
cout << "Enter phone number: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 3);
setInfo(anEntry);
}
if(input == "4" || input == "E" || input == "e" || input == s4) {
cout << "Enter email: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 4);
setInfo(anEntry);
}
if(input == "5" || input == "R" || input == "r" || input == s5)
return;
}
}
}
static void edit() {
if(!myRolodex.getCurrent() != 0) { // make sure its not empty
string s1 = "Name", s2 = "Address", s3 = "Phone Number" ,
s4 = "E-Mail", s5 = "Return to Menu";
cout << "Find entry by..." << endl;
cout << setw(5) << right << "1) " << s1 << endl;
cout << setw(5) << right << "2) " << s2 << endl;
cout << setw(5) << right << "3) " << s3 << endl;
cout << setw(5) << right << "4) " << s4 << endl;
cout << setw(5) << right << "5) " << s5 << endl;
string input, ifin;
cout << "\n> ";
RolodexEntry anEntry;
getline(cin, input);
/* match input with option
print
get input
find specified entry
edit info for entry
*/
if(input == "1" || input == "N" || input == "n" || input == s1) {
cout << "Enter first or last name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 1);
editEntry(anEntry);
}
if(input == "2" || input == "A" || input == "a" || input == s2) {
cout << "Enter street name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 2);
editEntry(anEntry);
}
if(input == "3" || input == "P" || input == "p" || input == s3) {
cout << "Enter phone number: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 3);
editEntry(anEntry);
}
if(input == "4" || input == "E" || input == "e" || input == s4) {
cout << "Enter email: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 4);
editEntry(anEntry);
}
if(input == "5" || input == "R" || input == "r" || input == s5)
return;
}//endif
else
cout << "Rolodex is empty" << endl;
}
static void removeEntry() {
string s1 = "Name", s2 = "Address", s3 = "Phone Number" ,
s4 = "E-Mail", s5 = "Return to Menu";
cout << "Find Entry By..." << endl;
cout << setw(5) << right << "1) " << s1 << endl;
cout << setw(5) << right << "2) " << s2 << endl;
cout << setw(5) << right << "3) " << s3 << endl;
cout << setw(5) << right << "4) " << s4 << endl;
cout << setw(5) << right << "5) " << s5 << endl;
string input, ifin;
cout << "\n> ";
RolodexEntry anEntry;
getline(cin, input);
/* match input with option
print
get input
find specified entry
remove entry
*/
if(input == "1" || input == "N" || input == "n" || input == s1) {
cout << "Enter first or last name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 1);
anEntry.erase();
}
if(input == "2" || input == "A" || input == "a" || input == s2) {
cout << "Enter street name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 2);
anEntry.erase();
}
if(input == "3" || input == "P" || input == "p" || input == s3) {
cout << "Enter phone number: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 3);
anEntry.erase();
}
if(input == "4" || input == "E" || input == "e" || input == s4) {
cout << "Enter email: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 4);
anEntry.erase();
}
if(input == "5" || input == "R" || input == "r" || input == s5)
return;
}
static void find() {
if(!myRolodex.getCurrent() != 0) { // not emtpy
string s1 = "Name", s2 = "Address", s3 = "Phone Number" ,
s4 = "E-Mail", s5 = "Return to Menu";
cout << "How would you like to search?" << endl << "1) " << s1 << endl;
cout << "2) " << s2 << endl;
cout << "3) " << s3 << endl;
cout << "4) " << s4 << endl;
cout << "5) " << s5 << endl;
string input, ifin;
cout << "\n> ";
RolodexEntry anEntry;
getline(cin, input);
/* match input with option
print
get input
find specified entry
print entry
*/
if(input == "1" || input == "N" || input == "n" || input == s1) {
cout << "Enter first or last name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 1);
anEntry.print_entry(false);
}
if(input == "2" || input == "A" || input == "a" || input == s2) {
cout << "Enter street name only: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 2);
anEntry.print_entry(false);
}
if(input == "3" || input == "P" || input == "p" || input == s3) {
cout << "Enter phone number: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 3);
anEntry.print_entry(false);
}
if(input == "4" || input == "E" || input == "e" || input == s4) {
cout << "Enter email: ";
getline(cin, ifin);
anEntry = myRolodex.findEntry(ifin, 4);
anEntry.print_entry(false);
}
if(input == "5" || input == "R" || input == "r" || input == s5)
return;
}//endif
}
static void print() {
if(!myRolodex.getCurrent() != 0) { //check if empty
string c1 = "Print", c2 = "Print XML Tags", c3 = "Return to menu";
// print menu options
cout << setw(5) << right << "1) " << c1 << endl;
cout << setw(5) << right << "2) " << c2 << endl;
cout << setw(5) << right << "3) " << c3 << endl;
// receive user input
string input;
cout << "\n> ";
getline(cin, input);
cout << endl;
// menu options
if(input == "1" || input == "P" || input == "p" || input == c1)
myRolodex.print_all(false);
else if(input == "2" || input == "X" || input == "x" || input == c2)
myRolodex.print_all(true);
else if(input == "3" || input == "R" || input == "r" || input == c3)
return;
else
cout << "Error: Invalid input." << endl;
}//endif
else
cout << "Rolodex is empty" << endl;
}
static void setInfo(RolodexEntry& r) {
string fname,
lname,
street,
town,
state,
phone,
email,
zip;
cout << "First Name: ";
getline(cin, fname);
r.set_fname(fname);
cout << "Last Name: ";
getline(cin, lname);
r.set_lname(lname);
cout << "Street Name: ";
getline(cin, street);
r.set_street(street);
cout << "Town Name: ";
getline(cin, town);
r.set_town(town);
cout << "State Name: ";
getline(cin, state);
r.set_state(state);
cout << "Zip-Code: ";
getline(cin, zip);
r.set_zip(zip);
cout << "Phone#: ";
getline(cin, phone);
r.set_phone(phone);
cout << "E-Mail: ";
getline(cin, email);
r.set_email(email);
cout << endl;
}
static void editEntry(RolodexEntry& r) {
string c1 = "First Name", c2 = "Last Name",
c3 = "Street", c4 = "Town", c5 = "State",
c6 = "Phone", c7 = "Email", c8 = "Zip", c9 = "Return to Menu";
// print menu options
cout << setw(5) << right << "1) " << c1 << endl;
cout << setw(5) << right << "2) " << c2 << endl;
cout << setw(5) << right << "3) " << c3 << endl;
cout << setw(5) << right << "4) " << c4 << endl;
cout << setw(5) << right << "5) " << c5 << endl;
cout << setw(5) << right << "6) " << c6 << endl;
cout << setw(5) << right << "7) " << c7 << endl;
cout << setw(5) << right << "8) " << c8 << endl;
cout << setw(5) << right << "9) " << c9 << endl;
// receive user input
string input;
cout << "\n> ";
getline(cin, input);
cout << endl;
string data;
// menu options
// get data
// set data
if(input == "1" || input == "F" || input == "f" || input == c1) {
cout << "New First Name: ";
getline(cin, data);
r.set_fname(data);
}//endif
else if(input == "2" || input == "L" || input == "l" || input == c2){
cout << "New Last Name: ";
getline(cin, data);
r.set_lname(data);
}//endif
else if(input == "3" || input == "Str" || input == "str" || input == c3){
cout << "New Street: ";
getline(cin, data);
r.set_street(data);
}//endif
else if(input == "4" || input == "T" || input == "t" || input == c4){
cout << "New Town: ";
getline(cin, data);
r.set_town(data);
}//endif
else if(input == "5" || input == "Sta" || input == "sta" || input == c5){
cout << "New State: ";
getline(cin, data);
r.set_state(data);
}//endif
else if(input == "6" || input == "P" || input == "p" || input == c6){
cout << "New Phone Number: ";
getline(cin, data);
r.set_phone(data);
}//endif
else if(input == "7" || input == "E" || input == "e" || input == c7){
cout << "New E-Mail: ";
getline(cin, data);
r.set_email(data);
}//endif
else if(input == "8" || input == "Z" || input == "z" || input == c8){
cout << "New Zip-Code: ";
getline(cin, data);
r.set_email(data);
}//endif
else if(input == "9" || input == "R" || input == "r" || input == c9)
return;
else
cout << "Error: Invalid input." << endl;
}