2012-08-08 13 views
0

我試圖寫一個程序,C++圓對象/的getArea()

•定義,其中半徑是使用利用參數化構造

由隨機類提供的隨機數設置五週界的對象的數組

•定義,其中半徑是使用利用setRadius方法

•顯示使用getArea()

每個陣列中的每個圓的區域由隨機類提供的隨機數設置五週界的第二陣列對象

我需要使用getArea()方法顯示每個數組中每個圓的區域的問題,我需要訪問每個數組中具有五個圓的半徑值的數組,然後求出區域3.14 * Radius *半徑。然後將該區域顯示在屏幕上。

同樣在做了一些研究後,我是否需要實例化我在Circle myCircle;中輸入的圓對象 。然而,它想出了一個錯誤,說

1> main.obj:錯誤LNK2019:無法解析的外部符號 「公用: __thiscall圈::圈(無效)」(?? 0Circle @@ QAE @ XZ)中引用函數_main

Circle.h文件

#pragma once 
#include <string> 

class Circle 
{ 
private: 
    float Radius; 

public: 
    Circle(); // initialised radius to 0 
    Circle(float r); // accepts an argument and assign its value to the radius attribute 
    void setRadius(float r); // sets radius to a value provided by its radius parameter (r) 
    float getRadius(); // returns the radius of a circle 
    float getArea(); // calculates and returns the areas of its circle 
}; 

Random.h文件

#pragma once 
#include <iostream> 
#include <cstdlib> 
#include <ctime> 
using namespace std; 

class Random 
{ 
public: 
    static void initialiseSeed(); 
    // random number initialised 
    // random number has been initialised to the current time. 

    static int random(int lower, int upper); 
    // this function will return a positive random number within a specific lower and 
    // upper boundary. 
}; 

Main.cpp的文件

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <cmath> 
#include <iomanip> 
using namespace std; 

// Include header files 
#include "Circle.h" 
#include "Random.h" 

int main() 
{ 
    // Instantiate myCircle object 
    //Circle myCircle; 

    // Array 1 
    // an array of five circles objects where radius is set using the random number 
    // provided by the random class utilising the parameterised constructor 

    int CircleArrayOne [5]; // store the numbers 
    const int NUM = 5; // Display 5 random numbers 

    srand(time(NULL)); // seed the generator 

    // populate the array by calling the random function from the random class 
    // within the lower and upper bounds 

    for(int x = 0; x < NUM; ++x) 
    { 
     CircleArrayOne[x] = Random::random(1, 40); // assumed 0 and 40 as the bounds 
    } 

    // Below is the code I use to output the array to the screen to make sure 
    // that is output the correct number of values and it generate number between 1 and 40 
    // I am doing this to test the code above that populate the array within lower and upper 
    // bounds 

    cout << "Checking the radius in the Array 1." << endl; 
    for(int i = 0; i < NUM; ++i) 
    { 
     cout << CircleArrayOne[i] << endl; 
    } 

    // Array 2 
    // a second array of five circles objects where radius is set using the random number 
    // provided by the random class utilising the setRadius method 

    float CircleArrayTwo [5]; // store the numbers 
    const int Number = 5; // Display 5 random numbers 

    srand(time(NULL)); // seed the generator 

    // populate the array with random numbers 

    for(int i = 0; i < Number; ++i) 
    { 
     CircleArrayTwo[i] = rand()%100; 
    } 

    // Below is the code I use to output the array to the screen to make sure 
    // that is output the correct number of values and it generate random values 

    cout << "Checking the radius in the Array 2." << endl; 
    for(int i = 0; i < Number; ++i) 
    { 
     cout << CircleArrayTwo[i] << endl; 
    } 

    // Display the area of each Circle within each array using getArea() 

    cout << "\nThe area of each circle within array 1: " << endl; 

    cout << "\nThe area of each circle within array 2: " << endl; 

    // Display a message that indicates which set of circle has the largest 
    // combined area 

    cout << "\nArray: " << "had the largest combined area." << endl; 

    // Display a message that indicates which set contains the circle 
    // with the largest area 

    cout << "\nArray: " << "contain the circle with the largest area.\n" << endl; 

    system("PAUSE"); 
    return 0; 
} 

// Calling Circle(float r) from Circle Class 
Circle::Circle(float r) 
{ 
    if (r<=0) 
    { 
     cout << "An invalid radius has been detected." << endl; 
     cout << "The radius has been set to 1.0" << endl; 
     Radius = 1.0; 
    } 
    else 
     Radius = r; 
} 

// Calling getArea() from Circle Class 
float Circle::getArea() 
{ 
    // Area = pi * radius * radius 
    return 3.14 * Radius * Radius; 
} 

// Calling setRadius(float r) from Circle Class 
void Circle::setRadius(float r) 
{ 
    rand()%100; 
    Radius = r; 
} 

// Calling getRadius() from Circle Class 
float Circle::getRadius() 
{ 
    return Radius; 
} 

// Calling random(int lower, int upper) from Random Class 
int Random::random(int lower, int upper) 
{ 
    int range = upper - lower + 1; 
    return (rand() % range + lower); 
} 

// Calling initialiseSeed() from Random Class 
void Random::initialiseSeed() 
{ 
    srand((unsigned int)time(0)); 
    rand()%100; 
} 

使用getArea()功能

+0

爲繪製你可能想看看控制檯圖。例如請參閱http://stackoverflow.com/questions/1937163/drawing-in-a-win32-console-on-c上用於繪製圓圈的接受答案 – 2012-08-08 10:47:11

回答

3

您沒有提供Circle的默認構造函數的定義,添加

Circle::Circle() : Radius(0) {} 

到的main.cpp。

0

你給了沒有Circle()默認構造函數的定義(實現)我如何獲得每個陣列中的每個圓的面積,但只適用於Circle(float)構造函數。 一種可能性是:

Circle::Circle() : Radius(0) {} 

還要注意,你就不會得到這個錯誤,當你在頭文件中都留下了Circle();(因爲這可以防止編譯器將默認的構造函數爲你)。

2

您需要定義Circle的默認構造函數:

Circle::Circle() : Radius() {} 

這也初始化Radius0.0F