Stránka 1 z 1

C++ string argumentem funkce - chyba

Napsal: sob 20. dub 2013, 13:56
od acerr
Ahoj, proč mi toto nejde přeložit? Či spíše jak to vyřeším abych mohl string použít jako argument funkce?

Kód: Vybrat vše

#include <iostream>
#include <string>
#include <cstring>
void strcount (const  string * str);
using namespace std;

int main()
{
   string input;
   string next;

   cout << "Zadejte radek textu:\n";
   getline(cin, input);
   while (cin)
   {
       getline(cin,next);
       while (next != "")
        getline(cin,next);
       strcount(input);
       cout << "Zadejte dalsi radek textu (prazdny radek na ukonceni): \n";
       getline(cin,input);
   }
   cout << "Sbohem\n";
   return 0;
}

void strcount(const string * str)
{
    static int total = 0;
    int count = 0;
    cout << "\"" << str << "\" obsahuje ";
    count = strlen(str);
    total +=count;
    cout << count << " znaku\n";
    cout << total << " znaku celkem\n";
}


Re: C++ string argumentem funkce - chyba

Napsal: sob 20. dub 2013, 18:23
od GrEEEne

Kód: Vybrat vše

void strcount(const string * str)
-->

Kód: Vybrat vše

void strcount(const string str)
(začal bych takhle, ale nijak podrobně jsem ten tvůj kód nestudoval)

Re: C++ string argumentem funkce - chyba

Napsal: sob 20. dub 2013, 20:51
od acerr
a proč nemohu předat jen pointer na string? Jako u pole ...

Re: C++ string argumentem funkce - chyba

Napsal: ned 21. dub 2013, 09:48
od GrEEEne
std::string v C++ je objekt, nikoliv pole charů jako v C. Zaměř se na nějaké referenční ukázky kódů v učebnicích apod.

Re: C++ string argumentem funkce - chyba

Napsal: ned 21. dub 2013, 10:27
od OndraSter
Taktéž se nevolá strlen(nejakystring), ale buď nejakystring.length nebo strlen(nejakystring.c_str()), pokud chceš použít klasickou C funkci.

Obvykle se používá void nejakametoda(const string& str).