Witam. Probelm, który opiszę napotkałem w języku Pawn. Jako, że jest on prawie identyczny jak C++, napisałem podbny kod w tym języku i okazało się, że w nim problem także występuje.
Otóż (nie wiem czy dobrze to nazywam) chciałbym aby funkcja za pomocą referencji zwróciła mi dwie wartości, np.:
#include <iostream>
#include <string.h>
using namespace std;
void funkcja(int l1, int l2, int &suma, int &roznica)
{
suma = l1+l2;
roznica = l1-l2;
}
int main()
{
system("cls");
int sumka, rozni;
funkcja(3, 4, sumka, rozni);
cout<<"suma " <<sumka<<" roznica: " <<rozni;
system("PAUSE>NULL");
return 0;
}
Do tej pory, wszystko działa poprawnie. Jednak jeśli chcę dodać jeszcze jedną zmienną, która do której zostani wpisany string, pojawia się już problem.
#include <iostream>
#include <string.h>
using namespace std;
void funkcja(int l1, int l2, int &suma, int &roznica, string &jakistekst[30])
{
suma = l1+l2;
roznica = l1-l2;
jakistekst = "raz,dwa,trzy";
}
int main()
{
system("cls");
int sumka, rozni;
string tekst[30];
funkcja(3, 4, sumka, rozni, tekst);
cout<<"suma " <<sumka<<" roznica: " <<rozni<< " tekst: " << tekst;
system("PAUSE>NULL");
return 0;
}
Błędy:
7 C:\Dev-Cpp\Examples\Hello\Hello.cpp declaration of `jakistekst' as array of references
C:\Dev-Cpp\Examples\Hello\Hello.cpp In function `void funkcja(int, int, int&, int&)':
10 C:\Dev-Cpp\Examples\Hello\Hello.cpp `jakistekst' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
C:\Dev-Cpp\Examples\Hello\Hello.cpp In function `int main()':
7 C:\Dev-Cpp\Examples\Hello\Hello.cpp too many arguments to function `void funkcja(int, int, int&, int&)'
20 C:\Dev-Cpp\Examples\Hello\Hello.cpp at this point in file
Dodam jeszcze, że w języku Pawn, treść błędu to:
variable cannot be both a reference and an array (variable "dalszaczesc")