Zwrócenie długości stringa do zmiennej.

0

Witam, jak zwrócić długość stringa do zmiennej?

Mam taką strukturę:

struct osoba {
	string imie;
	string nazwisko;
	int wiek;
	float skutecznosc;
	int wielkosc_imienia;
	int wielkosc_nazwiska;
	osoba *nast;
};

i chcę zwrócić długość imie do wielkosc_imienia:

(pier->imie).length()=pier->wielkosc_imienia;

program się nie kompiluje

error: lvalue required as left operand of assignment|
0

Ty próbujesz przypisać wielkosc_imienia do metody co nie ma sensu.

Wystarczy zamienić miejscami:

pier->wielkosc_imienia = pier->imie.length();
0

Warto zwrócić uwagę, co oznacza błąd "lvalue required as left operand of assignment". "lvalue", mówiąc skrótowo: coś, do czego można przypisywać: http://en.cppreference.com/w/cpp/language/value_category

A modifiable lvalue may be used as the left-hand operand of the built-in assignment and compound assignment operators.

W przeciwieństwie do "rvalue", do której nie można przypisywać:

An rvalue can't be used as the left-hand operand of the built-in assignment or compound assignment operators.

PS.
Ze strony http://en.cppreference.com/w/cpp/language/operator_assignment:

The direct assignment operator expects a modifiable lvalue as its left operand and an rvalue expression or a braced-init-list (since C++11) as its right operand, and returns an lvalue identifying the left operand after modification.

0

Polecam nauczyć się angielskiego.

1 użytkowników online, w tym zalogowanych: 0, gości: 1