Страница 1 из 1

Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 02:47
Lotles
Помогите исправить ошибки:

class ProperFraction: public FloatFraction{
error C2504: 'FloatFraction' : base class undefined



ProperFloatFraction(int n, int d){
set(n, d);
}
warning C4183: 'ProperFloatFraction': missing return type; assumed to be a member function returning 'int'



#include <stdafx.h>
fatal error C1075: end of file found before the left brace '{' at 'FloatFractionV3.cpp(5)' was matched

Код: Выделить всё

#include <stdafx.h>
#include <conio.h>
#include "Fraction.h"
using namespace std;
class FloatFraction : public Fraction {
public:
   FloatFraction(){
	   set(0, 1);
	}
    FloatFraction(int n, int d){
		set(n, d);
	}
	FloatFraction(int n){
		set(n,1);
	}
	FloatFraction(const Fraction& src){
		set(src.get_num(),src.get_den());
	}
     double get_float(){
     return static_cast<double>(get_num())/get_den();
};
class ProperFraction: public FloatFraction{
public:
    ProperFraction(){
	   set(0, 1);
	}
    ProperFloatFraction(int n, int d){
		set(n, d);
}
	ProperFraction(int n){
		set(n,1);
	}
	ProperFraction(const Fraction& src){
		set(src.get_num(),src.get_den());
	}
     double get_float(){
     return static_cast<double>(get_num())/get_den();
	 }
	 void pr_proper(ostream& os){
		 if(get_whole()!=0)
			 os << get_whole() << " ";
		 os << get_num << "/" << get_den();
	 }
	 int get_whole(){
		 int n =get_num();
		 return n/get_den();
	 }
	 int get_num(){
		 int n = get_num();
		 return n%get_den();
	 }
};
int main() {
    ProperFraction f1(1,2),f2(5,6),f3;
	cout << "Value of f3 is " << f3.pr_proper(cout) << endl;
	cout << "Float value of is " << f3.get_float() << endl;
    return 0;
}

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 09:27
Albor
Приведи содержимое Fraction.h

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 09:29
BBB
Lotles писал(а):Помогите исправить ошибки:
ProperFloatFraction(int n, int d){
set(n, d);
}
warning C4183: 'ProperFloatFraction': missing return type; assumed to be a member function returning 'int'
Не уккзан тип возвращаемого функцией результата. Компилятор пишет, что "по умолчанию" он будет считать, что тип - int.
Т.к. в данном случае (по смыслу функции) возвращать ничего не надо, то ууказываем тип void:

Код: Выделить всё

  void  ProperFloatFraction(int n, int d){
		set(n, d);
	}

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 13:49
Albor
FloatFraction::get_float() нет закрывающей скобки }, заканчивающей тело функции.

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 14:03
Lotles
Albor писал(а):Приведи содержимое Fraction.h

Код: Выделить всё

#include "stdafx.h"
#include "conio.h"
using namespace std;
class Fraction{
private:
    int num, den;
	void normalize(){
		if (den == 0 || num == 0) {
        num = 0;
        den = 1;
		}
	}
	int gcf(int a, int b){
	    if (a % b == 0)
        return abs(b);
    else
        return gcf(b, a % b);
	}
	int lcm(int a, int b){
		return (a/gcf(a,b)*b);
	}
public:
    Fraction(){
		set(0, 1);
	}
    Fraction(int n, int d){
		set(n, d);
	}
	Fraction(int n){
		set(n,1);
	}
	Fraction(const Fraction& src){
		set(src.num,src.den);
	}
	void set(int n, int d){
		num = n; den = d; normalize();
	}
    int get_num() const{
		return num;
	}
    int get_den() const{
		return den;
	}
	Fraction add(const Fraction& other){
		Fraction fract;
    int lcd = lcm(den, other.den);
    int quot1 = lcd/den;
    int quot2 = lcd/other.den;
    fract.set(num * quot1 + other.num * quot2, lcd);
    fract.normalize();
    return fract;
	}
	Fraction mult(const Fraction& other){
		Fraction fract;
    fract.set(num * other.num, den * other.den);
    fract.normalize();
    return fract;
	}
    Fraction operator+(const Fraction& other){
		return add(other);
		}
    Fraction operator*(const Fraction& other){
		return mult(other);
	}   
	ostreram& operator << (ostream& os, Fraction& fr){
		os << fr.num << "/" << fr.den;
		return os;
	}
};

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 14:04
Lotles
Albor писал(а):Приведи содержимое Fraction.h

Код: Выделить всё

#include "stdafx.h"
#include "conio.h"
using namespace std;
class Fraction{
private:
    int num, den;
	void normalize(){
		if (den == 0 || num == 0) {
        num = 0;
        den = 1;
		}
	}
	int gcf(int a, int b){
	    if (a % b == 0)
        return abs(b);
    else
        return gcf(b, a % b);
	}
	int lcm(int a, int b){
		return (a/gcf(a,b)*b);
	}
public:
    Fraction(){
		set(0, 1);
	}
    Fraction(int n, int d){
		set(n, d);
	}
	Fraction(int n){
		set(n,1);
	}
	Fraction(const Fraction& src){
		set(src.num,src.den);
	}
	void set(int n, int d){
		num = n; den = d; normalize();
	}
    int get_num() const{
		return num;
	}
    int get_den() const{
		return den;
	}
	Fraction add(const Fraction& other){
		Fraction fract;
    int lcd = lcm(den, other.den);
    int quot1 = lcd/den;
    int quot2 = lcd/other.den;
    fract.set(num * quot1 + other.num * quot2, lcd);
    fract.normalize();
    return fract;
	}
	Fraction mult(const Fraction& other){
		Fraction fract;
    fract.set(num * other.num, den * other.den);
    fract.normalize();
    return fract;
	}
    Fraction operator+(const Fraction& other){
		return add(other);
		}
    Fraction operator*(const Fraction& other){
		return mult(other);
	}   
	ostreram& operator << (ostream& os, Fraction& fr){
		os << fr.num << "/" << fr.den;
		return os;
	}
};

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 14:06
Lotles
Ой удалите одно случайно получилось

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 14:43
Albor
Lotles, код не нужен уже. Смотри ошибки выше.

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 15:04
Lotles
А тут че
cout << "Value of f3 is " << f3.pr_proper(cout) << endl;
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

Re: Наследование(исправьте синтаксические ошибки)

Добавлено: 27 окт 2010, 15:20
BBB
Lotles писал(а):А тут че
cout << "Value of f3 is " << f3.pr_proper(cout) << endl;
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
А что вы ожидаете увидеть в исходящем потоке из ф-ии pr_proper(), если учесть, что это ф-ия, ничего не возвращающая (имеет тип резуьтата void)?