Creating a .h file

Help,

I am a beginner with C++, taking a class right now. The lab this week is to create a user defined class and have it accesses in a seperate .h header file from the main.

I think I'm finding my way through it, but I'm getting a complie error that makes no sense to me:

1> Resistor.cpp
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(11): error C2146: syntax error : missing ';' before identifier 'resistor'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(14): error C2061: syntax error : identifier 'string'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(20): error C2146: syntax error : missing ';' before identifier 'getResName'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(20): warning C4183: 'getResName': missing return type; assumed to be a member function returning 'int'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(21): error C2061: syntax error : identifier 'string'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(22): error C2061: syntax error : identifier 'string'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(23): error C2061: syntax error : identifier 'string'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(29): error C2061: syntax error : identifier 'string'
1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\resistor.h(30): error C2065: 'res1' : undeclared identifier

//

Here is the first portion of the .h 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
/
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <math.h>
#include <string>

class CResistor	{

	double res1, res2, res3, percentage, Nominal, Tolerance;
	bool tf;
	string resistor;

public:
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <math.h>
#include <string>

class CResistor	{

	double res1, res2, res3, percentage, Nominal, Tolerance;
	bool tf;
	string resistor;

public:
/



Any help would be greatly appriciated!
You only need the #includes one time in a header file. Second, there's no need to declare the class twice with the same stuff. But here's what I did for you for defining the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CRESISTOR_H	// Include this so the class is not called more than once.
#define CRESISTOR_H     // Also include the define.

#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <math.h>
#include <string>
using namespace std;	// You forgot this

class CResistor	
{
	private:  // You forgot to declare private (or protected if that's your case)
		double res1, res2, res3, percentage, Nominal, Tolerance;
		bool tf;
		string resistor;

	public:
	// Your public definitions.

};	// Don't forget the semicolon
#endif	// Gotta include this at the end. 

Last edited on
Thanks very much, I have another question if you don't mind. For some reason in main when I try to declare an object for the class CResistor it gets an error.

Here is the class code now
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
#ifndef CRESISTOR_H	
#define CRESISTOR_H 

#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <math.h>
#include <string>

using namespace std;

class CResistor	{

	double res1, res2, res3, tolerance, Nominal;
	bool fnDisplay;
	string resistor;

public:
	CResistor (double, double, string, bool); 
	void resObj (double, bool);
	double getNominal (bool);
	void setNominal (double,bool);
	double getTolerance (double);
	void setTolerance (double, bool);
	string getResName (bool);
	void Res1 (double, double, string, bool);
	void Res2 (double, double, string, bool);
	void Res3 (double, double, string, bool);
	void DisplayResistance(double,double, double, bool );
};
#endif 


Here is the peice of main where I am trying to declare " resObj " and the error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void main(void)
{

 //Control variable to display function names when called
 bool fnDisp = false;
 CResistor resObj;
 CResistor Res1(4700.0, 0.10, "Res1", fnDisp);
 CResistor Res2(330.0, 0.10, "Res2", fnDisp);
 CResistor Res3(10000.0,0.10, "Res3", fnDisp);

 DisplayResistance(Res1, fnDisp);
 DisplayResistance(Res2, fnDisp);
 DisplayResistance(Res3, fnDisp);
  
 EnterResistance(Res1, fnDisp);
 DisplayResistance(Res1, fnDisp);

 system("pause");
}

1>c:\users\tom\documents\school\comp 220\week 2\lab\lab 2.2\lab 2.2\my first class.cpp(69): error C2512: 'CResistor' : no appropriate default constructor available


??
Try CResistor(); for your default constructor. You're also going to need your implementation file (functions). Example would be like...

1
2
3
4
void CResistor::DisplayResistance(double, double, double, bool)
{
//your function code.
}


Oh and don't forget the #include "CResistor.h" for every file.
Last edited on
1
2
3
4
void CResistor::DisplayResistance(double, double, double, bool)
{
//your function code.
}


Does that go in the .h file or the .cpp?
void main() ?!?!?!!??!!? fix that please...
When you declare a CResistor you need to construct it with the parameters you gave it

No a .h file is separate from a .cpp file
in the .cpp file you need to
 
#include "CResistor.h" //or whatever you named it 


CResistor (double, double, string, bool);

so in main you need

 
CResistor resObj(double, double, string, bool); //Replace types with the initial values you want 


p.s. and please use
1
2
3
4
5
6
int main()
{
......
.....
return 0;
}
Last edited on
The void main (void) was in there from my professor. What gets you so excited about it?
New convention. Windows will let you use void main, but unix/linux will not. So int main() works in both.

I am not really that excited though. There are much more exciting things in life :)
Last edited on
Alright so I went from what I thought was a rudamentry understanding to confusion. I can't declare " resObj " as an object? Like int x or in this case " CResistor resObj; ". I must declare it as a function; am I understanding this correctly?
Last edited on
no, you are declaring it as an object, but your object has a constructor that takes parameters.

so the way you currently have it declared
CResistor resOBJ(3.4, 2.7, "poop, 1);

resObj is a type just like x in int x.

if you do not want to initialize it with parameters then your constructor (in your class) should just be
CResistor(); //Add this to your class to allow a CResistor object that takes no parameters to construct

The way you have res1, res2, res3 are declared properly
Last edited on
I noticed you have void resObj (double, bool); in your class.

when you declare a CResistor object then you can call something like res1.resObj(param1, param2);

resObj is a function in your class
Ok sorry, I now noticed what your misunderstanding is

http://www.cplusplus.com/doc/tutorial/classes/

go there and start from scratch with classes. After a little practice with some simple stuff you should get it no problem.
Another piece of advice: don't use any namespaces in a header, as this forces anything using the header to use that namespace.
Thanks to all of you.... i need this type of information... :)
1
2
3
4
void CResistor::DisplayResistance(double, double, double, bool)
{
//your function code.
}
will go in a separate cpp file, so you'll have 3 files total: a header, implementation, and main. Why 3 instead of just 1 file? Cause the code can/will get big and its better readability.
I finally figured out the answer to my question. There are two kinds of constructors, default Constructor::Constructor {} and a constructor with parameters Constructor::Constructor (int, double, bool, ...... ect {}

The default will have a single object like Constructor Tom;
The other will have a single object with one or more parameters like Constructor Yom (double, int);
Topic archived. No new replies allowed.