-2

I am very new to C++ so this may be very simple. How do I call a method in the same class. My friend told me I need a constructor. Code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    disMessage("");
    return 0;
}

edit: How do I just call the method without a new class/ contructors

void disMessage(string a)
{
    cout << "\n" << a << endl;
}
Mackenzie
  • 31
  • 1
  • 6
  • You need a **class** type before either a "constructor" or "method" even become part of the conversation. what you're doing here requires none of the above. A *function* will suffice. – WhozCraig Sep 17 '13 at 18:09
  • There is no class in that code. What are you trying to do? – Beta Sep 17 '13 at 18:09
  • 2
    You don't need a constructor or class to simply call a function. You just call it. – bcr Sep 17 '13 at 18:10
  • C++ has functions, not methods. Functions can be member functions or free functions, and member functions can be static or non-static. What are you trying to do? Only non-static member functions require you to first construct a class instance. – Ben Voigt Sep 17 '13 at 18:10
  • before all this, try reading a good book on C++ – Saksham Sep 17 '13 at 18:11

4 Answers4

4

I guess you mean, how do I call a function (not method) that's defined later in the same file (not class). You're not defining any classes here, so there's no need to worry about constructors. (Perhaps you're used to languages that force you to wrap all the code up in classes. Welcome to C++: there's none of that nonsense here.)

You need to declare a function before you call it, so either do that:

void disMessage(string);   // declaration

int main() {
    disMessage("safas");   // use (after declaration)
    return 0;
}

void disMessage(string a){ // definition
    cout << "\n" << a << endl;
}

or move the function definition to before the point of use: a definition is also declaration.

void disMessage(string a){ // definition and declaration
    cout << "\n" << a << endl;
}

int main() {
    disMessage("safas");   // use (after declaration)
    return 0;
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Good point, some Java programmers may be confused about the difference between a class and a source file. – Ben Voigt Sep 17 '13 at 18:13
  • Sorry I am used to Java not C++ syntax also why the downvotes? – Mackenzie Sep 17 '13 at 18:14
  • @MackenzieGoodwin: I guess some people downvote questions they think are too basic. You'll probably find it more useful to work through a [good introductory book](http://stackoverflow.com/questions/388242), and ask questions about specific points you don't understand. – Mike Seymour Sep 17 '13 at 18:19
  • @MackenzieGoodwin : As you are familiar with java, in the question add a java code which solves what you are trying to do in c++. This makes it easy for others to understand what you need. –  Sep 17 '13 at 18:24
1

Maybe this example will help you to understand how to call methods from inside and outside the class.

class foo
{
public:
    void disMessage(string a)
    {
        cout << "\n" << a << endl;
    }

    void bar()
    {
        disMessage("foo::bar");
    }
};

int main() {
    foo f;

    f.disMessage("main");
    f.bar();

    return 0;
}
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

For a constructor you need both a class and an instantiated object:

class disMessage {
  public:
  disMessage(string a) {
    cout << "\n" << a << endl;
  }
};

int main() {
  disMessage obj("safas");
  return 0;
}
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
votelessbubble
  • 788
  • 6
  • 19
0

Short answer? Move the disMessage(string a) method above the main() method like so:

void disMessage(string a){

    cout << "\n" << a << endl;

}

int main() {

    disMessage("safas");

    return 0;

}

This is due to the type of code you are writing (more about this below) which means the compiler needs to find your functions before you can call them.

However, what you wrote here is not object oriented code (i.e. class based). It is actually just plain C code. You should read a bit about object oriented programming to understand this properly as this is a huge subject.

ytanay
  • 81
  • 3
  • Putting a declaration somewhere before the call would suffice ... – πάντα ῥεῖ Sep 17 '13 at 18:22
  • You are right, of course, but this answer wasn't my point. This is C style coding - not C++, as the question requested. If you want C, that's totally fine, but I think it's more important to show why the question is problematic than to just show how to fix it without saying why it's not a real fix. – ytanay Sep 17 '13 at 18:36
  • Hä? What has a function declaration to do with C style coding?? Function declarations (no matter if free function or class member) are ubiquitous in C++!!! – πάντα ῥεῖ Sep 17 '13 at 18:39
  • You misunderstand me - I'm not referring to your answer literally (which is totally correct), I'm talking about **this** specific sample of code and they way it's written. – ytanay Sep 17 '13 at 18:44
  • To be perfectly clear, I'm referring to the original question's (which mentioned things like constructors and classes) lack of any connection to the bit of code that was provided with it. – ytanay Sep 17 '13 at 18:50