I am new to C++ and am writing a simple program to test out working with strings. I have defined a separate function that returns a string and want to call it within the main method. The same code works within the main method but I need to define it as a separate function.
My code is below:
7 #include <cstdlib>
8 #include <iostream>
9 #include <string>
10
11 using namespace std;
12
13 // function declaration
14 string hi();
15
16 int main(int argc, char** argv) {
17 // call method
18 string hi;
19 hi = hi();
20 cout << hi << endl;
21 }
22
23 string hi() {
24 //simple string function
25 return "Hello World";
26 }
And below is the error returned to the console when I try to compile with g++:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:19:13: error: no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string<char>}) ()’
hi = hi();
^
::hi().