Forum - C/C++
- Problema con iostream ed il gcc
Gabry (Normal User)
Rookie
Messaggi: 36
Iscritto: 19/04/2011
Premetto che programmo in C++ da poco e per motivi scolastici mentre il C ed altri linguaggi li conosco decentemente. Il mio OS è Ubuntu 12 ed il mio compiulatore il gcc. Il programma è questo
Codice sorgente - presumibilmente C++
#include <iostream>
using namespace std;
main( ) {
int a,b,c;
cout << "Inserisci A: " ;
cin >> a;
cout << "Inserisci B: " ;
cin >> b;
cout << "Inserisci C: " ;
cin >> c;
int max= a;
if ( b> max) max = b;
if ( c> max) max = c;
cout << max<< "\n \n " ;
}
Appena provo a compilarlo il messaggio è il seguente
Codice sorgente - presumibilmente VB.NET
Massimo.cpp :5:6: warning: ISO C++ forbids declaration of ‘main’ with no type [ - Wreturn- type]
/ tmp/ ccFaS7rV.o : In function `main':
Massimo.cpp :( .text + 0xe) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x13) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0x1f) : undefined reference to `std::cin'
Massimo.cpp :( .text + 0x24) : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>( int& ) '
Massimo.cpp :( .text + 0x2e) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x33) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0x3f) : undefined reference to `std::cin'
Massimo.cpp :( .text + 0x44) : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>( int& ) '
Massimo.cpp :( .text + 0x4e) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x53) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0x5f) : undefined reference to `std::cin'
Massimo.cpp :( .text + 0x64) : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>( int& ) '
Massimo.cpp :( .text + 0x90) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x95) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<( int ) '
Massimo.cpp :( .text + 0xa2) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
/ tmp/ ccFaS7rV.o : In function `__static_initialization_and_destruction_0( int , int ) ':
Massimo.cpp :( .text + 0xd0) : undefined reference to `std::ios_base::Init::Init( ) '
Massimo.cpp :( .text + 0xd5) : undefined reference to `std::ios_base::Init::~Init( ) '
collect2: ld returned 1 exit status
provando a togliere le riga "using namespace std;" ottengo un altro errore:
Codice sorgente - presumibilmente Delphi
Massimo. cpp : 3: 6: warning: ISO C++ forbids declaration of ‘main’ with no type [ - Wreturn- type ]
Massimo. cpp : In function ‘int main( ) ’:
Massimo. cpp : 5: 3: error: ‘cout’ was not declared in this scope
Massimo. cpp : 5: 3: note: suggested alternative:
/ usr/ include / c++/ 4. 6/ iostream: 62: 18: note: ‘std:: cout’
Massimo. cpp : 6: 3: error: ‘cin’ was not declared in this scope
Massimo. cpp : 6 : 3 : note: suggested alternative:
la libreria iostream serve per le funzioni cin e cout. Come detto conosco il C e non avrei problemi personalmente ad usare printf e scanf, ma il mio professore non credo capisca molto l'argomento (provate a spiegargli cos'è Linux...) e quindi vuole proprio che si faccia a modo suo.
Quindi come faccio a includere iostream e compagnia bella?
RiccardoG97 (Member )
Pro
Messaggi: 133
Iscritto: 03/01/2012
Prova con questo codice:
Codice sorgente - presumibilmente C++
#include <iostream>
int main ( void )
{
int a, b, c;
std:: cout << "Inserisci A: " ;
std:: cin >> a;
std:: cout << "Inserisci B: " ;
std:: cin >> b;
std:: cout << "Inserisci C: " ;
std:: cin >> c;
int max = a;
if ( max < b )
max = b;
if ( max < c )
max = c;
std:: cout << "Il numero massimo è: " << max;
return 0 ;
}
Dimmi se funziona... a me funge
Gabry (Normal User)
Rookie
Messaggi: 36
Iscritto: 19/04/2011
Codice sorgente - presumibilmente VB.NET
/ tmp/ ccxgby6t.o : In function `main':
Massimo.cpp :( .text + 0xe) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x13) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0x1f) : undefined reference to `std::cin'
Massimo.cpp :( .text + 0x24) : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>( int& ) '
Massimo.cpp :( .text + 0x2e) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x33) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0x3f) : undefined reference to `std::cin'
Massimo.cpp :( .text + 0x44) : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>( int& ) '
Massimo.cpp :( .text + 0x4e) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x53) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0x5f) : undefined reference to `std::cin'
Massimo.cpp :( .text + 0x64) : undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>( int& ) '
Massimo.cpp :( .text + 0x90) : undefined reference to `std::cout'
Massimo.cpp :( .text + 0x95) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >( std::basic_ostream<char, std::char_traits<char> >& , char const * ) '
Massimo.cpp :( .text + 0xa2) : undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<( int ) '
/ tmp/ ccxgby6t.o : In function `__static_initialization_and_destruction_0( int , int ) ':
Massimo.cpp :( .text + 0xd0) : undefined reference to `std::ios_base::Init::Init( ) '
Massimo.cpp :( .text + 0xd5) : undefined reference to `std::ios_base::Init::~Init( ) '
collect2: ld returned 1 exit status
XBarboX (Member )
Guru
Messaggi: 945
Iscritto: 31/12/2008
Allora un codice C++ si compila con g++ e non gcc. gcc è per il c.
Poi hai fatto un piccolo errore, hai dimenticato "int " davanti a main.
Compila così con g++:
g++ file.cpp -o file.out
Codice sorgente - presumibilmente C++
#include <iostream>
using namespace std;
int main( ) {
int a,b,c;
cout << "Inserisci A: " ;
cin >> a;
cout << "Inserisci B: " ;
cin >> b;
cout << "Inserisci C: " ;
cin >> c;
int max= a;
if ( b> max) max = b;
if ( c> max) max = c;
cout << max<< "\n \n " ;
}
Gabry (Normal User)
Rookie
Messaggi: 36
Iscritto: 19/04/2011
proverò con il g++. Grazie mille.
ps
Quello dell'int davanti al main lo sapevo, ma il prof aveva detto di fare così...
Gabry (Normal User)
Rookie
Messaggi: 36
Iscritto: 19/04/2011
Ora ho provato e va tutto bene. Compilazione perfietta.