2013-11-19 5 views
1

У меня проблема с моим кодом, который имеет дело с оператором friend >, но я не могу его исправить.
Я добавил код friend bool operator > (const my_int& c1, const my_int& c2);, и я знаю, что это правильно, но в основном классе, где я пытался его реализовать, это немного неправильно и не знаю, как исправить.Проблема с определением оператора друга

#include <iostream> 
#include <cmath> 
using namespace std; 
//********************************************************* 
// Function: my_int 
// Purpose: Constructor initializes the val to x 
// Params: x - the value for the val 
// Calls: none 
// Uses:  none 
//********************************************************* 
//my_int::my_int(int x); 
//********************************************************* 
// Function: my_int 
// Purpose: Constructor initializes the val to 0 
// Params: none 
// Calls: none 
// Uses:  none 
//********************************************************* 
//my_int::my_int(); 
//********************************************************* 
// Function: my_int 
// Purpose: Constructor initializes the val to x 
// Params: x - the value for the val 
// Calls: none 
// Uses:  none 
//********************************************************* 
//my_int::my_int(); 
//********************************************************* 
// Function: set 
// Purpose: Sets the val to x 
// Params: x - the new value for the val 
// Calls: none 
// Uses:  none 
//********************************************************* 
// void my_int::set(int x); 
//********************************************************************* 
// Function: input 
// Purpose: reads and stores a value from inp. if fin is a input 
//   stream, then fin is already connected to a file.User enters 
//   a value and ask the user to re-enter the data if the 
//   user enters an incorrect value. 
// Params: inp -- the input stream 
// Calls: none 
// Uses:  istream 
//********************************************************************* 
//void my_int:: input(istream& inp); 
//********************************************************************* 
// Function: output 
// Purpose: display the val on fout. if fout is a output stream 
//   then fout is already connected to a file 
// Params: fout -- the output stream 
// Calls: none 
// Uses:  ostream 
//********************************************************************* 
//void my_int::output(ostream& fout); 
//********************************************************* 
// Function: get_int 
// Purpose: returns the val 
// Params: none 
// Calls: none 
// Uses:  none 
//********************************************************** 
// int my_int::get_int() 
//********************************************************* 
// Function: is_prime 
// Purpose: object num contains a valid positive value 
//   returns true if num is prime; otherwise 
//   returns false 
// Params: num - the value to be checked for prime 
// Calls: sqrt 
// Uses:  cmath 
//********************************************************* 
// bool is_prime(const my_int& num); 

class my_int 
{ 
public: 
    my_int(int x); 
    my_int(); 

    void set(int x); 

    void input(istream& inp); 

    void output(ostream& fout) const; 

    int get_int() const; 

    friend bool operator >(const my_int& c1, const my_int& c2); 

private: 
    int val; 
}; 

bool is_prime(const my_int& num); 

int main() 
{ 
    my_int value1; 

    value1.input(cin); 

    value1.output(cout); 

    if (is_prime(value1)) 
     cout << " is a prime number \n"; 
    else 
     cout << " is not a prime number \n"; 

    return 0; 
} 

my_int::my_int(int x) 
{ 
    val = x; 
} 

my_int::my_int() 
{ 
    val = 0; 
} 

void my_int::set(int a) 
{ 
    val = a; 
} 

void my_int::output(ostream& fout) const 
{ 
    fout << " The value is equal to " << val << endl; 
} 

int my_int::get_int() const 
{ 
    return val; 
} 

bool operator >(my_int& c1, my_int& c2) 
{ 
    if (c1.val > c2.val) 
     return (true); 
    else 
     return (false); 

} 

void my_int::input(istream& inp) 
{ 
    cout << "Enter a positive value greater than 1 "; 
    inp >> val; 
    while (val <= 1) 
    { 
     cout << "Entered an Invalid value \n"; 
     cout << "Enter a positive value greater than 1 \n"; 
     inp >> val; 
    } 
} 

//num has been given a value and returns true if the value is a 
//prime number and false otherwise 

bool is_prime(const my_int& num) 
{ 
    double limit; 
    int n;  //divisor 
    bool prime= true; 

    limit = sqrt(static_cast<double>(num.get_int())); 

    n = 2; 

    while (n <= limit && prime) 
    { 
     if (num.get_int() % n == 0) 
     prime = false; 
     else 
     n++; 
    } 
    return prime; 
} 
+0

Я обновил название, чтобы быть более полезным для будущих посетителей. –

ответ

1

Вам нужно что-то вроде этого:

bool operator > (my_int& c1, my_int& c2) 
{ 
    return c1.val > c2.val; 
} 

то имя членов данных является val, их тип int. Вызов c1.int не имеет смысла в C++. И рисунок

if (x) 
    return true; 
else 
    return false; 

длинный и одноименный. Вы можете просто return x;.

Кроме того, вы должны рассмотреть возможность передачи const ссылок. Это позволит вам привязываться к временным значениям:

bool operator > (const my_int& c1, const my_int& c2) { ... } 

Необходимо изменить декларацию и определение.

+0

Вал нельзя использовать, поскольку он является закрытым. – user2977562

+2

@ user2977562 Оператор является «другом». – juanchopanza

Смежные вопросы