# Thread: Basic Use Of Classes - Help

1. ## Basic Use Of Classes - Help

I seem to be getting this error:

Converter.obj : error LNK2019: unresolved external symbol "public: double __thiscall convert::MPH2KM(double)" (?MPH2KM@convert@@QAENN@Z) referenced in function _main

fatal error LNK1120: 1 unresolved externals
Converter.cpp

Code:
```#include "stdafx.h"
#include <iostream>
#include "m4th2.h"

using std::cout;
using std::cin;

int main()
{

//Variables declared, one for the choice made by the user
//the other for the value entered for converting.
int choice;
double mph;
//double pounds;
//double inches;

convert convert2;
cout << "This Is A Converter\n";
cout << "Menu:\n" << "1 - MPH To KM\n" << "2 - Pounds To Kilos\n" << "3 - Inches To CM\n";
cout << "Please Enter A Choice:\n";
cin >> choice;

if (choice == 1)
{
cout << "Please Enter The Amount Of MPH To Convert To KM:\n";
cin >> mph;
cout << convert2.MPH2KM(mph);
}
else if (choice == 2)
{
cout << "Please Enter The Amount of Pounds To Convert To Kilos\n";
}
else
{
cout << "Please Enter The Amount of Inches To Convert To CM\n";
}

return 0;
}```
m4th.cpp

Code:
```#include "stdafx.h"
#include "m4th2.h"

double MPH2KM(double mph)
{
double km;
double mphconv = 1;
km = mph * 1.609;

return km;

}```
m4th.h

Code:
```#include <iostream>

class convert
{
public:
double MPH2KM(double mph);
double P2K(double p);
double I2C(double i);

};```

Can anyone help?

2. in m4th.cpp, don't forget the scope operator to resolve MPH2KM.

Code:
`double convert::MPH2KM(double mph)`

3. Yep, Mario hit it right on.

Also, just some general advice on classes for future reference.

1) They are meant to represent real world objects with properties (car; bank account; etc.) rather than actions (convert; jump; etc.). So creating a class called "Convert" is kinda weird where simple functions in your main file would make more sense.

2) Also, the header ".h" and implementation file ".cpp" naming convention tends to be the same as the enclosed class. (i.e. convert.h & convert.cpp).

4. Thank you very much for the tips.

I am now getting this error though:

Code:
`error C2761: 'double convert::MPH2KM(double)' : member function redeclaration not allowed`

5. Not sure what is the problem, but you might want to use header guards.

6. "M4th.h" needs inclusion guards. You are including it in two files and there's where the redefinition should come to play.
Also, it doesn't seem to need the #include <iostream>

So it should look like:
Code:
```#ifndef M4TH_H
#define M4TH_H
/*
class declaration
*/
#endif```

7. Originally Posted by Kurisu33
1) They are meant to represent real world objects with properties (car; bank account; etc.) rather than actions (convert; jump; etc.). So creating a class called "Convert" is kinda weird where simple functions in your main file would make more sense.
If that is meant to be a general rule of thumb, then I would disagree somewhat. classes can also represent functors (classes which overload operator() ), objects of these classes can be used in a manner very similar to functions, in which case, it would make alot of sense to name the class to reflect an ability or method.

just to go back on topic, here's one way the OP's program could have been written with functors
Code:
```#include <iostream>

class MPHtoKPH
{
const double conversion_factor;
public:
MPHtoKPH() : conversion_factor(1.609) {}
double operator()(double);
};

double MPHtoKPH::operator ()(double d)
{
return d*conversion_factor;
}

int main()
{
MPHtoKPH convert;
double mph(10.0);
std::cout<< convert(mph);
}```
[/Overkill]

8. For your example, operator() should probably be const.

9. Originally Posted by Bench82
objects of these classes can be used in a manner very similar to functions, in which case, it would make alot of sense to name the class to reflect an ability or method.
Your class would still be a Converter. Calling an object of class Converter "convert" looks ok to me.
Kurt

10. Could you elaborate on this? I didn't see why...

Originally Posted by anon
"M4th.h" needs inclusion guards. You are including it in two files and there's where the redefinition should come to play.
Also, it doesn't seem to need the #include <iostream>

So it should look like:
Code:
```#ifndef M4TH_H
#define M4TH_H
/*
class declaration
*/
#endif```