Compiling a program with classes?

Hey guys,

I'm fairly new to programming, and in my C++ class, we're currently working on classes. I have a program due on tuesday, and I began writing it yesterday. I thought I was almost done, but I'm stuck trying to actually run it. I have followed the structure of a few example programs our professor put up, so I don't think theres anything too wrong with what I'm doing. But when I try to compile using g++ weight.cpp driver.cpp or flipping the two files around, I still get a massive amount of compiler errors.

The class is "Weight". I have 3 total files - a driver program (main program) which I just started writing, the weight.cpp file (which has all the function definitions, utility functions, etc) and a weight.h file (class header file with the function members, private/public section, etc). I may just be royally screwing up the code, so if you guys could help me debug it, I'd appreciate it greatly.

driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "weight.h"
#include <iostream>

using namespace std;
#include "weight.h"

main ()
{
	Weight idealweight;

	idealweight.set(160, 'L');
	cout << idealweight << endl;
}


weight.cpp
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
/*-- Weight.cpp------------------------------------------------------------
 
   This file implements the Weight member functions.

-------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#include "Weight.h"

/** Utility functions / Prototypes **/

double toKilos (double weight, char unit);
double toPounds (double &weight, char &unit);

//Definition of default constructor

Weight::Weight()
	: myHeavy(120.0), myUnit('L')
{
}

//Definition of explicit value constructor

Weight::Weight(double initHeavy, char initUnit){
	if(initHeavy > 0 && (initUnit == 'L' || initUnit == 'K'){ 
		myHeavy = initHeavy;
		myUnit = initUnit;
	} else
		cerr << "Invalid initial values. Sorry." <<endl;
}

// Definition of getHeavy

unsigned Weight::getHeavy() const {
	return myHeavy;
} 

//Definition of getUnit

unsigned Weight::getUnit() const {
	return myUnit;
}

// Definition of the display function 

void Weight::display(ostream & out) const
{
out << myHeavy << " " << myUnit;
}

// Definition of operator<<()
ostream & operator<<(ostream & out, const Weight &w)
{
  w.display(out);
  return out;
}

// Definition of set function

void Weight::set(unsigned weight, char unit)
{   
//Check class invariants 
	if ((unit == 'L' || unit == 'K') && weight > 0){
		myHeavy = weight;
		myUnit = unit;
	} else
		cerr << "WARNING: Unable to set weight to these values. " <<endl;
}

// Relational Operators

bool operator<(const Weight &w1, const Weight &w2){
	return w1.getHeavy() < w2.getHeavy();
}

bool operator>(const Weight &w1, const Weight &w2){
	return w1.getHeavy() > w2.getHeavy();
}

bool operator!=(const Weight &w1, const Weight &w2){
	return w1.getHeavy() != w2.getHeavy();
}

bool operator==(const Weight &w1, const Weight &w2){
	return w1.getHeavy() == w2.getHeavy();
}

// Definitions of Ultility Functions
double toKilos(double weight, char unit){      //converts from pounds to Kilos
	myUnit = 'K';
	return (weight * .4536);
}

double toPounds(double weight, char unit){      //Converts from kilos to pounds
	myUnit = 'L';								//Changes unit to L for pounds
	return (weight / .4536);                    //Returns converted weight
}


Weight.h
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
/*-- Weight.cpp------------------------------------------------------------
 
   This file implements the Weight member functions.

-------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

#include "Weight.h"

/** Utility functions / Prototypes **/

double toKilos (double weight, char unit);
double toPounds (double &weight, char &unit);

//Definition of default constructor

Weight::Weight()
	: myHeavy(120.0), myUnit('L')
{
}

//Definition of explicit value constructor

Weight::Weight(double initHeavy, char initUnit){
	if(initHeavy > 0 && (initUnit == 'L' || initUnit == 'K'){ 
		myHeavy = initHeavy;
		myUnit = initUnit;
	} else
		cerr << "Invalid initial values. Sorry." <<endl;
}

// Definition of getHeavy

unsigned Weight::getHeavy() const {
	return myHeavy;
} 

//Definition of getUnit

unsigned Weight::getUnit() const {
	return myUnit;
}

// Definition of the display function 

void Weight::display(ostream & out) const
{
out << myHeavy << " " << myUnit;
}

// Definition of operator<<()
ostream & operator<<(ostream & out, const Weight &w)
{
  w.display(out);
  return out;
}

// Definition of set function

void Weight::set(unsigned weight, char unit)
{   
//Check class invariants 
	if ((unit == 'L' || unit == 'K') && weight > 0){
		myHeavy = weight;
		myUnit = unit;
	} else
		cerr << "WARNING: Unable to set weight to these values. " <<endl;
}

// Relational Operators

bool operator<(const Weight &w1, const Weight &w2){
	return w1.getHeavy() < w2.getHeavy();
}

bool operator>(const Weight &w1, const Weight &w2){
	return w1.getHeavy() > w2.getHeavy();
}

bool operator!=(const Weight &w1, const Weight &w2){
	return w1.getHeavy() != w2.getHeavy();
}

bool operator==(const Weight &w1, const Weight &w2){
	return w1.getHeavy() == w2.getHeavy();
}

// Definitions of Ultility Functions
double toKilos(double weight, char unit){      //converts from pounds to Kilos
	myUnit = 'K';
	return (weight * .4536);
}

double toPounds(double weight, char unit){      //Converts from kilos to pounds
	myUnit = 'L';								//Changes unit to L for pounds
	return (weight / .4536);                    //Returns converted weight
}
The first problem you are likely running into is that you are missing header guards around weight.h.

http://en.wikipedia.org/wiki/Include_guard
You're right.

I added the ifndef WEIGHT and endif header guards. That' what you're referring to, right?

I also realized I forgot to add a function definition for inputs, which I've done as so in my weight.cpp file:


// Definition of the read function

void Weight::read(istream &in)
{
unsigned heavy; //local variables to check for class invariants before putting in members
char unit;

in >> heavy >> unit;
//Check class invariants
if(heavy > 0 && (unit == 'L' || unit == 'K')){
myHeavy = heavy;
myUnit = unit;
}
else
cerr << "Invalid inputs"
}
//Definition of operator>>()
istream & operator>>(istream &in, Weight &w)
{
w.read(in);
return in;
}


Wow, I just potentially did the stupidest mistake ever. In transferring over my files from my main computer to my laptop, my header file got replaced with .cpp (which is why they are both identical here). WOOPS!

I feel like a total idiot. when it included the header file, it would just include another copy of itself :P.

Seems to be better now, just got a few compiler errors that I can probably work through.

Thanks!
Topic archived. No new replies allowed.