Hello I was making a program called englishweight and it has three main components englishweight.h which is a class declaration, englishweight_def.cpp which defines the public member fucntions, and EW_driver which uses the class to calculate what to do with the englishweight and preforms very basic operations. My program works WITHOUT englishweight_def.cpp when i define the functions within the englishweight.h class but gives me 23 errors when i try and use englishweight_def.cpp!!! Of those errors it tries to make me give the constructor a type and when i do it gives me 25 errors.
Here is the source code:
englishweight.h

class englishweight{
private:
	float number;
	float eng_weight;
 //not englishweight because IDE thinks it is constructor
public: 
	englishweight();
	/*{
		number=0;
		eng_weight=0;
	};*/
	float getnumber();
	/*{
		return number;
	};*/
	float getweight();
	/*{
		return eng_weight;
	};*/
	void def_number(float defn);
	/*{
		number=defn;
	};*/
	void def_weight(float defw);
	/*{
		eng_weight=defw;
	};*/
};

Englsihweight_def.h:

#include"englishweight.h"
#include<iostream>
englishweight::englishweight()
{
number=0;
eng_weight=0;
};
float englishweight::getnumber()
{
return number;
};
float englishweight::getweight()
{
return eng_weight;
};
void englishweight::def_number(float defn)
{
number=defn;
};
void englishweight::def_weight(float defw)
{
eng_weight=defw;
};

EW_driver.cpp:'

#include<iostream>
#include"Englishweight.h"
#include"englishweight_def.cpp"
using namespace std;
int main()
{
float defw=0; //define float eng_weight 
float defn=0;//define number
float defo=0;//define ounces(part of defw seen later)
class englishweight testing;
cout<<"Welcome to Englishweight V 1.0!"<<endl;
system("pause");
system("cls");
cout<<"Please input a weight-pounds then ounces."<<endl<<"Pounds: ";
cin>>defw;
cout<<endl<<"Ounces: ";
cin>>defo;
cout<<defo;
//system("cls");
cout<<"Now enter a number."<<endl<<"number: ";
cin>>defn;
if(defo!=0)
{
cout<<endl<<"@@@@@@@@@@"<<endl<<"defo before: "<<defo;
cout<<endl<<"defw before: "<<defw;
defo=(defo/16.0); //convert ounces to pounds (wats the 16, 16.0 difference?)
defw=(defw+defo); //add the converted weight to the ounces  to get the final eng_weight
cout<<endl<<"defo after: "<<defo;
cout<<endl<<"defwafter: "<<defw<<endl<<"@@@@@@@@@"<<endl;
}
testing.def_number(defn); //use def_number and def_weight functions to define eng_weight 
testing.def_weight(defw);
cout<<"***************"<<endl<<"Englishweight: "
<<testing.getweight()<<endl<<"Number: "<<testing.getnumber()<<endl<<"defw: "<<defw<<endl<<"defn: "<<defn<<endl<<"defo: "<<defo<<endl<<"***************"<<endl;
cout<<endl<<"Englishweight + Englishweight is: "<<( (testing.getweight() )*2);
cout<<endl<<"Englishweight - Englishweight is: "<<"0";      	//will always be 0 so no need to use processing power
cout<<endl<<"Englishweight / Englishweight is: "<<"1";			//will always be 1 so no need to use processing powercout<<endl<<"Englishweight * number is: "<<( (testing.getweight()) * (testing.getnumber()) );
cout<<endl<<"Number * Englishweight is: "<<( (testing.getnumber()) * (testing.getweight()) );
cout<<endl<<"Englishweight / number is: "<<( (testing.getweight()) / (testing.getnumber()) );
system("pause");
return 0;
}

Recommended Answers

All 14 Replies

You compile and link .cpp files, not include it like a header.
Two, repost with the indented version in CODE TAGS, so people can read it.

i still get 12 errors:

