本文介绍了C ++错误:'Line2'尚未声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
circle2。 h:
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:
circle.cpp:
bool Circle2::contains(Line2 l) {
return 0;
}
geometry.h:
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:
This is 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:
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;
before the
class Circle2 {
这篇关于C ++错误:'Line2'尚未声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!