2011-01-31 28 views
15

Tôi đã đi săn hàng giờ để tìm ra những gì tôi đang làm sai và tôi đã gạt bỏ hầu hết các vấn đề của tôi, nhưng khi tôi cố gắng để biên dịch mã của tôi trong main() nó đi kèm với thông điệp này lỗi tương tự:yêu cầu đối với thành viên "..." trong "..." thuộc loại không thuộc loại "..."

request for member "..." in "..." which is of non-class type "..." 

và nó lặp lại cho tất cả các chức năng tôi cố gắng gọi trong main() . Vấn đề là gì? Tôi không thể tìm ra lỗi của tôi ở đâu.

Tôi đang sử dụng Thiết bị đầu cuối trên macbook để biên dịch mã.

Đây là chức năng chính của tôi:

//Program1.cpp 
//Program1Math test function 
#include "Program1Math.h" 

int main() 
{ 
    //Create a Program1Math object 
    Program1Math myProgram1Math(); 

    myProgram1Math.setNumber1(); 
    myProgram1Math.setNumber2(); 

    myProgram1Math.displayMultiple(); 
    myProgram1Math.displaySine1(); 
    myProgram1Math.displayTangent1(); 
    myProgram1Math.displaySine2(); 
    myProgram1Math.displayTangent2(); 
} 

Dưới đây là các định nghĩa thành viên-chức năng cho các lớp:

//Program1Math.cpp 
//Program1Math member-function definitions. 
#include <iostream> 
#include <cmath> 
#include "Program1Math.h" 
using namespace std; 

//constructor makes a Program1Math, adds an blank line 
Program1Math::Program1Math() 
{ 
    cout << "/n"; 
} 

//function to assign the first integer to its appropriate location 
void Program1Math::setNumber1() 
{ 
    cout << "Please enter the first integer number /n"; 
    int numberSpot; 
    cin >>numberSpot; 
    static_cast<double>(numberSpot); 
    number1 = numberSpot; 
} 

//function to assign the second integer to its appropriate location 
void Program1Math::setNumber2() 
{ 
    cout << "Please enter the second integer number /n"; 
    int numberSpot; 
    cin >>numberSpot; 
    static_cast<double>(numberSpot); 
    number2 = numberSpot; 
} 

//function to find the sine value for a specified number 
void Program1Math::calculateSine(double inputNumber) 
{ 
    sineValue = sin(inputNumber); 
} 

//function to find the tangent value for a specified number 
void Program1Math::calculateTangent(double inputNumber) 
{ 
    tangentValue = tan(inputNumber); 
} 

//function to determine if the user-inputted numbers are multiples of each other 
void Program1Math::calculateModulus() 
{ 
    int number1Int = static_cast<int>(number1); 
    int number2Int = static_cast<int>(number2); 
    int modulusValue = number1Int % number2Int; 
    if (modulusValue == 0) 
    multiple = true; 
    else 
    multiple = false; 
} 

//function to display the whether the numbers are multiples or not 
void Program1Math::displayMultiple() 
{ 
    if(multiple == true) 
    cout<< number1 << " is a multiple of " << number2 << "!/n"; 
    else 
    cout<< number1 << "is not a multiple of " << number2 << "./n"; 
} 

//function to display the sine value of the first number 
void Program1Math::displaySine1() 
{ 
    calculateSine(number1); 
    cout << "Sine(" << number1 << ") = " << sineValue << "/n"; 
} 

//function to display the sine value of the second number 
void Program1Math::displaySine2() 
{ 
    calculateSine(number2); 
    cout << "Sine(" << number2 << ") = " << sineValue << "/n"; 
} 

//function to display the tangent value of the first number 
void Program1Math::displayTangent1() 
{ 
    calculateTangent(number1); 
    cout << "Tan(" << number1 << ") = " << tangentValue << "/n"; 
} 

//function to display the tangent value of the second number 
void Program1Math::displayTangent2() 
{ 
    calculateTangent(number2); 
    cout << "Tan(" << number2 << ") = " << tangentValue << "/n"; 
} 

ở đây là các tập tin tiêu đề:

#include <cmath> 
using namespace std; 

class Program1Math 
{ 
public: 
    Program1Math(); 
    void setNumber1(); 
    void setNumber2(); 
    void calculateSine(double); 
    void calculateTangent(double); 
    void calculateModulus(); 
    void displayMultiple(); 
    void displaySine1(); 
    void displaySine2(); 
    void displayTangent1(); 
    void displayTangent2(); 
private: 
    double number1; 
    double number2; 
    double sineValue; 
    double tangentValue; 
    bool multiple; 
}; 
+0

Tôi tin rằng câu trả lời của tôi yêu cầu khai báo lớp cũng như định nghĩa. –

+0

Tôi không điều này có thể được chẩn đoán này mà không có tập tin tiêu đề với định nghĩa lớp. –

+0

Tại sao bạn nhập số của mình dưới dạng ints và truyền chúng thành tăng gấp đôi? Tại sao không đọc chúng trực tiếp như đôi? –

Trả lời

0

Bạn nên tạo ví dụ Program1Math như sau:

Program1Math myProgram1Math; 

hoặc phân bổ các đối tượng trên heap, sử dụng new keyword:

Program1Math *myProgram1Math = new Program1Math(); 
+0

Tôi không nghĩ mình có thể sử dụng "= new" trong C++. Đó không phải là cho Java? – user596228

+1

-1: Đó là cú pháp Java. Trong C++, 'mới' không đúng trong trường hợp này. – Sjoerd

+0

@Sjoerd, Thật vậy, trong C++ new trả về một con trỏ và cấp phát bộ nhớ trên heap. Trả lời chỉnh sửa cho đúng đắn. – mizo

17

dòng Program1Math myProgram1Math(); được hiểu như là một khai báo hàm myProgram1Math() trở Program1Math;

Chỉ cần sử dụng

Program1Math myProgram1Math; 

Chỉ sử dụng() khi một constructor chấp nhận một tham số (không có đối số mặc định).

EDIT: bạn phải biên dịch tất cả các tệp nguồn (.cpp) bao gồm chương trình của bạn. Điều đó sẽ tạo ra các tệp đối tượng có cùng tên với một phần mở rộng khác (trên cửa sổ này là .obj, cho bạn là .o).

Sau đó, tất cả các .o -files phải được liên kết với nhau với một số thư viện do trình biên dịch cung cấp để làm cho chương trình thực thi của bạn.

+0

phân tích cú pháp gây tranh cãi nhất thực sự – KitsuneYMG

+0

khi tôi làm điều đó tôi nhận được thông báo lỗi này: Biểu tượng không xác định: "Program1Math :: displaySine1()", được tham chiếu từ: _main in ccIvBynB.o "Program1Math :: displaySine2()", được tham chiếu từ : _main trong ccIvBynB.o "Program1Math :: displayTangent1()", tham chiếu từ: _main trong ccIvBynB.o "Program1Math :: Program1Math()", tham chiếu từ: _main trong ccIvBynB.o ld: biểu tượng (s) không tìm thấy collect2: ld trả về 1 trạng thái thoát – user596228

+0

Đó là lỗi liên kết cho bạn biết rằng bạn chưa cung cấp triển khai cho các thành viên chức năng –

Các vấn đề liên quan