首页 > 解决方案 > C++ - 致命错误:circle.h:没有这样的文件或目录

问题描述

我是 C++ 编程新手,第 52 行出现错误"fatal error: circle.h: No such file or directory"。我不确定我错过了什么无法识别,可以帮我理解我在这里做错了什么吗?

我的代码应该计算圆形和正方形的面积,并包含头文件(Circle.h, Square.h)和实现文件(circle.cpp,Square.cppmain.cpp)。

感谢您的任何帮助,因为它很感激......

圈子.h:

#ifndef CIRCLE_H
#define CIRCLE_H

struct Circle  //circle structure class
{
public:
    Circle();  //declaration
    void setRadius(double r);  //setting the radius
    double getRadius(); //getting the radius
    double Area(); //getting calculated area of the circle
    void Display(); //display the output

protected:
    double Radius; //declaring the radius value

    private;
};

#endif

方.h:

#ifndef SQUARE_H
#define SQUARE_H

struct Square  //square structure class
{
puble:
    Square();  //delcaration
    void setLength();  //setting the length
    double getLength();  //getting the length
    double Area(); //get the calculated area of the square
    void Display(); //display the output

protected :
    double Length;  //length value declaration

    private;
};

#endif

圈子.cpp:

#include <iostream>
#include "circle.h"

using namespace std;

const double PI=3.14159;  //initializing the global values
Circle::Circle()  //method declaration
{
    Radius = 0; //initializing the radius value
}

void Circle::setRadius(double r)
{
    Radius = r<=0 ? 1 : r;  //check the input radius value
}

double Circle::getRadius()
{
    return Radius; //getting and setting the value
}

double Circle::Area()
{
    return Radius * Radius * PI;  //calculating and returning the area for the cirlce
}

void Circle::Display()  //display the data
{
    cout << "\nCircle Data";
    cout << "\nRadius = " <<getRadius();
    cout << "\nArea = "  <<Area() << "\n";
}

方.cpp:

#include<iostream>
#include "square.h"

using namespace std;

Square::Square()  //method declaration
{
    Length = 0;  //initializing the length
}

void Square::setLength(double)
{
    Length = 1<=0 ? 1 : l;  //check the input length value
}

double Square::getLength()
{
    return Length;  //returning the length
}

double Square::Area()
{
    return Length * 4;  //calculate and return the area of the square
}

void Square::Display()
{
    cout << "n\Square Data";
    cout << "n\Length = " <getLength();
    cout << "n\Area = " << Area() << "n\";
}

主.cpp:

#include <iostream>
#include "circle.h" //include the header file for circle
#include "square.h"  //include the header file for square

using namespace std;

int main()
{
    int radius.length;
    Circle circle; //Displaying a circle using the default value of the radius
    circle Display();
    Circle circ; //This is the circle with the data entered for the radius

    cout << "Enter a radius value for the circle: " endl;  //Prompt the user to input the radius
    cin >> Radius;  //read the input

    circ setRadius (radius);
    circ Display();

    Square square;  //Displaying a square using the default value
    square Display();
    Square sqre; //This is the square with the data entered for the length

    cout << "Enter the length of one side of the square: " endl;  //Prompt the user to input the length
    cin >> length; //read the input

    sqre.setLength(length);  //from the base class
    sqre Display();


    return 0;
}

标签: c++

解决方案


首先,列出文件并检查拼写(在 linux 上):

ls -l

重命名/保存您的 main.cpp,并替换为:

#include <iostream>
#include "circle.h" //include the header file for circle
#include "square.h"  //include the header file for square

using namespace std;

int main()
{
    Circle circle; // create a default circle
    circle.Display(); //Display circle using the default values

    Square square; // create a default square
    square.Display(); //Display square using the default values

    return 0;
}

如果这给出了同样的错误,那么试试这个

#include <iostream>
#include "Circle.h" //include the header file for circle
#include "Square.h"  //include the header file for square

using namespace std;

int main()
{
    Circle circle; // create a default circle
    circle.Display(); //Display circle using the default values

    Square square; // create a default square
    square.Display(); //Display square using the default values

    return 0;
}

推荐阅读