Nowe posty

xx Problem ze sterownikami. (5)
2024-04-13, 21:25:16
xx Instalacja xfce4 (2)
2024-04-13, 16:20:17
xx Serie kompilacji bez instalacji dla “emerge” w Gentoo (2)
2024-04-08, 18:40:04
xx Plasma 6 w Neonie ssie trochę mniej ... (17)
2024-04-05, 10:03:46
xx Problem z Linux Lite po instalacji (3)
2024-04-03, 14:23:40
xx Jak właczyć num locka przy starcie systemu debian 12? (12)
2024-04-02, 17:43:54
xx Brak dźwieku w systemie. (5)
2024-04-02, 16:13:41
xx Dystrybucja pod HP Omen (7)
2024-03-29, 11:33:05
xx [Poradnik] Wyszukiwanie Sterowników (2)
2024-03-27, 21:08:23
xx Ile pingwinów? (1)
2024-03-27, 08:59:24

Autor Wątek: Nie mogę utworzyć nowego obiektu na my_file?  (Przeczytany 1556 razy)

KelThuzad

  • Gość
Nie mogę utworzyć nowego obiektu na my_file?
« dnia: 2016-09-20, 19:12:31 »
Witam chce by mój program pewne swoje operacja przesyłał do plik ale wyskakuje mi komunikat no matching function for call to my_file::my_file() .

main:
#include "my_file.h"
#include "transactions.h"
#include "date.h"
#include <iostream>
#include <fstream>                                                      //DO KORZYSTANIA Z PLIKOW
#include <iomanip>                                                      //DO SETPRECISION
#include <stdexcept>                                                    //OBSLUGA BLEDOW
#include <string>
#include <sstream>                                                      //STRINGBUFFER
int main()
{
    my_file file( "dane.txt" ) ;
    my_file file_final;
    if ( file.is_file_opened() )
    {
        std::cerr << "Plik nie istnieje" << std::endl;
        std::exit ( EXIT_FAILURE );                                     //KONCZY PROGRAM
    }
    if ( file.is_file_empty() )
    {
        std::cerr << "Plik jest pusty" << std::endl;
        std::exit ( EXIT_FAILURE );                                     //KONCZY PROGRAM
    }
    double budget = {};
    try
    {
        budget = std::stod( file.add_budget() );
    }
    catch ( const std::invalid_argument&)
    {
        std::cerr << "Dane w pliku nie są liczbami" << std::endl;
        std::exit ( EXIT_FAILURE );                                     //KONCZY PROGRAM
    }

    std::cout << "budzet na starcie wynosi: " << std::setprecision(5) << budget << std::endl;
    transactions choice_of_payment , cost;
    date day_payment , month_payment , year_payment , buy ;
    int day , month , year ;
    double payment = {};
    char answer = {};
    std::stringstream date_final , string_budget ;
    std::string what_buy = {} , purchase = {} ;
    do
    {
        char choice = choice_of_payment.selec_cash_card_or_payment();
        if ( choice == '1' )
        {
            std::cout << "Wybrales\aa zaplate przez gotowke" << std::endl;
            std::cout << "Podaj date w ktorym zaplaciles\as gotowka" << std::endl;
            day = day_payment.day();
            month = month_payment.month();
            year = year_payment.year();
            date_final << day << "-" << month << "-" << year;
            std::cout << date_final.str() << std::endl;
            what_buy = buy.what_buying();
            budget = cost.payment_cash( budget );
            string_budget << budget;
            std::cout << "Teraz budzet wynosi: " << budget << std::endl;
            purchase = what_buy + " " + date_final.str() + " " + string_budget.str();
            std::cout << purchase << std::endl;
            file_final.add_informatio_to_file( purchase );
        }
        else if ( choice == '2' )
        {
            std::cout << "Wybrales\as zaplate przez carte bankowa" << std::endl;
        }
        else if ( choice == '3' )
        {
            std::cout << "Wybrales\as wplate gotowki na konto" << std::endl;
            payment = choice_of_payment.payment_cash_to_card();
            budget += payment;
            std::cout << "Wplaciles/as na konto: " << payment << " teraz na koncie masz: " << budget << std::endl;
            do
            {
                std::cout << "Czy chce zakonczyc program, jezeli tak to wpisz Y - yes" << std::endl;
                std::cout << "Jezeli nie chcesz zakonczyc to napisz N - no" << std::endl;
                std::cin >> answer;
            }
            while ( !((answer == 'Y' || answer == 'y') || (answer == 'N' || answer == 'n') ));
        }
        else
        {
            std::cerr << "Wybrales\as zla opcje" << std::endl;
        }
    }
    while ( !( answer == 'Y' || answer == 'y' ) );

    return 0;
}
my_file.cpp
#include "my_file.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

