/*this program:
-finds the distance between 2 points
*/
#include <iostream>
#include <cmath>
#include "Declarations.cpp"
usingnamespace std;
int main()
{
Distance coordinates; //holds coordinates
double x1, x2; //x coordinates
double y1, y2; //y coordinates
//get coordinates from the user
cout<<"Enter the first X coordinate. ";
cin>>x1;
cout<<"Enter the first y coordinate. ";
cin>>y1;
cout<<"Enter the second x coordinate. ";
cin>>x2;
cout<<"Enter the second y coordinate. ";
cin>>y2;
//set the coordinates
set(x1, x2, y1, y2);
//print the distance
cout<<"\n\nThe distance is "<<get_dist()<<endl;
//pause the program
cin.get();
//terminate the program
return 0;
}
//----------------------------------------------------------------------------------------------------------------------
//Distance class functions
void Distance::set(double a1, double a2, double b1, double b2) //sets the x and y coordinates
{
a1=x1;
a2=x2;
b1=y1;
b2=y2;
//calculate the distance
dist=calc();
}
double Distance::calc() //calculates the distance between 2 points
{
return (sqrt( (x2-x1)*(x2-x1) - (y2-y1)*(y2-y1) ));
}
//--------------------------------------------------------------------------------------------------------------------------
Declarations.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//finds the distance between 2 points
class Distance
{
private:
double x1, x2; //x coordinates
double y1, y2; //y coordinates
double dist; //holds the distance between 2 points
public:
void set(double a1, double a2, double b1, double b2); //sets x and y values
double get_dist()
{return dist;}
private:
double calc(); //calculates the distance between 2 points
}
There is an error in main.cpp on line 15. Error: expected unqualified-id before 'using'. I have been trying to solve this problem for some time now, and I can't get it. Thanks for your help.
To solve your problem, put a semi-colon at the end of line 14 in Declarations.cpp
However, you shouldn't be #include-ing .cpp files.
Make a new file "Declarations.h" and cut/paste the lines from Declarations.cpp to it. Then cut/paste the Distance class functions from main.cpp into Declarations.cpp. Put #include Declarations.h" at the top of Declarations.cpp and main.cpp.
Example:
main.cpp
1 2 3 4 5 6 7 8
#include "example.h"
int main(){
Example example;
example.write();
cin.get();
return 0;
}
example.h
1 2 3 4 5 6 7
class Example{
public:
Example(){};
~Example(){};
void write();
};