temulus
asked on
undefined reference errors on compile of c++ program using gcc 3.2
This program was for an assignment that I had for my Intro to C++ class. I am not asking for others to do my homework, the assignment has already been turned in.
I could not get the program to compile correctly using gcc version 3.2. I asked the instructor for assistance and she pointed out that the program compiled fine under Microsoft Visual C++. So in order to get the program completed, I finished it using Visual C++.
My professor, and the rest of my class for that matter, uses Visual C++ and has limited knowledge of gcc and issues involved with using gcc. I, however, will not be programming for the Windows platorm at all, so I would like to learn what I did wrong in my program in order for it to compile under gcc.
When I try to compile the program I get the following errors:
/tmp/cct1iilo.o: In function `main':
/tmp/cct1iilo.o(.text+0x2e ): undefined reference to `Movie::openDBFile()'
/tmp/cct1iilo.o(.text+0x40 ): undefined reference to `Movie::printMainMenu() const'
/tmp/cct1iilo.o(.text+0xb2 ): undefined reference to `Movie::addMovie()'
/tmp/cct1iilo.o(.text+0xc9 ): undefined reference to `Movie::delMovie()'
/tmp/cct1iilo.o(.text+0xe0 ): undefined reference to `Movie::userSearch()'
/tmp/cct1iilo.o(.text+0xf4 ): undefined reference to `Movie::listMovies() const'
/tmp/cct1iilo.o(.text+0x10 8): undefined reference to `Movie::rentMovie()'
/tmp/cct1iilo.o(.text+0x11 c): undefined reference to `Movie::returnMovie()'
/tmp/cct1iilo.o(.text+0x17 a): undefined reference to `Movie::saveDBFile()'
collect2: ld returned 1 exit status
Here is my code in case you need it. I can also email the files to you if you prefer:
**** start of client program file Movie-store.cpp *******
#include"Movie.h" //preprocessor directives
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
Movie videodb; //variable of class type Movie
char answer;
system("tput clear");
videodb.openDBFile();
do
{
videodb.printMainMenu();
cin >> answer;
answer = toupper(answer);
switch(answer) {
case 'A': videodb.addMovie(); break;
case 'D': videodb.delMovie(); break;
case 'S': videodb.userSearch(); break;
case 'L': videodb.listMovies(); break;
case '-': videodb.rentMovie(); break;
case '+': videodb.returnMovie(); break;
case 'Q': cout << "\n\nThank you for using the Video Management System.\n\n";break;
default: cout << "\n\nError in choice. Please try again.\n" << endl;
}
} while (answer != 'Q');
videodb.saveDBFile();
return 0;
}
**** end of client program file Movie-store.cpp *******
**** start of specification file Movie.h *******
#include <iostream>
using namespace std;
const int Max_Video=100;
struct MovieType //define our struct to use in our class
{
char title[80];
char genre[12];
char moviestar1[30];
char moviestar2[30];
int totalNumCopies;
int numAvailCopies;
};
class Movie
{
public:
void printMainMenu() const;
void chooseGenre(char[12]);
// uses a menu to select a genre
// Precondition: user has chosen function which needs a genre defined
// Postcondition: defines a genre for addition of title etc.
void addMovie();
// user adds a movie title to the db
// Precondition: video list has been read into memory
// Postcondition: the new title is added to the video list
void delMovie();
// user deletes a movie title from the db
// Precondition: video list has been read into memory, and title
// is found in the list
// Postcondition: the title is deleted from the video list
void listMovies() const;
// Precondition: video list has been read into memory
// Postcondition: the entire list of movies is printed to the screen
void userSearch();
// Function to prompt for user defined pattern to search for
// Precondition: video list has been read into memory and the users search
// results in a matching title
// Postcondition: the results of the search are printed to the screen
void searchMovies(char[10], char[80], int[], int&);
// Generalized search function
// Precondition: video list has been read into memory, and title is found
// Postcondition: the results from the search are returned to the calling
// function
void rentMovie();
// Marks a movie as rented
// Precondition: video list has been read into memory, and title is found
// Postcondition: the avail number for the title is decreased by 1
void returnMovie();
// Marks a movie as returned to store
// Precondition: video list has been read into memory, and title is found
// Postcondition: the avail number for the title is increased by 1
int openDBFile();
// Precondition: video list file is opened successfully
// Postcondition: the video list contains entries from the db file
void saveDBFile();
// Precondition: video list file is opened successfully
// Postcondition: the video list is written to the db file
int compareStrings(char[80],ch ar[80]);
//strcmpi does not exist for gcc, this is a workaround
//Precondition: two input strings
//Postcondition: results determine whether or not strings match
MovieType videos[Max_Video];
private:
// MovieType videos[Max_Video];
int totalNumMovies;
};
**** end of specification file Movie.h *******
**** start of implementation file Movie.cpp *******
#include"Movie.h"//preproc essor directives
#include <iostream>
#include <iomanip>
using namespace std;
//************************ ********** ********** ********** ********** ****
int Movie::openDBFile()
{
FILE* movieDBFile;
if ((movieDBFile = fopen("Montgomery6.bin", "rb")) == NULL) {
cout << "\nCannot open file: Montgomery6.bin\n";
return 1;
} else {
fread(videos, sizeof videos, 1, movieDBFile);
fread(&totalNumMovies, sizeof totalNumMovies, 1, movieDBFile);
return 0;
}
}
//************************ ********** ********** ********** ********** ****
void Movie::saveDBFile()
{
FILE* movieDBFile;
if ((movieDBFile=fopen("Montg omery6.bin ", "wb")) == NULL) {
cout << "Cannot open file" << endl;
} else {
//Open our binary file for writing
fwrite(videos, sizeof videos, 1, movieDBFile);
fwrite(&totalNumMovies, sizeof totalNumMovies, 1, movieDBFile);
fclose(movieDBFile);
}
}
//************************ ********** ********** ********** ********** ****
void Movie::printMainMenu() const
{
cout << "\n_______________________ \n";
cout << "Video Management System\n";
cout << "_______________________\n ";
cout << "A. Add a Movie\n";
cout << "D. Delete a Movie\n";
cout << "L. List all Movies\n";
cout << "S. Search for a Movie\n";
cout << "-. Mark movie as rented\n";
cout << "+. Mark movie as returned\n";
cout << "Q. Quit\n";
cout << "_______________________\n ";
cout << "Enter your choice: ";
}
//************************ ********** ********** ********** ********** ****
void Movie::chooseGenre(char genre[12])
{
char answer = '0';
char valid = '1';
do {
cout << "\nChoose a movie genre\n";
cout << "--------------------\n";
cout << "1. Action\n";
cout << "2. Classics\n";
cout << "3. Comedy\n";
cout << "4. Drama\n";
cout << "5. Family\n";
cout << "6. Foreign\n";
cout << "7. Horror\n";
cout << "8. Romance\n";
cout << "9. SciFi\n";
cout << "Enter the number of your choice: ";
cin >> answer;
switch(answer) {
case '1' : strcpy(genre,"Action"); valid = '0'; break;
case '2' : strcpy(genre,"Classics"); valid = '0'; break;
case '3' : strcpy(genre,"Comedy"); valid = '0'; break;
case '4' : strcpy(genre,"Drama"); valid = '0'; break;
case '5' : strcpy(genre,"Family"); valid = '0'; break;
case '6' : strcpy(genre,"Foreign"); valid = '0'; break;
case '7' : strcpy(genre,"Horror"); valid = '0'; break;
case '8' : strcpy(genre,"Romance"); valid = '0'; break;^M
case '9' : strcpy(genre,"SciFi"); valid = '0'; break;
default : cout << "\n\nInvalid choice. Please try again.\n"; break;
}
} while ( valid != '0' );
}
//************************ ********** ********** ********** ********** ****
void Movie::addMovie()
{
char title[80];
char genre[12];
char moviestar1[30];
char moviestar2[30];
int totalNumCopies;
char field[10] = "title";
char valid='1';
int results[Max_Video]; //array to store search results
int count=0; //number of results returned by search
while (valid != '0') {
system("tput clear");
cin.clear();
cin.ignore(100,'\n');
cout << "Enter movie title to add: ";
cin.getline(title,80);
searchMovies(field, title, results, count);
if (count > 0) {
cout << "\n\n" << title << " already exists in the movie database.\n\n";
break;
}
chooseGenre(genre);
cout << "Enter the name of the movie's star: ";
cin.clear(); //flush the buffer
cin.ignore(100,'\n');
cin.getline(moviestar1,30) ;
cout << "Enter the name of a second star or <ENTER> to skip: ";
cin.getline(moviestar2,30) ;
cout << "Enter total number of copies: ";
cin >> totalNumCopies;
while (!cin) {
cout << "\nEnter only numbers please.\n\n";
cin.clear();
cin.ignore(100,'\n');
cout << "Enter total number of copies: ";
cin >> totalNumCopies;
}
//save the values into our videos array
strcpy(videos[totalNumMovi es].title, title);
strcpy(videos[totalNumMovi es].genre, genre);
strcpy(videos[totalNumMovi es].movies tar1, moviestar1);
strcpy(videos[totalNumMovi es].movies tar2, moviestar2);
videos[totalNumMovies].tot alNumCopie s = totalNumCopies;
videos[totalNumMovies].num AvailCopie s = totalNumCopies;
totalNumMovies++; // increment after adding a movie
cout << "\n" << title << " added to the movie database.\n\n";
valid = '0';
}
}
//************************ ********** ********** ********** ********** ****
void Movie::delMovie()
{
char field[10]= "title";
char title[80];
int results[Max_Video];
char valid='1';
int count=0;
while (valid != '0') {
system("tput clear");
if (totalNumMovies == 0) {
cout << "\n\nNo movies in the database. Nothing to delete.\n\n";
break;
}
cout << "Enter movie title to delete: ";
cin.clear();
cin.ignore(100,'\n');
cin.getline(title,80);
searchMovies(field, title, results, count);
if (count > 0) {
videos[results[0]]=videos[ totalNumMo vies - 1];
totalNumMovies--;
cout << "\n" << title << " deleted from the movie database.\n\n";
valid='0';
} else {
cout << "\n" << title << " not found in the movie database.\n\n";
break;
}
}
}
//************************ ********** ********** ********** ********** ****
void Movie::userSearch()
{
char field[10];
char searchPattern[80];
int results[100];
char genre[12];
char answer;
char valid='1';
int count=0;
int i;
do {
cout << "\n\n-----------\n";
cout << "Search Menu\n";
cout << "-----------\n";
cout << "Do you want to search:\n\n";
cout << "1. By Movie Title\n";
cout << "2. By Actor/Actress\n";
cout << "3. By Genre\n\n";
cout << "Enter the number of your choice: ";
cin >> answer;
if ((answer == '1') || (answer == '2') || (answer == '3')) {
valid = '0';
}
} while (valid != '0');
if (answer == '1') {
cout << "\n\nEnter a movie title: ";
cin.clear(); // clear buffer to allow for input
cin.ignore(100,'\n');
cin.getline(searchPattern, 80);
strcpy(field,"title");
} else if (answer == '2') {
cout << "\n\nEnter the name of an Actor/Actress: ";
cin.clear();
cin.ignore(100,'\n');
cin.getline(searchPattern, 80);
strcpy(field,"moviestar");
} else if (answer == '3') {
chooseGenre(genre);^M
strcpy(searchPattern,genre );
strcpy(field,"genre");^M
}
searchMovies(field, searchPattern, results, count);
if (count > 0) {
//list all elements of videos for search results
cout << "\nSearch results for " << searchPattern << endl << endl;
cout << setw(20) << "Movie" << setw(7) << "Genre" << setw(15) << "Star#1";
cout << setw(15) << "Star#2" << setw(10) << "#copies" << setw(7) << "#avail" << endl;
cout << "------------------------- ---------- ---------- ---------- ---------- ---------\ n";
for (i=0; i<count; i++) {
cout << setw(20) << videos[results[i]].title;
cout << setw(7) << videos[results[i]].genre;
cout << setw(15) << videos[results[i]].moviest ar1;
cout << setw(15) << videos[results[i]].moviest ar2;
cout << setw(10) << videos[results[i]].totalNu mCopies;
cout << setw(7) << videos[results[i]].numAvai lCopies << endl;
}
} else {
cout << "No records found matching " << searchPattern << endl;
}
}
//************************ ********** ********** ********** ********** ****
void Movie::searchMovies (char field[10], char searchPattern[80], int results[Max_Video], int& count)
{
int i;
if ( compareStrings(field,"titl e") == 0) {
if (totalNumMovies > 0) {
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt ern,videos [i].title) == 0) { //strings match - add movie to the results array
results[count] = i;
count++;
}
}
}
} else if ( compareStrings(field,"movi estar") == 0) {
if (totalNumMovies > 0) {
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt ern,videos [i].movies tar1) == 0) {
results[count] = i;
count++;
}
}
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt ern,videos [i].movies tar2) == 0) {
results[count] = i;
count++;
}
}
}
} else if ( compareStrings(field,"genr e") == 0) {
if (totalNumMovies > 0) {
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt ern,videos [i].genre) == 0) {
results[count] = i;
count++;
}
}
}
}
}
//************************ ********** ********** ********** ********** ****
void Movie::rentMovie ()
{
char title[80];
char field[10] = "title";
int results[100];
char valid='1';
int count=0;
while (valid != '0') {
system("tput clear");
cin.clear();
cin.ignore(100,'\n');
cout << "Enter movie title to mark as rented: ";
cin.getline(title,80);
results[0]=-1;
searchMovies(field, title, results, count);
if (count >= 0) {
//if copies are available mark as rented
if (videos[results[0]].numAva ilCopies != 0) {
videos[results[0]].numAvai lCopies--;
cout << "\n" << title << " marked as rented in the movie database.\n\n";
valid = '0';
} else {
cout << "\n\nAll copies already rented. Cannot mark ";
cout << videos[results[0]].title << " as rented.\n\n";
break;
}
} else {
cout << "\n" << title << " not found in the movie database.\n\n";
break;
}
}
}
//************************ ********** ********** ********** ********** ****
void Movie::returnMovie ()
{
char title[80];
char field[10] = "title";
int results[100];
char valid='1';
int count=0;
while (valid != '0') {
system("tput clear");
cin.clear();
cin.ignore(100,'\n');
cout << "Enter movie title to mark as returned: ";
cin.getline(title,80);
results[0]=-1;
searchMovies(field, title, results, count);
if (count >= 0) {
//if all movies are not accounted for, mark as returned
if (videos[results[0]].numAva ilCopies != videos[results[0]].totalNu mCopies) {
videos[results[0]].numAvai lCopies++;
cout << "\n" << title << " marked as returned in the movie database.\n\n";
valid = '0';
} else {
cout << "\n\nAll copies already accounted for. Cannot mark ";
cout << videos[results[0]].title << " as returned.\n\n";
break;
}
} else {
cout << "\n" << title << " not found in the movie database.\n\n";
break;
}
}
}
//************************ ********** ********** ********** ********** ****
void Movie::listMovies() const
{
int i;
cout << endl << endl;
system("tput clear");
if (totalNumMovies > 0) {
cout << "\nList of all movies in the database\n\n";
cout << setw(20) << "Movie" << setw(7) << "Genre" << setw(15) << "Star#1";^M
cout << setw(15) << "Star#2" << setw(10) << "#copies" << setw(7) << "#avail" << endl;^M
cout << "------------------------- ---------- ---------- ---------- ---------- ---------\ n";^M
for (i=0; i<totalNumMovies; i++) {^M
cout << setw(20) << videos[i].title;^M
cout << setw(7) << videos[i].genre;^M
cout << setw(15) << videos[i].moviestar1;^M
cout << setw(15) << videos[i].moviestar2;^M
cout << setw(10) << videos[i].totalNumCopies;^ M
cout << setw(7) << videos[i].numAvailCopies << endl;^M
}
} else {
cout << "There are no movies in the database.";
}
cout << endl << endl;
}
//************************ ********** ********** ********** ********** ****
int Movie::compareStrings (char string1[80], char string2[80]) {
if( strlen(string1) != strlen(string2)) return 1;
for( int i = 0; i < strlen(string1); ++i ) {
if( toupper(string1[i]) != toupper(string2[i]) ) return 1;
}
return 0;
}
**** end of implementation file Movie.cpp *******
I could not get the program to compile correctly using gcc version 3.2. I asked the instructor for assistance and she pointed out that the program compiled fine under Microsoft Visual C++. So in order to get the program completed, I finished it using Visual C++.
My professor, and the rest of my class for that matter, uses Visual C++ and has limited knowledge of gcc and issues involved with using gcc. I, however, will not be programming for the Windows platorm at all, so I would like to learn what I did wrong in my program in order for it to compile under gcc.
When I try to compile the program I get the following errors:
/tmp/cct1iilo.o: In function `main':
/tmp/cct1iilo.o(.text+0x2e
/tmp/cct1iilo.o(.text+0x40
/tmp/cct1iilo.o(.text+0xb2
/tmp/cct1iilo.o(.text+0xc9
/tmp/cct1iilo.o(.text+0xe0
/tmp/cct1iilo.o(.text+0xf4
/tmp/cct1iilo.o(.text+0x10
/tmp/cct1iilo.o(.text+0x11
/tmp/cct1iilo.o(.text+0x17
collect2: ld returned 1 exit status
Here is my code in case you need it. I can also email the files to you if you prefer:
**** start of client program file Movie-store.cpp *******
#include"Movie.h" //preprocessor directives
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
Movie videodb; //variable of class type Movie
char answer;
system("tput clear");
videodb.openDBFile();
do
{
videodb.printMainMenu();
cin >> answer;
answer = toupper(answer);
switch(answer) {
case 'A': videodb.addMovie(); break;
case 'D': videodb.delMovie(); break;
case 'S': videodb.userSearch(); break;
case 'L': videodb.listMovies(); break;
case '-': videodb.rentMovie(); break;
case '+': videodb.returnMovie(); break;
case 'Q': cout << "\n\nThank you for using the Video Management System.\n\n";break;
default: cout << "\n\nError in choice. Please try again.\n" << endl;
}
} while (answer != 'Q');
videodb.saveDBFile();
return 0;
}
**** end of client program file Movie-store.cpp *******
**** start of specification file Movie.h *******
#include <iostream>
using namespace std;
const int Max_Video=100;
struct MovieType //define our struct to use in our class
{
char title[80];
char genre[12];
char moviestar1[30];
char moviestar2[30];
int totalNumCopies;
int numAvailCopies;
};
class Movie
{
public:
void printMainMenu() const;
void chooseGenre(char[12]);
// uses a menu to select a genre
// Precondition: user has chosen function which needs a genre defined
// Postcondition: defines a genre for addition of title etc.
void addMovie();
// user adds a movie title to the db
// Precondition: video list has been read into memory
// Postcondition: the new title is added to the video list
void delMovie();
// user deletes a movie title from the db
// Precondition: video list has been read into memory, and title
// is found in the list
// Postcondition: the title is deleted from the video list
void listMovies() const;
// Precondition: video list has been read into memory
// Postcondition: the entire list of movies is printed to the screen
void userSearch();
// Function to prompt for user defined pattern to search for
// Precondition: video list has been read into memory and the users search
// results in a matching title
// Postcondition: the results of the search are printed to the screen
void searchMovies(char[10], char[80], int[], int&);
// Generalized search function
// Precondition: video list has been read into memory, and title is found
// Postcondition: the results from the search are returned to the calling
// function
void rentMovie();
// Marks a movie as rented
// Precondition: video list has been read into memory, and title is found
// Postcondition: the avail number for the title is decreased by 1
void returnMovie();
// Marks a movie as returned to store
// Precondition: video list has been read into memory, and title is found
// Postcondition: the avail number for the title is increased by 1
int openDBFile();
// Precondition: video list file is opened successfully
// Postcondition: the video list contains entries from the db file
void saveDBFile();
// Precondition: video list file is opened successfully
// Postcondition: the video list is written to the db file
int compareStrings(char[80],ch
//strcmpi does not exist for gcc, this is a workaround
//Precondition: two input strings
//Postcondition: results determine whether or not strings match
MovieType videos[Max_Video];
private:
// MovieType videos[Max_Video];
int totalNumMovies;
};
**** end of specification file Movie.h *******
**** start of implementation file Movie.cpp *******
#include"Movie.h"//preproc
#include <iostream>
#include <iomanip>
using namespace std;
//************************
int Movie::openDBFile()
{
FILE* movieDBFile;
if ((movieDBFile = fopen("Montgomery6.bin", "rb")) == NULL) {
cout << "\nCannot open file: Montgomery6.bin\n";
return 1;
} else {
fread(videos, sizeof videos, 1, movieDBFile);
fread(&totalNumMovies, sizeof totalNumMovies, 1, movieDBFile);
return 0;
}
}
//************************
void Movie::saveDBFile()
{
FILE* movieDBFile;
if ((movieDBFile=fopen("Montg
cout << "Cannot open file" << endl;
} else {
//Open our binary file for writing
fwrite(videos, sizeof videos, 1, movieDBFile);
fwrite(&totalNumMovies, sizeof totalNumMovies, 1, movieDBFile);
fclose(movieDBFile);
}
}
//************************
void Movie::printMainMenu() const
{
cout << "\n_______________________
cout << "Video Management System\n";
cout << "_______________________\n
cout << "A. Add a Movie\n";
cout << "D. Delete a Movie\n";
cout << "L. List all Movies\n";
cout << "S. Search for a Movie\n";
cout << "-. Mark movie as rented\n";
cout << "+. Mark movie as returned\n";
cout << "Q. Quit\n";
cout << "_______________________\n
cout << "Enter your choice: ";
}
//************************
void Movie::chooseGenre(char genre[12])
{
char answer = '0';
char valid = '1';
do {
cout << "\nChoose a movie genre\n";
cout << "--------------------\n";
cout << "1. Action\n";
cout << "2. Classics\n";
cout << "3. Comedy\n";
cout << "4. Drama\n";
cout << "5. Family\n";
cout << "6. Foreign\n";
cout << "7. Horror\n";
cout << "8. Romance\n";
cout << "9. SciFi\n";
cout << "Enter the number of your choice: ";
cin >> answer;
switch(answer) {
case '1' : strcpy(genre,"Action"); valid = '0'; break;
case '2' : strcpy(genre,"Classics"); valid = '0'; break;
case '3' : strcpy(genre,"Comedy"); valid = '0'; break;
case '4' : strcpy(genre,"Drama"); valid = '0'; break;
case '5' : strcpy(genre,"Family"); valid = '0'; break;
case '6' : strcpy(genre,"Foreign"); valid = '0'; break;
case '7' : strcpy(genre,"Horror"); valid = '0'; break;
case '8' : strcpy(genre,"Romance"); valid = '0'; break;^M
case '9' : strcpy(genre,"SciFi"); valid = '0'; break;
default : cout << "\n\nInvalid choice. Please try again.\n"; break;
}
} while ( valid != '0' );
}
//************************
void Movie::addMovie()
{
char title[80];
char genre[12];
char moviestar1[30];
char moviestar2[30];
int totalNumCopies;
char field[10] = "title";
char valid='1';
int results[Max_Video]; //array to store search results
int count=0; //number of results returned by search
while (valid != '0') {
system("tput clear");
cin.clear();
cin.ignore(100,'\n');
cout << "Enter movie title to add: ";
cin.getline(title,80);
searchMovies(field, title, results, count);
if (count > 0) {
cout << "\n\n" << title << " already exists in the movie database.\n\n";
break;
}
chooseGenre(genre);
cout << "Enter the name of the movie's star: ";
cin.clear(); //flush the buffer
cin.ignore(100,'\n');
cin.getline(moviestar1,30)
cout << "Enter the name of a second star or <ENTER> to skip: ";
cin.getline(moviestar2,30)
cout << "Enter total number of copies: ";
cin >> totalNumCopies;
while (!cin) {
cout << "\nEnter only numbers please.\n\n";
cin.clear();
cin.ignore(100,'\n');
cout << "Enter total number of copies: ";
cin >> totalNumCopies;
}
//save the values into our videos array
strcpy(videos[totalNumMovi
strcpy(videos[totalNumMovi
strcpy(videos[totalNumMovi
strcpy(videos[totalNumMovi
videos[totalNumMovies].tot
videos[totalNumMovies].num
totalNumMovies++; // increment after adding a movie
cout << "\n" << title << " added to the movie database.\n\n";
valid = '0';
}
}
//************************
void Movie::delMovie()
{
char field[10]= "title";
char title[80];
int results[Max_Video];
char valid='1';
int count=0;
while (valid != '0') {
system("tput clear");
if (totalNumMovies == 0) {
cout << "\n\nNo movies in the database. Nothing to delete.\n\n";
break;
}
cout << "Enter movie title to delete: ";
cin.clear();
cin.ignore(100,'\n');
cin.getline(title,80);
searchMovies(field, title, results, count);
if (count > 0) {
videos[results[0]]=videos[
totalNumMovies--;
cout << "\n" << title << " deleted from the movie database.\n\n";
valid='0';
} else {
cout << "\n" << title << " not found in the movie database.\n\n";
break;
}
}
}
//************************
void Movie::userSearch()
{
char field[10];
char searchPattern[80];
int results[100];
char genre[12];
char answer;
char valid='1';
int count=0;
int i;
do {
cout << "\n\n-----------\n";
cout << "Search Menu\n";
cout << "-----------\n";
cout << "Do you want to search:\n\n";
cout << "1. By Movie Title\n";
cout << "2. By Actor/Actress\n";
cout << "3. By Genre\n\n";
cout << "Enter the number of your choice: ";
cin >> answer;
if ((answer == '1') || (answer == '2') || (answer == '3')) {
valid = '0';
}
} while (valid != '0');
if (answer == '1') {
cout << "\n\nEnter a movie title: ";
cin.clear(); // clear buffer to allow for input
cin.ignore(100,'\n');
cin.getline(searchPattern,
strcpy(field,"title");
} else if (answer == '2') {
cout << "\n\nEnter the name of an Actor/Actress: ";
cin.clear();
cin.ignore(100,'\n');
cin.getline(searchPattern,
strcpy(field,"moviestar");
} else if (answer == '3') {
chooseGenre(genre);^M
strcpy(searchPattern,genre
strcpy(field,"genre");^M
}
searchMovies(field, searchPattern, results, count);
if (count > 0) {
//list all elements of videos for search results
cout << "\nSearch results for " << searchPattern << endl << endl;
cout << setw(20) << "Movie" << setw(7) << "Genre" << setw(15) << "Star#1";
cout << setw(15) << "Star#2" << setw(10) << "#copies" << setw(7) << "#avail" << endl;
cout << "-------------------------
for (i=0; i<count; i++) {
cout << setw(20) << videos[results[i]].title;
cout << setw(7) << videos[results[i]].genre;
cout << setw(15) << videos[results[i]].moviest
cout << setw(15) << videos[results[i]].moviest
cout << setw(10) << videos[results[i]].totalNu
cout << setw(7) << videos[results[i]].numAvai
}
} else {
cout << "No records found matching " << searchPattern << endl;
}
}
//************************
void Movie::searchMovies (char field[10], char searchPattern[80], int results[Max_Video], int& count)
{
int i;
if ( compareStrings(field,"titl
if (totalNumMovies > 0) {
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt
results[count] = i;
count++;
}
}
}
} else if ( compareStrings(field,"movi
if (totalNumMovies > 0) {
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt
results[count] = i;
count++;
}
}
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt
results[count] = i;
count++;
}
}
}
} else if ( compareStrings(field,"genr
if (totalNumMovies > 0) {
for (i=0; i < totalNumMovies; i++) {
if (compareStrings(searchPatt
results[count] = i;
count++;
}
}
}
}
}
//************************
void Movie::rentMovie ()
{
char title[80];
char field[10] = "title";
int results[100];
char valid='1';
int count=0;
while (valid != '0') {
system("tput clear");
cin.clear();
cin.ignore(100,'\n');
cout << "Enter movie title to mark as rented: ";
cin.getline(title,80);
results[0]=-1;
searchMovies(field, title, results, count);
if (count >= 0) {
//if copies are available mark as rented
if (videos[results[0]].numAva
videos[results[0]].numAvai
cout << "\n" << title << " marked as rented in the movie database.\n\n";
valid = '0';
} else {
cout << "\n\nAll copies already rented. Cannot mark ";
cout << videos[results[0]].title << " as rented.\n\n";
break;
}
} else {
cout << "\n" << title << " not found in the movie database.\n\n";
break;
}
}
}
//************************
void Movie::returnMovie ()
{
char title[80];
char field[10] = "title";
int results[100];
char valid='1';
int count=0;
while (valid != '0') {
system("tput clear");
cin.clear();
cin.ignore(100,'\n');
cout << "Enter movie title to mark as returned: ";
cin.getline(title,80);
results[0]=-1;
searchMovies(field, title, results, count);
if (count >= 0) {
//if all movies are not accounted for, mark as returned
if (videos[results[0]].numAva
videos[results[0]].numAvai
cout << "\n" << title << " marked as returned in the movie database.\n\n";
valid = '0';
} else {
cout << "\n\nAll copies already accounted for. Cannot mark ";
cout << videos[results[0]].title << " as returned.\n\n";
break;
}
} else {
cout << "\n" << title << " not found in the movie database.\n\n";
break;
}
}
}
//************************
void Movie::listMovies() const
{
int i;
cout << endl << endl;
system("tput clear");
if (totalNumMovies > 0) {
cout << "\nList of all movies in the database\n\n";
cout << setw(20) << "Movie" << setw(7) << "Genre" << setw(15) << "Star#1";^M
cout << setw(15) << "Star#2" << setw(10) << "#copies" << setw(7) << "#avail" << endl;^M
cout << "-------------------------
for (i=0; i<totalNumMovies; i++) {^M
cout << setw(20) << videos[i].title;^M
cout << setw(7) << videos[i].genre;^M
cout << setw(15) << videos[i].moviestar1;^M
cout << setw(15) << videos[i].moviestar2;^M
cout << setw(10) << videos[i].totalNumCopies;^
cout << setw(7) << videos[i].numAvailCopies << endl;^M
}
} else {
cout << "There are no movies in the database.";
}
cout << endl << endl;
}
//************************
int Movie::compareStrings (char string1[80], char string2[80]) {
if( strlen(string1) != strlen(string2)) return 1;
for( int i = 0; i < strlen(string1); ++i ) {
if( toupper(string1[i]) != toupper(string2[i]) ) return 1;
}
return 0;
}
**** end of implementation file Movie.cpp *******
gcc is specific to C programs. When you want to compile C++ program, use g++.
The include paths, library paths, and many other things differ for these two.
When you want to compile a C++ program containing iostream.h using gcc, it will give error saying that the file is not found. Because the file is located at C++ specific include path, which g++ is configured to search for.
So, g++ must solve your problems....
The include paths, library paths, and many other things differ for these two.
When you want to compile a C++ program containing iostream.h using gcc, it will give error saying that the file is not found. Because the file is located at C++ specific include path, which g++ is configured to search for.
So, g++ must solve your problems....
ASKER
Sorry, I should have mentioned I did use g++. Here is the command line that I used
$ g++ -o testoutput Movie-store.cpp
/tmp/cct1iilo.o: In function `main':
/tmp/cct1iilo.o(.text+0x2e ): undefined reference to `Movie::openDBFile()'
/tmp/cct1iilo.o(.text+0x40 ): undefined reference to `Movie::printMainMenu() const'
/tmp/cct1iilo.o(.text+0xb2 ): undefined reference to `Movie::addMovie()'
/tmp/cct1iilo.o(.text+0xc9 ): undefined reference to `Movie::delMovie()'
/tmp/cct1iilo.o(.text+0xe0 ): undefined reference to `Movie::userSearch()'
/tmp/cct1iilo.o(.text+0xf4 ): undefined reference to `Movie::listMovies() const'
/tmp/cct1iilo.o(.text+0x10 8): undefined reference to `Movie::rentMovie()'
/tmp/cct1iilo.o(.text+0x11 c): undefined reference to `Movie::returnMovie()'
/tmp/cct1iilo.o(.text+0x17 a): undefined reference to `Movie::saveDBFile()'
collect2: ld returned 1 exit status
$ g++ -o testoutput Movie-store.cpp
/tmp/cct1iilo.o: In function `main':
/tmp/cct1iilo.o(.text+0x2e
/tmp/cct1iilo.o(.text+0x40
/tmp/cct1iilo.o(.text+0xb2
/tmp/cct1iilo.o(.text+0xc9
/tmp/cct1iilo.o(.text+0xe0
/tmp/cct1iilo.o(.text+0xf4
/tmp/cct1iilo.o(.text+0x10
/tmp/cct1iilo.o(.text+0x11
/tmp/cct1iilo.o(.text+0x17
collect2: ld returned 1 exit status
ASKER
Even using g++ I get the undefined reference errors. Can anybody tell me how to work around this?
The error is because, You are using a library which you are not linking.
So, you have to add this to compiler command. I don't know exactly as to which library you are using.
For example, if your library name is libmyutil.so and it is located at /home/temulus/mylib, then your compiler command will look like this:
$ g++ -o testoutput -I/home/temulus/mylib Movie-store.cpp -lmyutil
I am assuming that you are using OS of unix/linux flavour.
So for your program, replace -lmyutil by your library and the path by the path you are using. As for the path, you have to specify it only when the library file is not located in the standard path.
I think this must work. Give your feedback on this comment....
-Narendra
So, you have to add this to compiler command. I don't know exactly as to which library you are using.
For example, if your library name is libmyutil.so and it is located at /home/temulus/mylib, then your compiler command will look like this:
$ g++ -o testoutput -I/home/temulus/mylib Movie-store.cpp -lmyutil
I am assuming that you are using OS of unix/linux flavour.
So for your program, replace -lmyutil by your library and the path by the path you are using. As for the path, you have to specify it only when the library file is not located in the standard path.
I think this must work. Give your feedback on this comment....
-Narendra
ASKER
Narendra thanks for the response. Yes, I am using SuSe 8.1, which is why I do not like the answer that my program compiles fine under Microsoft Visual C++.
I did not think that I was creating a library. The Movie.h and Movie.cpp files are the specification and implementation files for the Movie class. All of the 'undefined reference' errors mention functions that I define in the Movie class.
I thought that a library was a group of functions and/or classes.
Since I am not using a library that I know of, I do not know what I would specify on the command line that you provided.
Thanks for any help.
I did not think that I was creating a library. The Movie.h and Movie.cpp files are the specification and implementation files for the Movie class. All of the 'undefined reference' errors mention functions that I define in the Movie class.
I thought that a library was a group of functions and/or classes.
Since I am not using a library that I know of, I do not know what I would specify on the command line that you provided.
Thanks for any help.
That means you have Movie-store.cpp and Movie.cpp files. And you are compiling Movie-store.cpp. And the class definitions are present in Movie.cpp.
If what I have written is correct, then change your compiler command to:
g++ -o testoutput Movie-store.cpp Movie.cpp
I think this must solve your problems of linking.....
....Hope you get new errors!:-))
-Narendra
If what I have written is correct, then change your compiler command to:
g++ -o testoutput Movie-store.cpp Movie.cpp
I think this must solve your problems of linking.....
....Hope you get new errors!:-))
-Narendra
ASKER
As you hoped Narenda, we got a new set of errors.
When I ran `g++ -o testoutput Movie-store.cpp Movie.cpp`, I got several hundred lines of the following:
/tmp/ccFYRgFs.o: In function `main':
/tmp/ccFYRgFs.o(.text+0x52 ): undefined reference to `std::cin'
/tmp/ccFYRgFs.o(.text+0x57 ): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
/tmp/ccFYRgFs.o(.text+0x12 e): undefined reference to `std::cout'
/tmp/ccFYRgFs.o(.text+0x13 3): 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/ccFYRgFs.o(.text+0x14 0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccFYRgFs.o(.text+0x14 d): undefined reference to `std::cout'
When I ran `g++ -o testoutput Movie-store.cpp Movie.cpp`, I got several hundred lines of the following:
/tmp/ccFYRgFs.o: In function `main':
/tmp/ccFYRgFs.o(.text+0x52
/tmp/ccFYRgFs.o(.text+0x57
/tmp/ccFYRgFs.o(.text+0x12
/tmp/ccFYRgFs.o(.text+0x13
/tmp/ccFYRgFs.o(.text+0x14
/tmp/ccFYRgFs.o(.text+0x14
ASKER
I realized that I had "using namespace std" in the Movie.cpp,and remembered that this should not be included in a class definition. When I removed this line, I get far less errors:
g++ -o testoutput Movie-store.cpp Movie.cpp
Movie.cpp: In member function `int Movie::openDBFile()':
Movie.cpp:19: `cout' undeclared (first use this function)
Movie.cpp:19: (Each undeclared identifier is reported only once for each
function it appears in.)
Movie.cpp: In member function `void Movie::saveDBFile()':
Movie.cpp:32: `endl' undeclared (first use this function)
Movie.cpp: In member function `void Movie::chooseGenre(char*)' :
Movie.cpp:76: `cin' undeclared (first use this function)
Movie.cpp: In member function `void Movie::userSearch()':
Movie.cpp:237: `setw' undeclared (first use this function)
g++ -o testoutput Movie-store.cpp Movie.cpp
Movie.cpp: In member function `int Movie::openDBFile()':
Movie.cpp:19: `cout' undeclared (first use this function)
Movie.cpp:19: (Each undeclared identifier is reported only once for each
function it appears in.)
Movie.cpp: In member function `void Movie::saveDBFile()':
Movie.cpp:32: `endl' undeclared (first use this function)
Movie.cpp: In member function `void Movie::chooseGenre(char*)'
Movie.cpp:76: `cin' undeclared (first use this function)
Movie.cpp: In member function `void Movie::userSearch()':
Movie.cpp:237: `setw' undeclared (first use this function)
So, do one thing.
Just after you write all the #includes, add the "using namespace std;" line and this should resolve the issue.
You can also try to add std:: before each cout and cin.
-Narendra
Just after you write all the #includes, add the "using namespace std;" line and this should resolve the issue.
You can also try to add std:: before each cout and cin.
-Narendra
ASKER
I now have the using namespace std line back in the Movie-store.cpp, Movie.h, & Movie.cpp. I am back to the following errors:
tmp/ccb6hHBP.o: In function `main':
/tmp/ccb6hHBP.o(.text+0x2e ): undefined reference to `Movie::openDBFile()'
/tmp/ccb6hHBP.o(.text+0x40 ): undefined reference to `Movie::printMainMenu() const'
/tmp/ccb6hHBP.o(.text+0xb2 ): undefined reference to `Movie::addMovie()'
/tmp/ccb6hHBP.o(.text+0xc9 ): undefined reference to `Movie::delMovie()'
/tmp/ccb6hHBP.o(.text+0xe0 ): undefined reference to `Movie::userSearch()'
/tmp/ccb6hHBP.o(.text+0xf4 ): undefined reference to `Movie::listMovies() const'
/tmp/ccb6hHBP.o(.text+0x10 8): undefined reference to `Movie::rentMovie()'
/tmp/ccb6hHBP.o(.text+0x11 c): undefined reference to `Movie::returnMovie()'
/tmp/ccb6hHBP.o(.text+0x17 a): undefined reference to `Movie::saveDBFile()'
collect2: ld returned 1 exit status
I am increasing the points on this because I really would like to know the answer. I did all my programs for this class using gcc 3.2, but because of this error I had to finish this program in Visual C++. I do not particularly like Visual C++, and I am starting the next C++ class soon. I do not want to run into this again, and be forced to borrow a friends computer so that I can use Visual C++.
Any help would be appreciated.
tmp/ccb6hHBP.o: In function `main':
/tmp/ccb6hHBP.o(.text+0x2e
/tmp/ccb6hHBP.o(.text+0x40
/tmp/ccb6hHBP.o(.text+0xb2
/tmp/ccb6hHBP.o(.text+0xc9
/tmp/ccb6hHBP.o(.text+0xe0
/tmp/ccb6hHBP.o(.text+0xf4
/tmp/ccb6hHBP.o(.text+0x10
/tmp/ccb6hHBP.o(.text+0x11
/tmp/ccb6hHBP.o(.text+0x17
collect2: ld returned 1 exit status
I am increasing the points on this because I really would like to know the answer. I did all my programs for this class using gcc 3.2, but because of this error I had to finish this program in Visual C++. I do not particularly like Visual C++, and I am starting the next C++ class soon. I do not want to run into this again, and be forced to borrow a friends computer so that I can use Visual C++.
Any help would be appreciated.
So, this means that the compiler is not seeing the definition of Movie class!
I will try to analyze again. Tell me if I am wrong.
1. You have got 3 files: Movie.cpp, Movie.h and Movie-store.cpp
2. Movie.h contains the definition of Movie class.
3. The compilation command is: gcc Movie.cpp Movie-store.cpp
With this it's giving the above said errors!?
If all the above points are right, I think I need to see the files. Please send it to mail: ssnkumar@rediffmail.com and also mention the complete command you are using to compile the programs. Also, tell me if these file reside in the same directory or different.
I think this should work without much problem....
-Narendra
I will try to analyze again. Tell me if I am wrong.
1. You have got 3 files: Movie.cpp, Movie.h and Movie-store.cpp
2. Movie.h contains the definition of Movie class.
3. The compilation command is: gcc Movie.cpp Movie-store.cpp
With this it's giving the above said errors!?
If all the above points are right, I think I need to see the files. Please send it to mail: ssnkumar@rediffmail.com and also mention the complete command you are using to compile the programs. Also, tell me if these file reside in the same directory or different.
I think this should work without much problem....
-Narendra
Please zip all the files and send:-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
temulus,
I am surprised to see that you have accepted answer, which I had posted before he had posted!:-( See the compilation command I have written as #3 in my previous post:
3. The compilation command is: gcc Movie.cpp Movie-store.cpp
It is doing the same thing with one command, what darlingm has done with 3 commands!!
I think you didn't try out my suggestions...:-( I feel that I was just wasting time by trying to help you out....
OK. Atleast now you are able to compile and execute your programs:-))
-Narendra
I am surprised to see that you have accepted answer, which I had posted before he had posted!:-( See the compilation command I have written as #3 in my previous post:
3. The compilation command is: gcc Movie.cpp Movie-store.cpp
It is doing the same thing with one command, what darlingm has done with 3 commands!!
I think you didn't try out my suggestions...:-( I feel that I was just wasting time by trying to help you out....
OK. Atleast now you are able to compile and execute your programs:-))
-Narendra
ssnkumar,
The confusion is that there were two seperate questions posted.
In my post on 02/24/2003 at 08:13PM, I was trying to let you know that you weren't being jipped with my initial line on the post:
(was posted at 02/24/2003 07:53PM on https://www.experts-exchange.com/questions/20528030/gcc-compilation-error-compiles-fine-under-visual-c-please-help.html) :
I gave temulus the answer at 07:53PM on his other question. On that other question, he asked me to re-post my answer on this question.
I don't think you should feel like you wasted your time. You're right, you can one-line the compile. Doing them seperately only has to make you wait for one cpp file to compile if the other one hasn't been changed. But, if the compile time is small enough that you don't mind, you can do them together.
There are so many great people on this board that identical answers are being given on questions occasionally, while one person is writing another person has submitted. The important thing is an exchange of information.
The confusion is that there were two seperate questions posted.
In my post on 02/24/2003 at 08:13PM, I was trying to let you know that you weren't being jipped with my initial line on the post:
(was posted at 02/24/2003 07:53PM on https://www.experts-exchange.com/questions/20528030/gcc-compilation-error-compiles-fine-under-visual-c-please-help.html) :
I gave temulus the answer at 07:53PM on his other question. On that other question, he asked me to re-post my answer on this question.
I don't think you should feel like you wasted your time. You're right, you can one-line the compile. Doing them seperately only has to make you wait for one cpp file to compile if the other one hasn't been changed. But, if the compile time is small enough that you don't mind, you can do them together.
There are so many great people on this board that identical answers are being given on questions occasionally, while one person is writing another person has submitted. The important thing is an exchange of information.
ASKER
Narendra,
I really appreciate the help and the attention that you gave to my question. In all the research that I did online and all the attemtps to get this to compile correctly, I never tried putting both cpp files on the g++ command line.
Sorry for the confusion by creating a second question. I had wanted to increase the points on the first question, but it would not let me go above 500. Since I wanted an answer I created the second question to increase the total points.
I like your answer because it allows me to compile the program in a single command line, which most likely I shall be doing often. darlingm answered my question first on the second posting, and seems to be a better solution for longer compile times.
Anyway, I have created a new question at the link below, in order to give you points that you deserve. Thank you for your great answer, and your time. Like I said, most likely I will want to compile everything at once, at least in these early stages. Sorry for the confusion.
https://www.experts-exchange.com/questions/20529482/points-for-ssnkumar.html
-tem
I really appreciate the help and the attention that you gave to my question. In all the research that I did online and all the attemtps to get this to compile correctly, I never tried putting both cpp files on the g++ command line.
Sorry for the confusion by creating a second question. I had wanted to increase the points on the first question, but it would not let me go above 500. Since I wanted an answer I created the second question to increase the total points.
I like your answer because it allows me to compile the program in a single command line, which most likely I shall be doing often. darlingm answered my question first on the second posting, and seems to be a better solution for longer compile times.
Anyway, I have created a new question at the link below, in order to give you points that you deserve. Thank you for your great answer, and your time. Like I said, most likely I will want to compile everything at once, at least in these early stages. Sorry for the confusion.
https://www.experts-exchange.com/questions/20529482/points-for-ssnkumar.html
-tem
Sorry, if I added to the confusion....:-)
-Narendra
-Narendra
ASKER
No you didn't add to the confusion. You provided me with an alternative solution.
Thanks for the help.
Thanks for the help.
compile and link a C++ program from the command line, I get
"undefined reference" errors all over the place.
A: Use gpp instead of gcc. gpp knows about the
extra libraries that C++ programs need.
(thanks to the djgpp FAQ -> http://www.rose-hulman.edu/~yerricde/minifaq.txt)
I played around with your code (I use DJGPP which has gcc and gpp). From my knowledge gcc is a C compiler, and you are writing C++ code, therefore it will not work. I ran it to get the same erros as you, and I would get the same errors under when compiled under gcc, however the program was fine under gpp.
This is because gpp is a C++ compiler.
I had a bit of a look, and posted a link above which may explain it better.
Hope this helps.
Cassandra.