1>------ Build started: Project: asignment_2, Configuration: Debug Win32 ------
1>Compiling...
1>englishweight_def.cpp
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(2) : error C2653: 'englishweight' : is not a class or namespace name
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(3) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(4) : error C2065: 'number' : undeclared identifier
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(5) : error C2065: 'eng_weight' : undeclared identifier
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(6) : warning C4508: 'englishweight' : function should return a value; 'void' return type assumed
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(7) : error C2653: 'englishweight' : is not a class or namespace name
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(9) : error C2065: 'number' : undeclared identifier
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(11) : error C2653: 'englishweight' : is not a class or namespace name
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(13) : error C2065: 'eng_weight' : undeclared identifier
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(15) : error C2653: 'englishweight' : is not a class or namespace name
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(17) : error C2065: 'number' : undeclared identifier
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(19) : error C2653: 'englishweight' : is not a class or namespace name
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\englishweight_def.cpp(21) : error C2065: 'eng_weight' : undeclared identifier
1>EW_driver.cpp
1>c:\users\steffen holm\documents\visual studio 2008\projects\asignment_2\asignment_2\ew_driver.cpp(26) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>Generating Code...
1>Build log was saved at "file://c:\Users\Steffen Holm\Documents\Visual Studio 2008\Projects\asignment_2\asignment_2\Debug\BuildLog.htm"
1>asignment_2 - 12 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

englishweight.h:

class englishweight{
private:
	float number;
	float eng_weight;
 //not englishweight because IDE thinks it is constructor
public: 
	englishweight();
	/*{
		number=0;
		eng_weight=0;
	};*/
	float getnumber();
	/*{
		return number;
	};*/
	float getweight();
	/*{
		return eng_weight;
	};*/
	void def_number(float defn);
	/*{
		number=defn;
	};*/
	void def_weight(float defw);
	/*{
		eng_weight=defw;
	};*/
};

englishweight_def.cpp:

englishweight::englishweight()
{
number=0;
eng_weight=0;
};
float englishweight::getnumber()
{
return number;
};
float englishweight::getweight()
{
return eng_weight;
};
void englishweight::def_number(float defn)
{
number=defn;
};
void englishweight::def_weight(float defw)
{
eng_weight=defw;
};

EW_driver.cpp:

#include<iostream>
#include"Englishweight.h"
#include"englishweight_def.cpp"
using namespace std;
int main()
{
float defw=0; //define float eng_weight 
float defn=0;//define number
float defo=0;//define ounces(part of defw seen later)
class englishweight testing;
cout<<"Welcome to Englishweight V 1.0!"<<endl;
system("pause");
system("cls");
cout<<"Please input a weight-pounds then ounces."<<endl<<"Pounds: ";
cin>>defw;
cout<<endl<<"Ounces: ";
cin>>defo;
cout<<defo;
//system("cls");
cout<<"Now enter a number."<<endl<<"number: ";
cin>>defn;
if(defo!=0)
{
cout<<endl<<"@@@@@@@@@@"<<endl<<"defo before: "<<defo;
cout<<endl<<"defw before: "<<defw;
defo=(defo/16.0); //convert ounces to pounds (wats the 16, 16.0 difference?)
defw=(defw+defo); //add the converted weight to the ounces  to get the final eng_weight
cout<<endl<<"defo after: "<<defo;
cout<<endl<<"defwafter: "<<defw<<endl<<"@@@@@@@@@"<<endl;
}
testing.def_number(defn); //use def_number and def_weight functions to define eng_weight 
testing.def_weight(defw);
cout<<"***************"<<endl<<"Englishweight: "
<<testing.getweight()<<endl<<"Number: "<<testing.getnumber()<<endl<<"defw: "<<defw<<endl<<"defn: "<<defn<<endl<<"defo: "<<defo<<endl<<"***************"<<endl;
cout<<endl<<"Englishweight + Englishweight is: "<<( (testing.getweight() )*2);
cout<<endl<<"Englishweight - Englishweight is: "<<"0";      	//will always be 0 so no need to use processing power
cout<<endl<<"Englishweight / Englishweight is: "<<"1";			//will always be 1 so no need to use processing power
cout<<endl<<"Englishweight * number is: "<<( (testing.getweight()) * (testing.getnumber()) );
cout<<endl<<"Number * Englishweight is: "<<( (testing.getnumber()) * (testing.getweight()) );
cout<<endl<<"Englishweight / number is: "<<( (testing.getweight()) / (testing.getnumber()) );
system("pause");
return 0;
}