#include <iomanip>
#include <stdexcept>

my_file::my_file(std::string name_file)
{
    file.open( name_file , std::ios::in );
}
//==========================================================================
bool my_file::is_file_opened()
{
    if ( !file.good() )
    {
        return true;
    }
}
//==========================================================================
bool my_file::is_file_empty()
{
    file.seekp( 0 , std::ios::end );
    size_t size = file.tellg();
    if ( size == 0 )
    {
        std::cout << "Plik jest pusty" << std::endl;
    }
    file.seekp(0 , std::ios::beg);
    return ( size == 0 );
}
//==========================================================================
std::string my_file::add_budget()
{
    std::string line;
    std::getline( file , line );
    return line;
}
//==========================================================================
void my_file::add_informatio_to_file( std::string p )
{
    file_final.open( "budzet.txt" , std::ios::out );
    file_final << p ;
}
my_file.h
#ifndef MY_FILE
#define MY_FILE
#include <iostream>
#include <fstream>

#include <iomanip>
#include <stdexcept>

class my_file
{
private:
    std::fstream file;
    std::fstream file_final;
public:
    my_file ( std::string name_file );
    bool is_file_opened();
    bool is_file_empty();
    std::string add_budget();
    void add_informatio_to_file( std::string p );
};

#endif // MY_FILE

I jeszcze jedna sprawa chce zamknąć plik file przez file.close() ale nie mogę tego zrobić w main. I nie wiem gdzie teraz będzie najlepiej zamknąć plik. Może zrobie nową metodę w klasie my_file i na samym końcu programu ją wywołam i będzie ona wykonywała zamknięcia wszystkich plików.
« Ostatnia zmiana: 2016-09-21, 13:50:44 wysłana przez MateuszA »

Offline Paweł Kraszewski

  • Administrator
  • Guru
  • *****
  • Wiadomości: 3056
  • Lenistwo jest matką potrzeby = babcią wynalazku
    • Zobacz profil
Odp: Nie mogę utworzyć nowego obiektu na my_file?
« Odpowiedź #1 dnia: 2016-09-21, 14:54:47 »
my_file file( "dane.txt" ) ;
To wymaga konstruktora  my_file::my_file(std::string name_file), nie ma problemu.

my_file file_final;
To wymaga konstruktora  my_file::my_file(), a takiego konstruktora nie ma...
Paweł Kraszewski
~Arch/Void/Gentoo/FreeBSD/OpenBSD/Specjalizowane customy

KelThuzad

  • Gość
Odp: Nie mogę utworzyć nowego obiektu na my_file?
« Odpowiedź #2 dnia: 2016-09-21, 15:23:01 »
Czyli chodzi o to że w my_file.cpp brakuje mi konstruktora na my_file file_final?
O to miej więcej Paweł chodziło wiem że jest źle ale czy chociaż dobrze kombinuje?
my_file::my_file(std::string name_file)
{
    file.open( name_file , std::ios::in );
}
//=========================================================================
my_file::my_file(std::string name_file)
{
    file_final.open( name_file , std::ios::out );
    file_final << p;
}
I jeszcze nie wiem jak tutaj dodać jeszcze coś do pliku?
« Ostatnia zmiana: 2016-09-25, 09:06:40 wysłana przez MateuszA »