到目前为止,我已经设法修复了自己的错误,但是这个错误让我困惑了一段时间。如果您有任何提示,那将非常感激!感谢。
我在Windows笔记本电脑上使用Eclipse,在构建之后会出现以下错误: 未命名的引用`Point :: Point()'circle.cpp / Assignment4_1 / IN4084MSc第13行C / C ++问题
我的所有文件(main4_1.cpp,point.h,point.cpp,circle.h,circle.cpp)都是项目的一部分(称为Assignment4_1)。
#include "point.h"
#include "circle.h"
#include <cmath>
using namespace std;
Circle::Circle(Point ct, double rd):Point(ct)
{
center = ct;
if(rd > 0)
{
radius = rd;
}
radius = -1;
}
Point Circle::get_center(){
return center;
}
double Circle::get_radius(){
return radius;
}
void Circle::print(){
cout << " <Circle(<Point(“"<<center.get_x()<<"”,“"<<center.get_y()<<"”)>,”"<<radius<<"”)>";
}
void Circle::print(string s){
cout << s;
print();
}
void Circle::println(){
print();
cout << endl;
}
void Circle::println(string s){
print(s);
cout << endl;
}
#ifndef CIRCLE_H_
#define CIRCLE_H_
#include <iostream>
#include "point.h"
using namespace std;
class Circle: public Point{
Point center;
double radius;
public:
Circle(Point ct, double rd);
Point get_center();
double get_radius();
void print();
void print(string s);
void println();
void println(string s);
};
#endif /* CIRCLE_H_ */
#include "point.h"
#include <cmath>
using namespace std;
Point::Point(double x, double y){
x_coord = x;
y_coord = y;
}
double Point::get_x(){
return x_coord;
}
double Point::get_y(){
return y_coord;
}
void Point::print(){
//post: has printed the contents of the Point object in the format
cout << "<Point( “ " << x_coord <<" ” , “ " << y_coord << " ” )>";
}
void Point::print(string s){
//post: has printed string s first, then printed the contents of the Point object
cout << s << " ";
print();
}
void Point::println(){
//post: has printed the contents of Point object, then moved the cursor to the next line
print();
cout << endl;
}
void Point::println(string s){
//post: has printed string s first, then printed the contents of the Point object, and moved the cursor to the next line
cout << s << " ";
println();
}
void Point::move(double dx, double dy){
// post: x_coord = x_coord + dx, y_coord = y_coord + dy
x_coord = x_coord + dx;
y_coord = y_coord + dy;
}
double Point::distance(Point that){
//post: returns the distance between this Point and that Point
return sqrt( pow(x_coord - that.get_x(),2) + pow(y_coord - that.get_y(),2) );
}
bool Point::equals(Point that){
//post : returns true if this Point and that Point have the same coordinates
return (x_coord = that.get_x());
}
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
using namespace std;
class Point{
protected:
double x_coord;
double y_coord;
public:
Point(double x, double y);
Point();
double get_x();
double get_y();
void print();
void print(string s);
void println();
void println(string s);
void move(double dx, double dy);
double distance(Point that);
bool equals(Point that);
};
#endif /* POINT_H_ */
#include <iostream>
#include <cstdlib>
using namespace std;
#include "point.h"
#include "circle.h"
void test1(){
cout << "test1:" << endl;
Point p1 = Point(2,3);
cout << "Point p1 is created, the data of p1 = (2,3)" << endl;
cout << "p1.get_x() -> " << p1.get_x() << endl;
cout << "p1.get_y() -> " << p1.get_y() << endl << endl;
p1.println("p1.println() -> ");
cout << endl;
cout << "end test1" << endl << endl;
}
void test2(){
cout << "test2:" << endl;
cout << "Point p1 is created, the data of p1 = (2,3)" << endl;
cout << "Point p2 is created, the data of p2 = (0,0)" << endl;
Point p1 = Point(2,3);p1.println("p1.println() -> ");
Point p2 = Point(0,0);p2.println("p2.println() -> ");
cout << endl;
p1.println("p1 before move -> ");
cout << "p1.move(1,1)" << endl;p1.move(1,1);
p1.println("p1 after move -> ");
cout << endl;
cout << "p1.distance(p2) -> " << p1.distance(p2) << endl << endl;
cout << "p1.equals(p1) -> " << p1.equals(p1) << endl;
cout << "p1.equals(p2) -> " << p1.equals(p2) << endl;
cout << "end test2" << endl << endl;
}
int main(){
test1();
test2();
return 0;
}
答案 0 :(得分:2)
编译器告诉你定义
Point::Point()
您只在Point
类定义中声明了。可能的实现方式是使用0.0
初始化两个坐标:
Point::Point() : x_coord(0.0), y_coord(0.0) {}
在这种情况下,您必须显式初始化成员,因为内置类型在默认构造时不会被初始化。