简体   繁体   English

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

[英]c++ .h and .cpp files - about methods

HI.你好。

How can I define a bool method in.h file and work with it in the cpp file?如何在.h 文件中定义 bool 方法并在 cpp 文件中使用它? I have我有

my.h我的.h

#include <string>

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

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

}

my.cpp我的.cpp

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

method (String name)
{
cout<<"name"<<endl;
}

is not working.why?不工作。为什么?

I suggest you learn the basics of C++ from a tutorial我建议你从教程中学习 C++ 的基础知识

my.h我的.h

#include <string>

class me
{
    public:
       me();

       bool method(std::string name) const;
};

my.cpp我的.cpp

#include 'my.h';

me::me()
{
}

bool me::method(std::string name)
{
    std::cout << name << std::endl;
}

As written, there is no need for me::method to be a member function (it could be a static).如所写,我::method 不需要成为成员 function (它可能是静态的)。

Numerous little fixes there.那里有许多小修复。 I get the sense that you are coming from C# (possibly java).我感觉您来自 C#(可能是 java)。 Read up on the differences.阅读差异。 Google has good sources:)谷歌有很好的资源:)

There are a number of issues with your code.您的代码存在许多问题。

my.h我的.h

#include <string>

// public class me; // Absolutely not needed. From which language did you get this?

class me
{
public:
me();

private: // You need the colon here. 

bool method(string name); //it is ok?? // No. The class is called std::string. You should pass it by const-reference (const std::string& name);

}

my.cpp我的.cpp

#include 'my.h';
me::me()
{
// `name` is undefined here. You also don't need to specify the type.
//method(string name); //can i do this? isn't there another alternative? 
    method("earl");
}

// method (String name) // See previous comment about const-reference and the name of the class. Also note that C++ is case-sensitive. You also need to specify the return type and the class name:
bool me::method(const std::string& name)
{
    // cout<<"name"<<endl; // Close...
    std::cout << "My name is " << name << std::endl;
    return true; // we are returning a `bool, right?
}

You'll also need to call your code:您还需要调用您的代码:

int main()
{
    me instance_of_me;
    return 0;
}

I suggest you take a look for a good C++ tutorial and some reading material .我建议你看看一个好的 C++ 教程一些阅读材料

Answers to questions in the comments:对评论中问题的回答:

could you please tell me why do I need to pass std::string through reference?你能告诉我为什么我需要通过引用传递 std::string 吗?

This question has already been asked (more than once) on StackOverflow.这个问题已经在 StackOverflow 上被问过(不止一次)。 I recommend this answer .我推荐这个答案

And what is with me mo?我怎么了?

In hindsight mo was a terrible choice for a variable name.事后看来, mo对于变量名来说是一个糟糕的选择。 instance_of_me may have been a better choice. instance_of_me可能是一个更好的选择。 This line constructs an instance of me , calling the constructor (which in turn calls method("earl") )这一行构造了一个me的实例,调用构造函数(依次调用method("earl")

You meant me method("hello");你的意思是我的方法(“你好”); in the main()在主()

I certainly did not!我当然没有!

You declared method as a private member function.您将method声明为private成员 function。 This method cannot, therefore, be called from outside the class.因此,不能从 class 外部调用此方法。

First of all, you have missed : after private首先,你错过了: private

Second, if method (String name) in the cpp file should be the method (String name) from your class, it must be:其次,如果 cpp 文件中的method (String name) method (String name)是 class 中的方法(字符串名称),它必须是:

bool me::method(std::string name)
{
    // ...
}

Third, if you want this bool me::method(std::string name) to be different function, a global one, not from you class, it must be:第三,如果你想让这个bool me::method(std::string name)不同 function,一个全局的,不是来自你 class,它必须是:

ret_type method(std::string name)
{
    // ...
}

And, fourth,第四,

cout<<"name"<<endl;

will pring the string (literal) "name".将打印字符串(文字)“名称”。 If you want to print the variable name , use it without the quotes:如果要打印变量name ,请使用不带引号的变量:

std::cout<< name <<endl;

I'd recommend you to get a book我建议你买一本


Ah, and this one:啊,还有这个:

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

method(string name) - this is not valid syntax. method(string name) - 这是无效的语法。 It should be something like:它应该是这样的:

me::me()
{
    string name;
    // do something with name
    method( name ); // if "method" is a method, for real
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM