程序的目的是输入城市及其坐标计算两地距离,要求class要在各自的头文件里
- City.h
#ifndef POINT_CLASS_HEADER_INCLUDED
#define POINT_CLASS_HEADER_INCLUDED
#include<iostream>
#include<string>
const int MAX_CITY_NAME = 6;
class City
{
private:
char name[MAX_CITY_NAME][20];
int count;
public:
City():name(),count(0){}
void get_name()
{
std::cin.ignore();
std::cout<<"\nEnter the name of a city:";
std::cin.getline(name[count],20);
count++;
}
void print_name()
{
for(int i = 1; i<= count; i++)
{
std::cout<<i<<")"<<name[i-1]<<"\n";
}
std::cout<<"\n";
}
};
#endif
2.Point.h
#ifndef POINT_CLASS_HEADER_INCLUDED
#define POINT_CLASS_HEADER_INCLUDED
#include<iostream>
class Point
{
private:
double x[6],
y[6];
int count;
public:
Point():count(0){};
void Point::Input()
{
std::cout<<"Enter the x-axis of this city:";
std::cin>>x[6];
std::cout<<"Enter the y-axis of this city:";
std::cin>>y[6];
count++;
}
void Point::print_pt()
{
std::cout<<"("<<x[count]<<","<<y[count]<<")\n";
}
};
#endif
3.distance.cpp
#include<string>
#include<iostream>
#include"city.h"
#include"point.h"
using namespace std;
int main()
{
system("clear");
char i;
bool quit = true;
City ob1;
Point ob2;
do{
cout<<"1.Enter city Information\n"
<<"2.Caluslate Distance between two cities\n"
<<"3.Print All cities\n"
<<"4.Quit\n";
cin>>i;
switch(i)
{
case'1':
case'E':
ob1.get_name();
ob2.Input();
break;
case'3':
case'P':
ob1.print_name();
ob2.print_pt();
break;
case'4':
case'Q':
quit = false;
break;
}
调换位置任意一个class都会报错
[Error] aggregate 'Point ob2' has incomplete type and cannot be defined