[solved] i need help, getting :error C2512:
Hi i have a problem with this program and I would like someone that can explain to me a beginner in C++, what I can do. Please understand that English isn't my first language, so be direct to the problem please not tech stuff without explanation of what you mean( thats where i get lost ). The program have a header file and two source files.
code:
//header file
#include <string>
using std::string;
class ConcernedDriver
{
public:
ConcernedDriver( int filling );
~ConcernedDriver();
void displayMessage();
void determineGasAverage();
private:
int kfilling;
};
// first cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed; // ensure that decimal point is displayed
#include <iomanip> //parqametized stream manipulators
using std::setprecision;
#include "ConcernedDriver.h"
ConcernedDriver::ConcernedDriver( int filling )
{
kfilling = filling;
}
ConcernedDriver::~ConcernedDriver()
{
}
void ConcernedDriver::displayMessage()
{
cout << "This is homework 4.13 page 178"
<< endl;
}
void ConcernedDriver::determineGasAverage()
{
int miles;
int gas;
double MPGtankful;
double totalMpg;
int gasCounter;
miles = 0;
gas = 0;
totalMpg = 0;
gasCounter = 0;
cout << "Enter the miles used (-1 to quit): ";
cin >> miles;
while (miles != -1)
{
gasCounter = gasCounter + 1;
cout << "Enter the miles used (-1 to quit): ";
cin >> miles;
}
if ( gas != 0 )
{
cout << "Enter gallons: ";
cin >> gas;
MPGtankful = static_cast< double >( miles ) / gas ;
totalMpg = totalMpg + MPGtankful;
//display total and average ( with two digits of precision)
cout << "\nMPG this tankful: " << MPGtankful << endl;
cout << "Total MPG: " << setprecision( 6 ) << fixed << totalMpg
<< endl;
}
else //no gas were entered
cout << "No gas were entered" << endl;
}
//second cpp
#include "ConcernedDriver.h"
int main()
{
ConcernedDriver myConcernedDriver();
myConcernedDriver.displayMessage(); /* this is where the problem is */
myConcernedDriver.determineGasAverage();
return 0;
}
VS 2003 tells me the problem is in the second cpp but i dont have any idea of how to resolve this problem. Now the problem seems to be with the constructor (again). Thanks to ToohrVyk for helping me with my other problem.
[Edited by - Greywhitewolf on October 28, 2007 4:59:28 PM]
ConcernedDriver myConcernedDriver(); is a function declaration. ConcernedDriver myConcernedDriver; is a variable definition.
Thanks for the reply ToohrVyk but now i have problems with the constructor again, it says error C2512: 'ConcernedDriver' : no appropriate default constructor available. Same thing it says before. I'm totally block when it comes to constructors I guess.
Quote:Original post by Greywhitewolf
Thanks for the reply ToohrVyk but now i have problems with the constructor again, it says error C2512: 'ConcernedDriver' : no appropriate default constructor available. Same thing it says before. I'm totally block when it comes to constructors I guess.
Well, there is no default constructor for your ConcernedDriver class. The only way to construct a ConcernedDriver is ConcernedDriver( int filling ); which requires an integer argument.
You can either define a default constructor, or provide the correct value to the integer-argument constructor:
ConcernedDriver myConcernedDriver(100);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement