2

HI.

How can I define a bool method in .h file and work with it in the cpp file? I have

my.h

#include <string>

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

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

}

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;
}

is not working.why?

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
linuxx
  • 1,487
  • 4
  • 16
  • 17
  • 2
    @dreamlax: philosophy is on another forum :) if you can't help, don't; if the question is really bad, downvote – sehe Apr 29 '11 at 09:36
  • I fixed the formatting for you and removed the Linux and Ubuntu references. They're not relevant. You need to specify what you mean by "is not working". – johnsyweb Apr 29 '11 at 09:36
  • 1
    A small note about terminology: What's known as a "method" in C# and Java is called a "member function" in C++. – Magnus Hoff Apr 29 '11 at 09:39

3 Answers3

8

I suggest you learn the basics of C++ from a tutorial

my.h

#include <string>

class me
{
    public:
       me();

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

my.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).

Numerous little fixes there. I get the sense that you are coming from C# (possibly java). Read up on the differences. Google has good sources :)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    The usual comment: Get a good [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (I recommend 2 and 4 from the beginners list to you), to learn C++ correctly. As you seem to already know some other (similar) language: Pay special attention to the differences in style and ideom used, do NOT try to write code as if you were still programming in that other language. – Fabio Fracassi Apr 29 '11 at 09:55
  • when do i have to pass a string though reference? what does that mean? could you explain a little? – linuxx Apr 29 '11 at 10:52
  • @linuxx: don't worry about that for the moment. – sehe Apr 29 '11 at 10:56
2

There are a number of issues with your code.

my.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

#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.

Answers to questions in the comments:

could you please tell me why do I need to pass std::string through reference?

This question has already been asked (more than once) on StackOverflow. I recommend this answer.

And what is with me mo?

In hindsight mo was a terrible choice for a variable name. instance_of_me may have been a better choice. This line constructs an instance of me, calling the constructor (which in turn calls method("earl"))

You meant me method("hello"); in the main()

I certainly did not!

You declared method as a private member function. This method cannot, therefore, be called from outside the class.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Johnsyweb could you please tell me why do I need to pass std::string through reference? And what is with me mo? You meant me method("hello"); in the main() – linuxx Apr 29 '11 at 10:38
  • @linuxx: I have updated my answer to address these comments. Hope that helps. Were you to compile it, you would see an error on calling `method("hello")`! – johnsyweb Apr 29 '11 at 21:59
1

First of all, you have missed : after private

Second, if method (String name) in the cpp file should be the method (String name) from your class, it must be:

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:

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:

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. It should be something like:

me::me()
{
    string name;
    // do something with name
    method( name ); // if "method" is a method, for real
}
Community
  • 1
  • 1
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187