Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [cpp]funkcje rekurencyjne
e-Informatyk
post
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 16.09.2007

Ostrzeżenie: (0%)
-----


Witam,

Jestem aktualnie w trakcie nauki C++. Podczas funkcji rekurencyjnych napotkałem następujący problem:

Kod
#include <iostream>
using namespace std;

void funkcja_testowa(int argument)
{
    if(argument > 0) funkcja_testowa(argument-1);
}

int main()
{
funkcja_testowa(5);
}


Program prawidłowo się kompiluje, jednak po jego uruchomieniu nic się nie dzieje. Czy jest na tym forum ktoś kto mógłby zwrócić moją uwagę na błąd popełniony w tej aplikacji (standardy itd.)? Program ten to lekko zmodyfikowany (nazwy funkcji, argumentów) przykład z najnowszej Symfonii.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
kwiateusz
post
Post #2


Admin Techniczny


Grupa: Administratorzy
Postów: 2 071
Pomógł: 93
Dołączył: 5.07.2005
Skąd: Olsztyn




imo działa jak najbardziej tylko tyle ze nie masz nigdzie cout więc nie zobaczysz na ekranie wyniku jego pracy a jedynie wystartuje i sie zakończy...
Go to the top of the page
+Quote Post
e-Informatyk
post
Post #3





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 16.09.2007

Ostrzeżenie: (0%)
-----


@kwiateusz: dzięki, ale Jak spróbuję gdzieś wstawić cout, np.:
Kod
#include <iostream>
using namespace std;

//*********************************************
void funkcja_testowa(int argument);
//*********************************************

int main()
{
cout << "Hello World";
funkcja_testowa(5);
}

//*********************************************
void funkcja_testowa(int argument)
{
cout << "test ";
    if(argument > 0) cout << funkcja_testowa(argument-1);
}
//*********************************************


albo po prostu dopisać gdzies w poprzednim kodzie kompilator wywala błędy:

CODE
maciej@maciej-desktop:~/Desktop$ g++ moc.cpp
moc.cpp: In function ‘void funkcja_testowa(int)’:
moc.cpp:6: error: ‘cout’ was not declared in this scope
maciej@maciej-desktop:~/Desktop$ g++ moc.cpp
moc.cpp: In function ‘void funkcja_testowa(int)’:
moc.cpp:16: error: expected `;' before ‘if’
maciej@maciej-desktop:~/Desktop$ g++ moc.cpp
moc.cpp: In function ‘void funkcja_testowa(int)’:
moc.cpp:16: error: no match for ‘operator<<’ in ‘std::cout << funkcja_testowa((argument - 1))’
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:78: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:90: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:241: note: std::basic_ostream<_CharT, _Traits>& ts>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:125: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:157: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:183: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:215: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:288: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:361: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:335: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:384: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:407: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ostream.tcc:430: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]


Niepotrzebnie było to cout obok if. Problem rozwiązany, temat do zamknięcia. smile.gif

Ten post edytował e-Informatyk 27.10.2007, 18:48:00
Go to the top of the page
+Quote Post
phpion
post
Post #4





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Kod
#include <iostream>
using namespace std;

//*********************************************
void funkcja_testowa(int argument);
//*********************************************

int main()
{
cout << "Hello World";
funkcja_testowa(5);
}

//*********************************************
void funkcja_testowa(int argument)
{
    cout << "test ";

    if(argument > 0) {
        cout << argument;
        funkcja_testowa(argument-1);
    }
}
//*********************************************

To tak po najmniejszej linii oporu.

Ten post edytował phpion.com 27.10.2007, 18:47:47
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 22:34