1

Hello I have a simple program with a main.cpp an a.h and an a.cpp. I'd like to define a class in a.cpp and simply call on the method of the class in main.cpp

my a.h:

#include <iostream>
using namespace std;

class Hello 
{
 string hello();
};

my a.cpp

#include "a.h"
#include <iostream>

string class Hello::hello(){return "hello world";};

my main.cpp

#include "a.h"

 int main()
 {
  Hello h;
  cout << h.hello();


 }

EDIT : changed the include"a.cpp" to a.h and added the string to the definition of the method hello. added the #include <string> to a.h

while compiling I get the error

"a.cpp:4:22: error: ‘hello’ in ‘class Hello’ does not name a type string class Hello::hello() {return "Hello";};"

"a.cpp:4:28: error: expected unqualified-id before ‘)’ token string class Hello::hello() {return "Hello";};"

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Eduardo Diaz
  • 49
  • 11
  • 3
    Make it `string Hello::hello(){return "hello world";}` You also need `#include ` in the header. – Igor Tandetnik Jan 29 '16 at 04:03
  • 1
    Never put `using namespace ` into global scope in the header. It will affect all the files, which include the header. – mip Feb 03 '16 at 08:39

2 Answers2

1

A simple problem, you have declared a function as part of your class:

class Hello {
// ^Function is part of the class Hello.

string hello();
// ^Function returns a string
//      ^Function is called "hello"
//           ^Function takes no arguments.

So when you go to define the function you need to give the compiler the same information:

string Hello::hello() {
// ^Function returns a string
//      ^Function is part of the class Hello
//             ^Function is called "hello"
//                  ^Function takes no arguments.   
  • You will also need to add the header <string> to your file to facilitate the use of the string object.
  • Your '#include "a.cpp" needs to be #include "a.h", a rule of thumb is you should never ever see #include file.c/cpp.
  • Finally, you will need to make the function hello public to allow its use outside class members.

Here is a live example for you to play with.

But the best advice i can give, is pick up a c++ beginners book. It will do you a world of good. The Definitive C++ Book Guide and List

Community
  • 1
  • 1
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • i tried this solution but i keep getting the error : a.cpp:4:22: error: ‘hello’ in ‘class Hello’ does not name a type string class Hello::hello() {return "Hello";}; ^ a.cpp:4:28: error: expected unqualified-id before ‘)’ token string class Hello::hello() {return "Hello";}; during compilation i use g++ to compile – Eduardo Diaz Jan 29 '16 at 04:59
  • Read my answer again. You have not invested the time to read it properly. Have a look at the live example. – Fantastic Mr Fox Jan 29 '16 at 05:25
  • Solved thank you very much, sorry for not seeing it now its so clear – Eduardo Diaz Jan 29 '16 at 05:42
0
class Hello::hello(){return "hello world";};   // Replace it 
      // with
string Hello::hello(){return "hello world";};  // hello() has string return-type

Also, add string header-file into your file

surajs1n
  • 1,493
  • 6
  • 23
  • 34