2013-10-18 145 views
0

到目前爲止,我已經設法解決了我自己的錯誤,但是這個讓我困惑了一會兒。如果您有任何提示,我們將非常感激!謝謝。C++「未定義的類::函數引用」

我使用Eclipse在Windows筆記本電腦,這給了我建成後出現以下錯誤: 未定義的參考`點::點()」 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

這不是C ... – 2013-10-18 12:50:59

回答

2

編譯器告訴你定義

Point::Point() 

您已在Point類的定義只是聲明。一個可能的實施將與0.0初始化兩個座標:

Point::Point() : x_coord(0.0), y_coord(0.0) {} 

你有成員明確初始化在這種情況下,因爲內建類型沒有得到零初始化時默認構造。

+0

萬分感謝幫助我! 已將您的建議添加到我的circle.cpp文件(第13行),它可以工作。 – user2894691