金华火腿毛 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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog