Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 27.10.2020, 19:04   #1
Александр222
Пользователь
 
Регистрация: 15.04.2020
Сообщений: 59
Вопрос c++. Не получается разделить код на файлы

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

jk.jpg

main.cpp
Код:
#include <iostream>
#include "Fraction.h"
#include "FazzyNumber.h"
 
 
int main() {
 
    Fraction R1(8, 3);
    Fraction R2(5, 4);
    Fraction Answer;
 
    R1.Addition(R2, Answer);
    std::cout << "R1 + R2 = " << Answer << std::endl;
 
    R1.Subtraction(R2, Answer);
    std::cout << "R1 - R2 = " << Answer << std::endl;
 
    R1.Multiplication(R2, Answer);
    std::cout << "R1 * R2 = " << Answer << std::endl;
 
    R1.Division(R2, Answer);
    std::cout << "R1 / R2 = " << Answer << std::endl;
 
    std::cout << "Equel:" << R1.Equel(R2) << std::endl;
    std::cout << "Greate:" << R1.Greate(R2) << std::endl;
    std::cout << "Less:" << R1.Less(R2) << std::endl;
 
    std::cout << "__________________________________________________________\n" << std::endl;
 
    FazzyNumber FN1(2, 10, 2);
    FazzyNumber FN2(1, 5, 1);
    FazzyNumber ANSWER;
 
    FN1.Addition(FN2, ANSWER);
    std::cout << "FN1 + FN2 = " << ANSWER << std::endl;
 
    FN1.Subtraction(FN2, ANSWER);
    std::cout << "FN1 - FN2 = " << ANSWER << std::endl;
 
    FN1.Multiplication(FN2, ANSWER);
    std::cout << "FN1 * FN2 = " << ANSWER << std::endl;
 
    FN1.Division(FN2, ANSWER);
    std::cout << "FN1 / FN2 = " << ANSWER << std::endl;
 
    FN1.ReverseNumber(FN2, ANSWER);
    std::cout << "Reverse number = " << ANSWER << std::endl;
}
Pair.h
Код:
#include <memory>
#include <iostream>
#include <functional>
 
class Pair {
public:
    double X;
    double Y;
 
    void SetX(double);
    void SetY(double);
    double GetX() const;
    double GetY() const;
 
    Pair();
    Pair(double, double);
    Pair(const Pair& other);
 
    virtual void  Addition(const Pair&, Pair&) = 0;
    virtual void Subtraction(const Pair&, Pair&) = 0;
    virtual void Multiplication(const Pair&, Pair&) = 0;
    virtual void Division(const Pair&, Pair&) = 0;
    virtual bool Equel(Pair&);
    virtual bool Greate(Pair&);
    virtual bool Less(Pair&);
 
    friend std::ostream& operator<<(std::ostream&, Pair&);
    friend std::istream& operator>>(std::istream&, Pair&);
};
Pair.cpp
Код:
#include "Pair.h"
#include <iostream>
#include <cmath>
 
Pair::Pair() : X{ 0 }, Y{ 0 }{};
Pair::Pair(double X, double Y) : X{ X }, Y{ Y } {};
Pair::Pair(const Pair& other) : X{ other.X }, Y{ other.Y } {};
 
void Pair::SetX(double X) { X = X; }
void Pair::SetY(double Y) { Y = Y; }
double Pair::GetX() const { return X; }
double Pair::GetY() const { return Y; }
bool Pair::Equel(Pair&) { return X == Y; }
bool Pair::Greate(Pair&) { return (X > Y); }
bool Pair::Less(Pair&) { return (X < Y); }
 
std::ostream& operator<<(std::ostream& out, Pair& P) {
    out << P.X << P.Y;
    return out;
}
std::istream& operator>>(std::istream& in, Pair& P) {
    in >> P.X >> P.Y;
    return in;
}
Fractio.h
Код:
#include "Pair.h"
 
class Fraction : public Pair {
public:
    Fraction();
    Fraction(double X, double Y);
    Fraction(const Fraction& other);
 
    void Addition(const  Pair&, Pair&) override;
    void Subtraction(const  Pair&, Pair&) override;
    void Multiplication(const  Pair&, Pair&) override;
    void Division(const  Pair&, Pair&) override;
 
    bool Equel(Pair&) override;
    bool Greate(Pair&) override;
    bool Less(Pair&) override;
 
    friend std::ostream& operator << (std::ostream& out, const Fraction& F);
};
Fractio.cpp
Код:
#include "Fraction.h"
 
Fraction::Fraction() : Pair() {};
Fraction::Fraction(double X, double Y) : Pair(X, Y) {};
Fraction::Fraction(const Fraction& other) : Pair(other) {};
void Fraction::Addition(const  Pair& other, Pair& answer) {
    answer.X = (X * other.Y) + (other.X * Y);
    answer.Y = Y * other.Y;
}
void Fraction::Subtraction(const Pair& other, Pair& answer) {
    answer.X = (X * other.Y) - (other.X * Y);
    answer.Y = Y * other.Y;
}
void Fraction::Multiplication(const Pair& other, Pair& answer) {
    answer.X = X * other.X;
    answer.Y = Y * other.Y;
}
void Fraction::Division(const Pair& other, Pair& answer) {
    answer.X = X * other.Y;
    answer.Y = Y * other.X;
}
bool Fraction::Equel(Pair& other) { return X == other.X && Y == other.Y; }
bool Fraction::Greate(Pair& other) { return (X * other.Y > Y * other.X); }
bool Fraction::Less(Pair& other) { return (X * other.Y < Y* other.X); }
std::ostream& operator<<(std::ostream& out, const Fraction& F) {
    out << F.X / F.Y;
    return out;
}
FazzyNumber.h
Код:
#include "Pair.h"
 
