code blocks/mingw32-g++/C++11 ISO: "ISO C++ forbids declaration of"..."with no type" [-fpermissive] przez: 'string& const'
Ostatnio zmodyfikowano 2016-01-13 14:06
zdzislaw Temat założony przez niniejszego użytkownika |
code blocks/mingw32-g++/C++11 ISO: "ISO C++ forbids declaration of"..."with no type" [-fpermissive] przez: 'string& const' » 2016-01-13 12:57:35 Witam, Nie wiem dlaczego w poniżej zamieszczonym kodzie: #include <iostream> using namespace std;
string s123( string a, string & b ) { a = "1234 "; return a + b; }
int main() { string x = "cos tam 1 "; string y = "cos tam 2"; string z; z = s123( x, y ); cout << z << "\n"; return 0; }
parametr: 'string a', który przesyłany jest przez wartość pozwala zmienić argument 'x', co daje: '1234 cos tam 2' Chyba tylko przez referencje mógłby być taki skutek ? Stąd, chcąc zablokować możliwość zmiany parametru: 'a' kod zmieniłem na: #include <iostream> using namespace std;
string s123( const & a, const & b ) { a = "1234"; return a + b; }
int main() { string x = "cos tam 1 "; string y = "cos tam 2"; string z; z = s123( x, y ); cout << z << "\n"; return 0; }
i przez typ: 'const', nie istotne czy:'(const& a, const& b) czy (const a, const b) czy (const& a, string& b) itp. zwraca błędy, do których w google nie znalezłem opisów: D:\test\test2.cpp|4|error: ISO C++ forbids declaration of 'a' with no type [-fpermissive]| D:\test\test2.cpp|4|error: ISO C++ forbids declaration of 'b' with no type [-fpermissive]| D:\test\test2.cpp||In function 'std::string s123(const int&, const int&)':| D:\test\test2.cpp|6|error: assignment of read-only reference 'a'| D:\test\test2.cpp|6|error: invalid conversion from 'const char*' to 'int' [-fpermissive]| D:\test\test2.cpp|7|error: invalid conversion from 'int' to 'const char*' [-fpermissive]| d:\cb\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|487|error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' [-fpermissive]| D:\test\test2.cpp||In function 'int main()':| D:\test\test2.cpp|15|error: invalid initialization of reference of type 'const int&' from expression of type 'std::string {aka std::basic_string<char>}'| D:\test\test2.cpp|4|error: in passing argument 1 of 'std::string s123(const int&, const int&)'| ||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
jeśli ktoś mógłby przekierować mnie do opisu tego problemu lub samemu w skrócie wskazać rozwiązanie to ucieszyłbym się. Pozdrawiam |
|
michal11 |
» 2016-01-13 13:32:26 W tym wypadku: string s123( const & a, const & b ) jakiego typu jest zmienna a i jakiego typu jest zmienna b ? |
|
darko202 |
» 2016-01-13 13:40:44 |
|
zdzislaw Temat założony przez niniejszego użytkownika |
» 2016-01-13 13:45:49 const string& a - Dziękuję za podpowiedź.
Dlaczego funkcja: 'string s123 (string a, string b)' zmienia argument 'x' w 'z = s123 (x,y);' na 'a="1234 "' pomimo, iż argument 'x' nie jest przekazywany przez referencje ? |
|
darko202 |
» 2016-01-13 14:03:56 nie chce mi się sprawdzać, ale byłby to niezgodne z podstawowymi zasadami patrzę na Twój kod ... z = s123( x, y ); cout << z << "\n"; return 0;
|
|
zdzislaw Temat założony przez niniejszego użytkownika |
» 2016-01-13 14:06:31 być może. dla mnie każda pomoc się liczy, dziękuję |
|
« 1 » |