In my programming course, we are using cygwin with gcc compiler. I understand that I have to compile each source before I can compile the driver program.
I have 3 files, falconex.h, falconex.cpp, prog5.cpp.
falconex.h compiles fine, but when I compile falconex.cpp, I get an "undefined reference to winmain error. Also, when I try to compile the driver file (prog5.cpp), I receive an error that each of my member functions are undefined.
Here are the three files
falconex.h(header file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#ifndef FALCONEX_H
#define FALCONEX_H
#include <iostream>
#include <string>
using namespace std;
class FalconEx
{
private:
string gameTitle;
const int maxVolume, minVolume;
bool powerStatus;
int curVolume;
public:
FalconEx();
void togglePower();
void loadGame(string);
void unloadGame();
void raiseVolume();
void lowerVolume();
void play();
};
#endif
|
falconex.cpp(implementation file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <iostream>
#include <string>
using namespace std;
#include "falconex.h"
/*
This function is the constructor for the FalconEx class.
The string 'gameTitle' is initialized to an empty string, power is set to OFF,
and the curVolume variable is set to the average of the max and min values,
which are initialized by the member initialization list.
*/
FalconEx::FalconEx(): maxVolume(6), minVolume(0)
{
gameTitle = "";
powerStatus = false;
curVolume = (maxVolume+minVolume)/2;
}
/*
The togglePower function checks the current status of the bool
operator for powerStatus. If its false(off) the powerStatus will
be set to true, and vice versa for true(on). A message will
be displayed for either case.
*/
void FalconEx::togglePower()
{
if (powerStatus == false)
{
powerStatus = true;
cout << "System turned ON." << endl;
}
else if (powerStatus == true)
{
powerStatus = false;
cout << "System turned OFF." << endl;
}
}
/*
The loadGame function will have the game name as a parameter.
If the power is turned on, the priv. member data of 'gameTitle'
is set back to the empty string.
*/
void FalconEx::unloadGame()
{
if(powerStatus == true)
{
if (gameTitle == "")
cout << "No game has been loaded." << endl;
else
gameTitle == "";
}
}
/*
The raiseVolume and lowerVolume function will check to see if the volume level is already
at max/min. If so, an error message will be displayed. If not, the current
volume will be modified by 1
*/
void FalconEx::raiseVolume()
{
if(powerStatus == true)
{
if (curVolume == maxVolume)
cout << "Volume at maximum" << endl;
else
curVolume++;
}
else
cout << "System not powered." << endl;
}
void FalconEx::lowerVolume()
{
if(powerStatus == true)
{
if (curVolume == minVolume)
cout << "Volume at minimum" << endl;
else
curVolume--;
}
else
cout << "System not powered." << endl;
}
/*
The play function checks to see if the system is ON. If so,
the code checks to see if the string is empty and will
display an error message accordingly. If the string is
not empty, the current game loaded will be displayed
as played.
*/
void FalconEx::play()
{
if (powerStatus == true)
{
if (gameTitle == "")
cout << "No game has been loaded." << endl;
else
cout << gameTitle << " being played." << endl;
}
else
cout << "System not powered." << endl;
}
|
prog5.cpp(driver file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <string>
using namespace std;
#include "falconex.h"
int selection;
FalconEx object;
string title;
int main()
{
cout << "Welcome to FalconEx 2014! Enter a command number to begin simulation." << endl << endl
<< "0 - Toggle power" << endl
<< "1 - Load a game" << endl
<< "2 - Unload a game" << endl
<< "3 - Raise the volume" << endl
<< "4 - Lower the volume" << endl
<< "5 - Play a game (load game first)" << endl
<< "6 - Quit FalconEx simulation" << endl;
do{
cin >> selection;
switch(selection)
{
case 1: object.togglePower();
break;
case 2: cout << "Enter the game you wish to play." << endl;
cin >> title;
object.loadGame(title);
break;
case 3: object.unloadGame();
break;
case 4: object.raiseVolume();
break;
case 5: object.lowerVolume();
break;
}//end switch
}while(selection != 6);
cout << "Thank you for using FalconEx!" << endl;
}
|
To compile these files I type these lines:
g++ falconex.h -o header
g++ falconex.cpp -o implement
g++ prog5.cpp -o driver
The second two lines do not work and I cannot figure out what to do!
Any help is appreciated.