金华火腿毛 2019-09-10 12:28 采纳率: 50%
浏览 462

两个含有class的头文件如何在同一个源文件上运行?

程序的目的是输入城市及其坐标计算两地距离,要求class要在各自的头文件里

  1. 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

  • 写回答

3条回答 默认 最新

  • JonathanYan 2019-09-10 12:50
    关注

    这个错误的意思是你没有引用对应的头文件

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决