2015-09-19 3 views
0

Привет, я получаю сообщение об ошибке для каждой функции, которую я пробовал, «функция не объявлена ​​в этой области». Я новичок в C++ и пытался получить свою TA для решения проблемы, но до сих пор не понял ее. Спасибо за помощь!C++ linking header/main error

Вот мой файл заголовка (circle.h):

#ifndef _CIRCLE_H_ 
#define _CIRCLE_H_ 

class circle 
{ 
private: 
    double x1; 
    double x2; 
    double y1; 
    double y2; 
protected: 
    double distance(double x1, double y1, double x2, double y2); 
public: 
    double radius(double x1, double y1, double x2, double y2); 
    double circumference(double x1, double y1, double x2, double y2); 
    double area(double x1, double y1, double x2, double y2); 
    void populate_classobj(double x1, double y1, double x2, double y2); 
}; 
#endif 

Вот где я определяю свои функции класса (circle.cc):

#include <cmath> 
#include "circle.h" 
#define PI (4*atan(1)) 
using namespace std; 

double circle::distance(double x1, double y1, double x2, double y2) { 
    double dist; 
    dist = sqrt(pow((x2-x1),2)+pow((y2-y1),2)); 
    return dist; 
} 

double circle::radius(double x1, double y1, double x2, double y2) { 
    return distance(x1, y1, x2, y2); 
} 

double circle::area(double x1, double y1, double x2, double y2) { 
    double circle_area = PI*(pow(radius(x1, y1, x2, y2),2)); 
    return circle_area; 
} 

double circle::circumference (double x1, double y1, double x2, double y2) { 
    double circ = 2*PI*radius(x1, y1, x2, y2); 
    return circ; 
} 

void circle::populate_classobj (double x_1, double y_1, double x_2, double y_2) { 
    double x1 = x_1; 
    double y1 = y_1; 
    double x2 = x_2; 
    double y2 = y_2; 
} 

Вот мой главный (основной. куб.см):

#include <iostream> 
#include <cmath> 
#include "circle.h" 
using namespace std; 

int main() 
{ 
    double x_1, y_1, x_2, y_2; 
    int switch_val; 
    cout << "Enter x1 coordinate: "; 
    cin >> x_1; 
    cout << "Enter y1 coordinate: "; 
    cin >> y_1; 
    cout << "Enter x2 coordinate: "; 
    cin >> x_2; 
    cout << "Enter y2 coordinate: "; 
    cin >> y_2; 
    circle mycircle; 
    mycircle.populate_classobj (x_1, y_1, x_2, y_2); 
    do{ 
     cout << "1 for radius, 2 for circumference, 3 for area, 4 for exit" << endl; 
     cout << "Enter your desired computation: "; 
     cin >> switch_val; 
     switch(1) { 
      case 1: cout << "Radius is: " <<endl; 
        cout << radius(x_1, y_1, x_2, y_2); 
        break; 
      case 2: cout << "Circumference is: " <<endl; 
        cout << circumference (x_1, y_1, x_2, y_2); 
        break; 
      case 3: cout << "Area is: " <<endl; 
        cout << area(x_1, y_1, x_2, y_2); 
        break; 
      case 4: cout << "Exiting program... "<< endl; 
        break; 
      default: cout << "Invalid input please re-input value"<<endl; 
        break; 
     } 
    }while(switch_val != 4); 
    return 0; 
} 

Я думаю, что это что-то делать с моей #include заявления, но я не уверен. Благодаря!

+0

Более конкретное название для вашего вопроса было бы приятным для других, у кого может быть такая же проблема. – Gombat

+0

В вашей main.cc содержится строка '#include" circle.h "'? –

ответ

2

В вашем switch вы используете метод вашего класса circle, например radius. Функции классов, так называемые методы, доступны только для вызова с использованием существующего экземпляра этого класса.

E.g.

circle mycircle; 
double radius = mycircle.radius(1.0,2.0,3.0,4.0); 

будет работать.

Но если методам не нужны какие-либо переменные-члены класса, вы можете объявить их как static. Просто напишите слово перед функцией в вашем заголовке:

`static double radius(double x1, double y1, double x2, double y2);` 

Теперь вы можете назвать это без экземпляра circle.

double radius = circle::radius(1.0,2.0,3.0,4.0); 

Вам нужно использовать пространство имен классов, чтобы компилятор знал, откуда эта функция.