|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
why the following code not compile
================ #include <iostream> using std::cout; // must need for using cout using std::endl; #include <string> using std::string; int main() { string stringa("abc"); string stringb("bbb"); cout << merge (stringa, stringb); } // end main string merge ( string a, string b){ string c = a + b; return c; } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
* Carmen Sei:
> why the following code not compile > > ================ > #include <iostream> > using std::cout; // must need for using cout > using std::endl; > > #include <string> > using std::string; > > int main() > { > string stringa("abc"); > string stringb("bbb"); > cout << merge (stringa, stringb); > > > } // end main > > string merge ( string a, string b){ > string c = a + b; > return c; > } Move the merge function to above main (before the first call to it). By the way, when passing class type objects as arguments, for efficiency one would usually pass them by reference, like string merge( string const& a, string const& b ) { return a + b; } Cheers, & hth., - Alf |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"Carmen Sei" <fatwallet951@yahoo.com> wrote in message > why the following code not compile > > ================ > #include <iostream> > using std::cout; // must need for using cout > using std::endl; > > #include <string> > using std::string; > > int main() > { > string stringa("abc"); > string stringb("bbb"); > cout << merge (stringa, stringb); > > > } // end main > > string merge ( string a, string b){ > string c = a + b; > return c; > } Compiler needs to know the signature/prototype of the functions that it comes across while compiling. We generally include header files for this purpose, like <stdio.h> for printf. In your case compiler doesn't know about the merge function, when it encounters it in main. So add the function prototype of merge before main, or move the definition before main. Sharad |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Sharad wrote:
> > Compiler needs to know the signature/prototype of the functions that it > comes across while compiling. In C++ the word you are looking for is "declaration". Prototype is a C term. Signature is something else. |
|
![]() |
| Outils de la discussion | |
|
|