Issues with coding and compiling class
Feb 4, 2013 at 3:56am UTC
Hi guys. OK I got another class that is giving me issues. This class has more than one constructor and I think that is what is throwing me off. Here is the coding for the class...
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
//Filter.h
#ifndef filter_H
#define filter_H
#include <string>
using namespace std;
class Filter //Names the class "Resistor".
{
private :
FiltRes:Resistor
FiltCap:Capacitor
double CutOffFrequency;
double minCutOffFrequency;
double maxCutOffFrequency;
double filterTol;
string filterType;
public : //Getter and setter functions.
Filter(Resistor initialR, Capacitor initialC); //constructor 1
Filter(double initialNominalR, double initialToleranceR,
double initialNominalC, double initialToleranceC); //constructor 2
Filter();//constructor 3
void setR(double a);
void setC(double b);
double getR(void );
double getRT(void );
double getC(void );
double getCTvoid);
double getCutOffFrequency(void );
};
#endif
And the cpp 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
//Filter.h
#ifndef filter_H
#define filter_H
#include <string>
using namespace std;
class Filter //Names the class "Resistor".
{
private :
FiltRes:Resistor
FiltCap:Capacitor
double CutOffFrequency;
double minCutOffFrequency;
double maxCutOffFrequency;
double filterTol;
string filterType;
public : //Getter and setter functions.
Filter(Resistor initialR, Capacitor initialC); //constructor 1
Filter(double initialNominalR, double initialToleranceR,
double initialNominalC, double initialToleranceC); //constructor 2
Filter();//constructor 3
void setR(double a);
void setC(double b);
double getR(void );
double getRT(void );
double getC(void );
double getCTvoid);
double getCutOffFrequency(void );
};
#endif
Feb 4, 2013 at 3:57am UTC
Here is the main program...
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
#include <iostream>
#include <fstream>
#include <conio.h>
#include "Filter.h"
using namespace std;
int main (void )
{
double resNom, resTol, capNom, capTol;
cout << "Testing default constructor and all get methods" << endl
<< "all values should be zero for filter f1:" << endl
<< endl;
Filter f1;
cout << "R = " << f1.getR( ) << endl
<< "R tol = " << f1.getRTolerance( ) << endl
<< "C = " << f1.getC( ) << endl
<< "C tol = " << f1.getCTolerance( ) << endl
<< "fc = " << f1.getCutOffFrequency( ) << endl << endl;
cout << "Testing setR and setC methods with filter f1:" << endl
<< "Expect these values:" << endl
<< "R = 150" << endl
<< "R tolerance = 20" << endl
<< "C = 0.5" << endl
<< "C tolerance = 30" << endl
<< "fc = 2122.066" << endl << endl;
f1.setR(150, 20);
f1.setC(0.5, 30);
cout << "R = " << f1.getR( ) << endl
<< "R tol = " << f1.getRTolerance( ) << endl
<< "C = " << f1.getC( ) << endl
<< "C tol = " << f1.getCTolerance( ) << endl
<< "fc = " << f1.getCutOffFrequency( ) << endl << endl;
//Save filter information to filter.txt in order. Filter.txt should be in the same dir as main.cpp
ofstream outfile("filter.txt" );
outfile <<f1.getR()<<endl
<<f1.getRTolerance()<<endl
<<f1.getC()<<endl
<<f1.getCTolerance()<<endl;
cout << "File written." << endl;
cout << "Testing parameterized constructor" << endl
<< "Creating f2 with these values:" << endl
<< "R = 27" << endl
<< "R tolerance = 10" << endl
<< "C = 0.03" << endl
<< "C tolerance = 50" << endl
<< "fc = 196487.584" << endl << endl;
Resistor r1(27, 10, "r1" );
Capacitor c1(0.03, 50);
Filter f2(r1, c1);
cout << "R = " << f2.getR( ) << endl
<< "R tol = " << f2.getRTolerance( ) << endl
<< "C = " << f2.getC( ) << endl
<< "C tol = " << f2.getCTolerance( ) << endl
<< "fc = " << f2.getCutOffFrequency( ) << endl << endl;
cout <<"Testing file read into filter 2:" <<endl;
ifstream infile("filter.txt" );
infile >> resNom
>> resTol
>> capNom
>> capTol;
f2.setR(resNom, resTol);
f2.setC(capNom, capTol);
cout << "R = " << f2.getR( ) << endl
<< "R tol = " << f2.getRTolerance( ) << endl
<< "C = " << f2.getC( ) << endl
<< "C tol = " << f2.getCTolerance( ) << endl
<< "fc = " << f2.getCutOffFrequency( ) << endl << endl;
cout << "Press ENTER key to exit" << flush;
cin.ignore( );
return 0;
}
Feb 4, 2013 at 4:17am UTC
Is your program compiling? I would imagine the error messages are telling you important things.
Try putting various "couts" in your class constructors, so you can see where things are. You can also overload the << operator to make life a little easier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Rectangle
{
int x;
int y;
public :
Rectangle(int a, int b) : x(a), y(b) {}
friend ostream& operator <<(ostream& out, const Rectangle& rect)
{ return out << "Rect: ( " << rect.x << ", " << rect.y << " ), Area:" << rect.x * rect.y; }
};
int main(void )
Rectangle myRect(3,4);
cout << myRect << endl;
return 0;
}
Last edited on Feb 4, 2013 at 2:36pm UTC
Feb 4, 2013 at 5:07am UTC
No its not compiling right now. I know my coding is off I'm just unsure how to fix it.
Feb 4, 2013 at 6:20am UTC
Can you provide the error messages? And perhaps I missed something but did you actually write the class functions?
Last edited on Feb 4, 2013 at 6:22am UTC
Topic archived. No new replies allowed.