PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > string merge
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
string merge

Réponse
 
LinkBack Outils de la discussion
Vieux 04/04/2008, 00h48   #1
Carmen Sei
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut string merge

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;
}
  Réponse avec citation
Vieux 04/04/2008, 00h54   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string merge

* 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
  Réponse avec citation
Vieux 04/04/2008, 01h48   #3
Sharad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string merge


"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


  Réponse avec citation
Vieux 04/04/2008, 13h39   #4
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string merge

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09676 seconds with 12 queries