Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

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
Autor Wiadomość
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:

C/C++
#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:
C/C++
#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







P-143434
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 ?
P-143436
darko202
» 2016-01-13 13:40:44
masz wyraźnie informację "forbids declaration"  - deklaracja zabroniona

chyba nie rozumiesz wymagań :
* deklaracji typu zmiennych
* ograniczeń const

popatrz np. na
http://www.learncpp.com​/cpp-tutorial​/73-passing-arguments-by-refer​ence​/

C/C++
void addOne( int & y ) // y is a reference variable
{
    y = y + 1;
} // y is destroyed here


The following function will produce a compiler error:

( const int & x ) // x is a const reference
{
    x = 6; // compile error: a const reference cannot have its value changed!
}
 




 
P-143437
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 ?
P-143438
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
C/C++
...
z = s123( x, y ); // tu zmieniasz "Z"
cout << z << "\n"; // tu wyświetlasz to zmienione "Z" a nie "X"
return 0; // koniec programu
P-143439
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ę
P-143440
« 1 »
  Strona 1 z 1