当我调用方法c++时,对象正在恢复为Null
Object reverting to Null when I call methods c++
我目前正在为我的一个班级使用Rolodex。然而,我似乎在调用它的类方法时遇到了问题。每当我试图调用一个方法时,以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;
}
在我看来,您的getCurrent检查给您的结果与您想要的完全相反。
if(!myRolodex.getCurrent() != 0) { // make sure its not empty
当然应该是:
if(myRolodex.getCurrent() != 0) { // make sure its not empty
而在前部没有CCD_ 1。
相关文章:
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 如何在 c++ 中'NULL'字符串
- 用C++将哈希表写入文件并从文件中恢复
- c++使用foreach使数组为null
- 当使用通配符和null指针调用函数时,对输出的说明
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 如何在映射中返回null
- Opencv 恢复到比我设置的更高的分辨率
- 变量在使用赋值语句赋值后恢复为以前的值
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 为什么返回 NULL 不会破坏函数?
- 构造函数中的 QQuickItem 父项 null
- 检查字符串是否"null" C++
- 删除所有字符串后如何恢复 QStringList?
- fopen 在 gdb 中返回 NULL
- what(): basic_string::_M_construct null not valid
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 添加到链表的节点在函数后恢复为 NULL
- 当我调用方法c++时,对象正在恢复为Null