Header and source files implementing classes.

I am trying to get a header file and source file to intergrate into a main c++ file.The class is in the header file, the functions are in the source file. I cannot get the source file to compile due to errors with my function prototypes. I am sure this is an easy fix I am just not able to quite understand the process for using the class by the functions. I dont know how to properly copy code to a forum. If someone can tell me how I will repost this neatly. I need alot of help thank you in advance.

header file.

#ifndef cResistor_H
#define cResistor_H

class cResistor //generates class for Resistor data
{
public:
cResistor();
cResistor( double m_dResValue, double m_dTolerance); //
void DisplayResistor();
~cResistor( );

private:

double m_dResValue;
double m_dTolerance;
double m_dMinResistance;
double m_dMaxResistance;

};
#endif


Source file

#include "Resistor.h" //Header file to use to access information.
#include <iostream> //Used for O/P for the functions
#include <iomanip> //Used for spacing

using namespace std;

void cResistor::DisplayResistor(void); //Function prototype for displaying resistor.

cResistor::cResistor(double m_dResValue, double m_dTolerance); //Function prototype for resistor calculations.




void cResistor:: DisplayResistor(void) //Function declaration to display resistor values.
{
cout << fixed << setprecision (2);
cout<<"Values for ResistorOne are: "<<endl;
cout<<"Resistor Nominal Value = "<<m_dResValue<<endl;
cout<<"ohmsResistorTolerance = "<<m_dTolerance*100<<" %"<<endl;
cout<<"Mininimum Resistance = "<<m_dMinResistance<<" ohms"<<endl;
cout<<"Maximum Resistance = "<<m_dMaxResistance<<" ohms"<<endl;
cout<<endl;
};

cResistor::cResistor(double m_dResValue, double m_dTolerance ) //Function declaration for resistor calculations
{
m_dResValue = 1000.0;
m_dTolerance = 0.10;
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
cout <<endl<< "Default Constructor Called:" <<endl;
};
//End Main Function


Main program

#include <iostream>
#include <iomanip>
#include "Resistor.h"
cResistor; //calls Class into main program

using namespace std;

int main(void)
{
cResistor::cResistor(); //constructor to for functions DisplayResistor and cResistor Calculations

system("pause");

}


First of all, click the code tag button with the <> on the right side to show the code tag. Type ur codes within the tags.

Second, your header file is fine. You have two constructor prototypes, a destructor and a function prototype.
1
2
3
4
cResistor();
cResistor( double m_dResValue, double m_dTolerance);
void DisplayResistor();
~cResistor( );


On your source file you should not put ANY prototypes. Remove the prototypes from the source file.

Thirdly, you cannot do that in your main method cResistor::cResistor();. You have to declare a pointer to the object.
Try
cResistor* resistor = new cResistor();
But your source file does not have any implementation for this constructor either. So it will give error unless you implement this in your source file
1
2
3
cResistor::cResistor()
{
}


Fourthly, (but i should put this suggestion first), you should seriously READ about Object Oriented programming or Classes on C++.
Read this
http://www.cplusplus.com/doc/tutorial/classes/
Thanks greenscar for your tips and direction. I reviewed the class tutorial and with a little prodding from another student in my class I finally cleared up my errors and got the expected output. I finally had the AHa moment last night during the lecture review and I think I got the basics down with the implementation. Now I just need a little more practice. Thanks again for your time.

Topic archived. No new replies allowed.