c ++ .h和.cpp文件 - 关于方法

lin*_*uxx 2 c++

HI.

如何在.h文件中定义bool方法并在cpp文件中使用它?我有

my.h

#include <string>

public class me;
class me
{
public:
me();

private bool method(string name); //it is ok??

}
Run Code Online (Sandbox Code Playgroud)

my.cpp

#include 'my.h';
me::me()
{
method(string name); //can i do this? isn't there another alternative?
}

method (String name)
{
cout<<"name"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

不工作.为什么?

seh*_*ehe 8

我建议你从教程中学习C++的基础知识

my.h

#include <string>

class me
{
    public:
       me();

       bool method(std::string name) const;
};
Run Code Online (Sandbox Code Playgroud)

my.cpp

#include 'my.h';

me::me()
{
}

bool me::method(std::string name)
{
    std::cout << name << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如上所述,me :: method不需要成为成员函数(它可能是静态的).

那里有很多小修复.我觉得你来自C#(可能是java).阅读差异.谷歌有很好的来源:)