C:\ CodeBlocks \ kool \ praks2 \ src .. \ include \ circle2.h | 8 |错误:
  “ Line2”未命名类型|
  C:\ CodeBlocks \ kool \ praks2 \ src .. \ include \ circle2.h | 17 |错误:
  尚未声明“ Line2” | || ===
  构建完成:2个错误,0个警告
  === |


circle2.h:

#ifndef CIRCLE2_H
#define CIRCLE2_H

#include "geometry.h"

class Circle2 {
    public:
        Vector2 p1;
        float r;

        Circle2();
        Circle2(Vector2 np1, float nr);
        float circumference();
        float area();
        bool contains(Vector2 v);
        bool contains(Line2 l);  // error is here.
        void scale(float factor);
        friend ostream& operator <<(ostream& out, const Circle2& cir);

};

#endif // CIRCLE2_H


circle.cpp:

bool Circle2::contains(Line2 l) {
    return 0;
}


geometry.h:

#ifndef GEOMETRY_H
#define GEOMETRY_H

// These are needed to use the functions in our library
#include <iostream>
using namespace std;

// Include own headers
// NB! Add your own headers here!
#include "vector2.h"
#include "line2.h"
#include "circle2.h"


#endif // GEOMETRY_H


这是circle2.cpp:

#include "../include/circle2.h"
#include <math.h>


Circle2::Circle2() {
    p1 = Vector2();
    r = 0;
}

Circle2::Circle2(Vector2 np1, float nr) {
    p1 = np1;
    r = nr;
}

float Circle2::circumference() {
    return 2 * r * M_PI;
}
float Circle2::area() {
    return pow(r, 2) * M_PI;
}
bool Circle2::contains(Vector2 v) {
    if(p1.distanceFrom(v) <= r) return 1;
    return 0;
}
bool Circle2::contains(Line2 l) {
    return 0;
}
void Circle2::scale(float factor) {
    r *= factor;
}
ostream& operator<<(ostream& out, const Circle2& cir) {
    out << "(" << cir.p1 << ", " << cir.r << ")";
    return out;
}


line2.cpp:

    #include "../include/line2.h"
#include <math.h>


Line2::Line2() {
    p1 = Vector2();
    p2 = Vector2();
}

Line2::Line2(Vector2 np1, Vector2 np2) {
    p1 = np1;
    p2 = np2;
}

float Line2::length() {
    return p1.distanceFrom(p2);
}

ostream& operator<<(ostream& out, const Line2& line) {
    out << "(" << line.p1 << " - " << line.p2 << ")";
    return out;
}


line2.h:

    #ifndef LINE2_H
#define LINE2_H

#include "geometry.h"

class Line2 {

    public:
        Vector2 p1;
        Vector2 p2;

        Line2();
        Line2(Vector2 np1, Vector2 np2);
        float length();
        friend ostream& operator <<(ostream& out, const Line2& line);
};
#endif // LINE2_H

最佳答案

您可以尝试添加:

class Line2;


之前

class Circle2 {

09-11 17:44