class FazzyNumber : public Pair {
    double A;
public:
    FazzyNumber();
    FazzyNumber(double e1, double A, double e2);
    FazzyNumber(const FazzyNumber& other);
 
    void Addition(const  Pair&, Pair&) override;
    void Subtraction(const  Pair&, Pair&) override;
    void Multiplication(const  Pair&, Pair&) override;
    void Division(const  Pair&, Pair&) override;
    void ReverseNumber(const  Pair&, Pair&);
 
    friend std::ostream& operator << (std::ostream& out, const FazzyNumber& FN);
    friend std::istream& operator>>(std::istream& in, FazzyNumber& FN);
};
FazzyNumber.cpp
Код:
#include "FazzyNumber.h"
 
FazzyNumber::FazzyNumber() : Pair() {};
FazzyNumber::FazzyNumber(double e1, double A, double e2) : Pair(e1, e2), A(A) {}
FazzyNumber::FazzyNumber(const FazzyNumber& other) : Pair(other) {};
void FazzyNumber::Addition(const Pair& other, Pair& answer) {
    const FazzyNumber& other2 = dynamic_cast<const FazzyNumber&>(other);
    FazzyNumber& answer2 = dynamic_cast<FazzyNumber&>(answer);
    answer2.A = A + other2.A;
    answer.X = A + other2.A - X - other.X;
    answer.Y = A + other2.A + Y + other.Y;
}
void FazzyNumber::Subtraction(const Pair& other, Pair& answer) {
    const FazzyNumber& other2 = dynamic_cast<const FazzyNumber&>(other);
    FazzyNumber& answer2 = dynamic_cast<FazzyNumber&>(answer);
    answer2.A = A - other2.A;
    answer.X = A - other2.A - X - other.X;
    answer.Y = A - other2.A + Y + other.Y;
}
void FazzyNumber::Multiplication(const Pair& other, Pair& answer) {
    const FazzyNumber& other2 = dynamic_cast<const FazzyNumber&>(other);
    FazzyNumber& answer2 = dynamic_cast<FazzyNumber&>(answer);
    answer2.A = A * other2.A;
    answer.X = A * other2.A - other2.A * X - A * other2.X + X * other.X;
    answer.Y = A * other2.A + other2.A * X + A * other2.X + X * other.X;
}
void FazzyNumber::Division(const Pair& other, Pair& answer) {
    const FazzyNumber& other2 = dynamic_cast<const FazzyNumber&>(other);
    FazzyNumber& answer2 = dynamic_cast<FazzyNumber&>(answer);
    answer2.A = A / other2.A;
    answer.X = (A - X) / (other2.A + other.Y);
    answer.Y = (A + other.X) / (other2.A - X);
}
void FazzyNumber::ReverseNumber(const Pair& other, Pair& answer) {
    const FazzyNumber& other2 = dynamic_cast<const FazzyNumber&>(other);
    FazzyNumber& answer2 = dynamic_cast<FazzyNumber&>(answer);
    answer2.A = 1 / A;
    answer.X = 1 / (A + other.Y);
    answer.Y = (A - X);
}
std::ostream& operator<<(std::ostream& out, const FazzyNumber& FN) {
    out << FN.X << ", " << FN.A << ", " << FN.Y;
    return out;
}
std::istream& operator>>(std::istream& in, FazzyNumber& FN) {
    int TMP;
    in >> TMP >> FN.A;
    FN.X = FN.A - TMP;
    in >> TMP;
    FN.Y = FN.A - TMP;
    return in;
}
Александр222 вне форума Ответить с цитированием
Старый 28.10.2020, 00:28   #2
BDA
МегаМодератор
СуперМодератор
 
Аватар для BDA
 
Регистрация: 09.11.2010
Сообщений: 7,289
По умолчанию

Интернет подсказывает, что нужно добавить include guard.
Пишите язык программирования - это форум программистов, а не экстрасенсов. (<= это подпись )
BDA на форуме Ответить с цитированием
Старый 28.10.2020, 07:11   #3
Алексей1153
фрилансер
Форумчанин
 
Регистрация: 11.10.2019
Сообщений: 960
По умолчанию

Александр222, в начале всех заголовочников добавь строку

Код:
#pragma once
Алексей1153 вне форума Ответить с цитированием
Старый 28.10.2020, 11:06   #4
Александр222
Пользователь
 
Регистрация: 15.04.2020
Сообщений: 59
По умолчанию

BDA,
Алексей1153, спасибо
Александр222 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Написать код в .BAT (Help) Nemai Помощь студентам 4 23.10.2020 07:30
HTML код AlanRaf2101 Общие вопросы по программированию, компьютерный форум 2 28.09.2020 21:39
Файлы(не получается написать малую часть кода) camel5 Общие вопросы C/C++ 0 03.04.2014 17:41
Не получается правильно переименовать файлы Janger Общие вопросы Delphi 7 20.02.2014 22:41
C++ (MinGW, g++) не получается запаковать файлы в zip Kib Общие вопросы C/C++ 0 23.02.2011 14:05