Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改C+中私有基元变量的值+;_C++_Class_Private Members - Fatal编程技术网

C++ 更改C+中私有基元变量的值+;

C++ 更改C+中私有基元变量的值+;,c++,class,private-members,C++,Class,Private Members,numberOfElements的值保持为0 class A { private: int numberOfElements; public: A() : numberOfElements(0) {} void add() { numberOfElements++; } 这是在我的应用程序中增加变量的实际代码。类A只是我原来的类的一个示例 void PhoneBook::add(std::string name, std::st

numberOfElements的值保持为0

class A {
    private:
        int numberOfElements;

    public:
        A() : numberOfElements(0) {}
        void add() { numberOfElements++; }
这是在我的应用程序中增加变量的实际代码。类A只是我原来的类的一个示例

void PhoneBook::add(std::string name, std::string phoneNumber) {
entries[numberOfElements] = Entry(name, phoneNumber);
numberOfElements++;
}
std::string电话簿::查找(std::string名称){
对于(int i=0;istd::你能不能也给我们看一下测试你的声明的代码,也许这就是错误所在?如果没有@nos,就不会发生你要求的编辑?这些都是变量numberOfElements进入的位置。@MutatingAlgorithm否,那里没有调用add()的代码完全正确。我要的是让你声称add()函数没有递增
numberOfElements
的代码。例如,这很好:@nos fixed,book是一个电话簿对象。我为这么一个小错误向你道歉。谢谢。注意有关const引用的事情,如评论中所建议的。
std::string PhoneBook::find(std::string name) {
for(int i = 0; i < numberOfElements; ++i) {
    std::cout << i << std::endl;
    if(entries[i].getName().compare(name) == 0) {
        return entries[i].getPhoneNumber();
    }
    return "Name not found";
}
}
void PhoneBook::add(std::string name, std::string phoneNumber) {
entries[numberOfElements] = Entry(name, phoneNumber);
numberOfElements++;
}

std::ifstream in;
in.open("phonebook.txt");

while(in >> name && in >> phoneNumber) {
    book.add(name, phoneNumber);
}
std::string PhoneBook::find(std::string name) {
for(int i = 0; i < numberOfElements; ++i) {
    std::cout << i << std::endl;
    if(entries[i].getName().compare(name) == 0) {
        return entries[i].getPhoneNumber();
    }
    // end of if, still in the loop
    return "Name not found";
    // we never get past this, so the loop will
    // run no more than a single time
}
}
std::string PhoneBook::find(std::string const & name) {
    for(int i = 0; i < numberOfElements; ++i) {
        std::cout << i << std::endl;
        if(entries[i].getName().compare(name) == 0) {
            return entries[i].getPhoneNumber();
        }
    }
    return "Name not found";
}