|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Junior Member
Iscritto dal: Jul 2009
Messaggi: 29
|
Esercizio in C++ e problema compilatore, aiuto urgente.
Salve ragazzi. Sono iscritto al primo anno della facoltà di ingegneria informatica. Purtroppo per seri problemi di salute non ho potuto seguire il primo semestre e quindi mi ritrovo a studiare da solo. Come testi uso quelli della deitel (C++ fondamenti di programmazione e C++ tecniche avanzate di programmazione).
Sono al capitolo 3 del suddetto libro (classi e oggetti) e come compilatore uso l'ottimo (a parer mio) e comunque obbligatorio (per volere del docente) QT Creator (si usa quello durante gli esami, quindi devo abituarmi a compilare su quello). A parte un pò di confusione sui costruttori, su come richiamare le classi ecc... per ora tutto ok. Il problema sorge nella compilazione. Sto facendo un esercizio, il quale, giusto per provarlo, è già compilato sul dischetto del libro. Il codice è diviso in una classe chiamata "GradeBook.h", una sezione per le funzioni membro "GradeBook.cpp" e infine la funzione main, chiamata in questo caso "main.cpp". Vi riporto in spoiler i codici: GradeBook.h: // Fig. 3.11: GradeBook.h // GradeBook class definition. This file presents GradeBook's public // interface without revealing the implementations of GradeBook's member // functions, which are defined in GradeBook.cpp. #include <string> // class GradeBook uses C++ standard string class using std::string; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor that initializes courseName void setCourseName( string ); // function that sets the course name string getCourseName(); // function that gets the course name void displayMessage(); // function that displays a welcome message private: string courseName; // course name for this GradeBook }; // end class GradeBook GradeBook.cpp: // Fig. 3.12: GradeBook.cpp // GradeBook member-function definitions. This file contains // implementations of the member functions prototyped in GradeBook.h. #include <iostream> using std::cout; using std::endl; #include "GradeBook.h" // include definition of class GradeBook // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return courseName; // return object's courseName } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage ed infine main.cpp: // Fig. 3.13: fig03_13.cpp // GradeBook class demonstration after separating // its interface from its implementation. #include <iostream> using std::cout; using std::endl; #include "GradeBook.h" // include definition of class GradeBook // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; return 0; // indicate successful termination } // end main Il codice presumo sia corretto, ma nel momento in cui compilo main.cpp mi dà il seguente errore: undefined reference to "GradeBook::GradeBook(std::string)" con una sfilza di errori simili. Andando a vedere GradeBook.cpp noto che la libreria iostream non viene inclusa! Cioè, mi dice a #include <iostream> che iostream non risulta essere nella cartella in quanto lo cerca nella cartella dove sta il file GradeBook.cpp! Presumo sia quello il problema ma non riesco a capire come fare a dire al compilatore di cercare iostream nelle sue cartelle di default. Inoltre con Devc++ ho lo stesso problema. Comunque con QT Creator per creare un codice faccio "new file or project > QT console application". Faccio bene? Grazie per le risposte. EDIT: ho notato che lo spoiler non riduce il testo... quindi lo levo. |
![]() |
![]() |
![]() |
#2 | ||
Junior Member
Iscritto dal: Aug 2011
Città: Francia
Messaggi: 5
|
Quote:
Devi soltanto aggiungere GradeBook.cpp al tuo progetto perché sembra che QtCreator non lo tiene di conto perché il collegamento non trova l'implementazione delle funzioni di GradeBook.h Quote:
|
||
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Jan 2008
Messaggi: 8406
|
Ho provato il codice che hai postato su QTCreator sotto Linux e compila e funziona perfettamente.
Il problema nel tuo caso è che manca il pacchetto dev di libstdc++. P.S. sei sotto windows o linux? |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:00.