You forgot to include "englishweight.h" in "englishweight_def.cpp", and it's impossible to read your un-indendeted code.

You're still including the .cpp in "EW_driver".

I also believe, but someone please correct me if I'm wrong, that you need #define in your .h file.

-TJ

Wow. okay i did what mosaic said and it compiled fine. Thank you so much for dealing with my sloppiness and 1 last question: I get a warning when dividing a float (defo) with a 16.0 and no warning when dividing defo by 16. The warning states:
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

The line of code this warning refers to defo=(defo/16.0);
Whee is the loss in data?

I also believe, but someone please correct me if I'm wrong, that you need #define in your .h file.

Yes, the "include guard."

#ifndef HeaderName_H
#define HeaderName_H

    /*header declarations*/

#endif
commented: very quick and concise help +1

Wow. okay i did what mosaic said and it compiled fine. Thank you so much for dealing with my sloppiness and 1 last question: I get a warning when dividing a float (defo) with a 16.0 and no warning when dividing defo by 16. The warning states:
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

The line of code this warning refers to defo=(defo/16.0);
Whee is the loss in data?

Wow. okay i did what mosaic said and it compiled fine. Thank you so much for dealing with my sloppiness and 1 last question: I get a warning when dividing a float (defo) with a 16.0 and no warning when dividing defo by 16. The warning states:
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

The line of code this warning refers to defo=(defo/16.0);
Whee is the loss in data?

The reason is because double has higher precision than float, I think float has precision upto 7 & double precision upto 15, that would be precision after the decimal, hence the loss of data would be there. (For example, 9.87654321 might become 9.8765432 when put into a float from a double, losing that tiny fraction off it. (Sorry for the somewhat poor example, just attempting to get a point across))

> The line of code this warning refers to defo=(defo/16.0);
16.0 is a double

defo=(defo/16.0f);
Adding a trailing f would make a float version of 16.0

oh okay, i tried that and it worked, does that command take up any more space/efficiency in the program? Thanks everyone for all your help!

does that command take up any more space/efficiency in the program? Thanks everyone for all your help!

In theory, doubles are more effecient then floats on modern systems. But with a small program like the one you wrote, you will never notice the difference. When you start calculating with *large* matrices, you might want to investigate the difference further.

Why would a programmer opt for 16.0f instead of just 16? For easy modification by any modifiers? And where could i go to learn more about the small efiiciency change?

CPPRULZ: I often require that the program says 16.0f 16L or some such thing.

There are three reasons: (a) to be clear about what sort of number is being used. (b) to avoid implicit conversions, (c) to help control/understand/document roundoff error.

(a) Often comes about in cases when we have maths libraries providing mathematical functions e.g. sqrt(2.0L) is different to sqrt(2.0f) .
It gives the reader an idea about the numerical tolerance later.

(b) If you have class A { A(const int); } and you have A x; x/3.0f; . You can avoid a conversion and add some type safety (an explicit would also help).

(c) You often need to "remind" the reader that the function is of low numerical accuracy. (e.g. return ans/16.0f; )

I am sure there are others that people use but I can't remember.

gcc-4.3 is getting MUCH picker about weak down conversions, I think it is how we will be going.

I have a question, why, when your setting up a constructor, do you declare all the objects values in the cpp file, yet comment the exact stuff in the header, why not just do it all initial constructor

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.