Problem with this code for an example of a Class.

I am using Visual Studio, 2015, and need help with this C++ code. The aim is to show a basic example of a Class using it's own Header file and to return three values. This code we are not using #pragma, is that an issue?

I put the error message below.

Can anyone tell me why this is not Compiling?

BMI.h below:

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
//#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

#ifndef BMI_H
#define BMI_H

class BMI {
public:
	// default constructor just to set member variables to null states
	BMI();

	
	
	BMI(string, int, double);

	//destructor
	~BMI();

	//Below are the accesor functions which return member variables
	//typicaly we use get in front as a universal way to identify it

	string getName() const;
	int getHeight() const;
	double getWeight() const;


private:
	//Member variables
	string newName;
	int newHeight;
	double newWeight;


};

#endif 


BMI.cpp below:
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
#include "BMI.h"
 
//Below is default contructor coresponding code.

BMI::BMI()
{
	newHeight = 0;
	newWeight = 0.0;

}
//below is overload coresponding code.

BMI::BMI(string name, int height, double weight)
{
	newName = name;
	newHeight = height;
	newWeight = weight;

}
BMI::~BMI()
{
	//leave empty
}

//below coresponding declaration for accesors

string BMI::getName() const 
{
	return newName;
}
int BMI::getHeight() const 
{
	return newHeight;
}

double BMI::getWeight() const
{
	return newWeight;
}


Main below:
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
//accesor functions are used to access information from member variables
#include "stdafx.h"
#include <iostream>
#include <string>
#include "BMI.h"

using namespace std;


int main()
{
	string name;
	int height;
	double weight;
		cout << "Enter your name: ";
	cin >> name;
	cout << "Enter your height in inches: ";
	cin >> height;
	cout << "Enter your weight in pounds: ";
	cin >> weight;

	//BMI Student_1;   //the object uses the default constructor
	//below if you want to also use overload constructor 

	BMI Student_1(name, height, weight);

	cout << endl << "Patients name: " << Student_1.getName() << endl <<
		"Height: " << Student_1.getHeight() << endl <<
		"weight: " << Student_1.getWeight() << endl;

    return 0;
}

Severity Code Description Project File Line Suppression State
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? BMI.main c:\users\logan\documents\visual studio 2015\projects\bmi.main\bmi.main\bmi.cpp 37

Last edited on
I do not know how to add colour tags

Place your code within [code] [/code] tags. For example, the input
[code]int main() { std::cout << "Hello World\n"; }[/code]
Produces
int main() { std::cout << "Hello World\n"; }

---
Can anyone tell me why this is not Compiling?

Most likely, if you post the error messages!


Last edited on
Sorted, all I needed to do was go into Project, options> and click C++ >
pre-compiled header and select "Not using prec-compiled headers."
Topic archived. No new replies allowed.