C++;私有函数-不在此范围内错误 我对C++非常陌生,从java和C.开始,我的书没有提到私人功能,谷歌搜索也没有出现很多。这对我来说应该是微不足道的,但我不能让它工作

C++;私有函数-不在此范围内错误 我对C++非常陌生,从java和C.开始,我的书没有提到私人功能,谷歌搜索也没有出现很多。这对我来说应该是微不足道的,但我不能让它工作,c++,g++,private,C++,G++,Private,我有以下代码: #ifndef RUNDATABASE_H #define RUNDATABASE_H #include <iostream> #include <string> class RunDatabase { public: int main(); protected: private: bool checkIfProperID(std::string); }; #endif // RUNDATABAS

我有以下代码:

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

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

#endif // RUNDATABASE_H
#如果ndef运行数据库#
#定义运行数据库
#包括
#包括
类运行数据库
{
公众:
int main();
受保护的:
私人:
bool checkIfProperID(std::string);
};
#endif//RUNDATABASE\u 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;
}
#包括“RunDatabase.h”
int main()
{
std::string id;//之后,我初始化id
如果(!checkIfProperID(id))
{

std::cout
checkIfProperID
RunDatabase
的一种方法。这意味着您需要一个
RunDatabase
对象才能调用
checkIfProperID

RunDatabase rd;
rd.checkIfProperID(id);
我不明白为什么其他函数不在范围内

这里的“范围”是类

RunDatabase::checkIfProperID

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

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

RunDatabase rd;
rd.checkIfProperID(id);
我不明白为什么其他函数不在范围内

这里的“范围”是类

RunDatabase::checkIfProperID

注意范围解析操作符<代码>::/>代码>这意味着该方法属于类,而不是全局范围。

< P>与java不同,C++允许独立的函数。在运行程序时调用的主< /Cuff>函数是独立的,代码<主< /代码>,不是成员>代码>主< /代码>。因此,事情应该是这样的:

int main() {
    RunDatabase rdb;
    rdb.main();
}

RunDatabase::main() {
    // the code of the main function from your post
}

不同于java,C++允许独立的函数。运行程序时调用的<代码>主< /C>函数是独立的,代码<主> <代码>,而不是成员>代码>主< /代码>。如果修改CPP文件如下,则应该编译:

int main() {
    RunDatabase rdb;
    rdb.main();
}

RunDatabase::main() {
    // the code of the main function from your post
}

问题是,
main
没有作为
RunDatabase
的成员实现

int main()
{
应该是

int RunDatabase::main()
{
然后需要一个
main()
函数,程序将在该函数中开始执行

也不要考虑在开始执行的主函数之后命名类成员函数,以避免混淆。例如:

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

int RunDatabase::execute()
{

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

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

}

/// runs when the program starts
int main()
{
    RunDatabase runDatabase;
    runDatabase.execute();
}
类运行数据库
{
公众:
int execute();
受保护的:
私人:
bool checkIfProperID(std::string);
};
int RunDatabase::execute()
{
std::string id;//之后,我初始化id
如果(!checkIfProperID(id))
{ 

std::cout问题在于
main
没有作为
RunDatabase
的成员实现

int main()
{
应该是

int RunDatabase::main()
{
然后需要一个
main()
函数,程序将在该函数中开始执行

也不要考虑在开始执行的主函数之后命名类成员函数,以避免混淆。例如:

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

int RunDatabase::execute()
{

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

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

}

/// runs when the program starts
int main()
{
    RunDatabase runDatabase;
    runDatabase.execute();
}
类运行数据库
{
公众:
int execute();
受保护的:
私人:
bool checkIfProperID(std::string);
};
int RunDatabase::execute()
{
std::string id;//之后,我初始化id
如果(!checkIfProperID(id))
{ 

std::如果你来自Java,你不应该知道这一点吗?对不起。无论如何,答案会很清楚。@Marlon因为main与checkIfProperId在同一个类中,我不明白为什么其他函数不在范围内。你可以从main在Java中做同样的事情,但方法显然必须是静态的,或者你需要实例化一个新对象如果你的书没有提到私有函数,你真的应该放弃它,得到.@R.MartinhoFernandes“C++编程与设计模式揭示”Tomasz Muldner只提到了私有字段。但是这个错误是因为我现在知道主函数不是类的一部分。如果你来自Java,你不应该知道吗?对不起。无论如何,答案会很清楚。@Marlon因为主函数和checkIfProperId在同一个类中,我不明白为什么另一个函数是n你可以从main在Java中做同样的事情,但方法显然必须是静态的,或者你需要实例化同一类的新对象。如果你的书没有提到私有函数,你应该真的放弃它,得到.@R.MartinhoFernandes“C++编程与设计模式揭示”Tomasz Muldner只提到了私有字段。但是这个错误是因为我现在知道主函数不是类的一部分。所以main函数不是该类的一部分。@ SkyNo.No。在全局范围内声明C和C++中的入口点<代码>主< /代码>。您误解了:<代码> RunDeabas::main < /CO。DE >是入口点.<代码> RunDeabase::主< /代码>与主< <代码>不一样。我正在学习“C++设计模式,显示设计模式”的C++。Tomasz Muldner,而且没有提到这一点。谢谢!你可以看到我是如何困惑的,因为main是java中的一部分。在一秒钟内接受回答。所以main函数不是这个类的一部分。@ SkyNo.No。C++和C++中的入口点<代码> main < /C>在全局范围内声明。你有误解:<代码> RunDeabas::main <代码> >入口> .>代码> RunDeabase::主< /代码>与主< <代码>不一样。我正在学习“C++设计模式,显示设计模式”的C++。由Tomasz Muldner编写,没有提到这一点。谢谢!你可以看到我有多困惑,因为main是Java类的一部分。请稍后接受答案。对不起,我不能接受你的答案,因为有人已经给了我答案。但这是我将使用的东西。+1抱歉,我不能接受你的答案,因为有人已经给了我答案。但这是我会用的东西