C++ 私有(private)函数 - 不在此范围内错误

标签 c++ g++ private

我是 C++ 的新手,来自 Java 和 C。我的书没有提到私有(private)函数,谷歌搜索也没有出现太多。这对我来说应该是微不足道的,但我无法让它发挥作用。

我有这个代码:

#ifndef RUNDATABASE_H
#define RUNDATABASE_H
#include <iostream>
#include <string>

class RunDatabase
{
    public:
        int main();
    protected:
    private:
        bool checkIfProperID(std::string);
};

#endif // RUNDATABASE_H

在另一个文件中:

#include "RunDatabase.h"

int main()
{

    std::string id; // after this, I initialize id

    if(!checkIfProperID(id))
    {
        std::cout << "Improperly formatted student ID, must be numeric" << std::endl;
        break;
    }

}

bool RunDatabase::checkIfProperID(std::string id)
{
    return true;
}

我得到这个错误:error: 'checkIfProperID' was not declared in this scope

在 Windows 7 64 位上使用 MinGW g++ 4.4.1。

感谢您的帮助。

最佳答案

checkIfProperIDRunDatabase 的一个方法。这意味着您需要有一个 RunDatabase 对象才能调用 checkIfProperID

RunDatabase rd;
rd.checkIfProperID(id);

I don't see why the other function is not in the scope.

这里的“范围”是类。

RunDatabase::checkIfProperID

注意作用域解析运算符 ::。这意味着该方法属于类,而不是全局范围。

关于C++ 私有(private)函数 - 不在此范围内错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9594842/

相关文章:

c++ - 有人可以告诉我的代码有什么问题吗?

c++ - 如何将 std::string 转换为 LPCSTR?

c++ - 如何阻止 g++ 链接不需要的异常处理代码?

c++ - 我如何访问通过引用传递给 operator= 函数的对象的私有(private)数据?

c++ - c++中友元声明的内容错误基础

c++ - NULL 和 nullptr 比较

c++ - 如何开始和停止收集 gprof 的配置文件数据

c++ - Gcc/G++ 编译成 NIOS 2 程序集

c - 有没有办法在源文件之间共享 'struct' 而不是在头文件中公开它?

c++ - 读取 RSysLog tcp 消息