Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Dzielenie kodu na kilka plików

Ostatnio zmodyfikowano 2012-07-06 18:33
Autor Wiadomość
Unlimited
Temat założony przez niniejszego użytkownika
Dzielenie kodu na kilka plików
» 2012-07-06 15:30:08
Witam. Mam problem z podziałem kodu na kilka plików jeśli zastosuję w głównym pliku klasę a w innym funkcje do tej klasy.
Mój kod:

main.cpp
C/C++
#include <iostream> //podstawowa biblioteka do obslugi m.in. cout, cin
#include <string> //biblioteka do obslugi lancuchów znaków
#include <fstream> //biblioteka do obslugi plików
#include <cstdlib> //biblioteka do obslugi komend m.in. ( zatrzymanie programu, usuwanie pliku )
#include <vector> //biblioteka potrzebna do stworzenia nieskonczonej tablicy
using namespace std; //zadeklarowanie przestrzeni nazw

class books //glowna klasa programu
{
private:
    string autor, tytul, wydawnictwo, xautor, xtytul, xwydawnictwo, sciezka, fraza;
    int rok, strony, xrok, xstrony, ilosc, n, x, liczba, dol, gora;
    size_t ZnalezionaFraza;
    fstream plik;
    vector < books > ksiazka;
    books( string xautor, string xtytul, string xwydawnictwo, int xrok, int xstrony );
public:
    int wybor1, wybor2, wybor3;
    bool sprawdz;
    books();
    void clear();
    void header();
    void add_book();
    void edit_book();
    void remove_book();
    void search_autor();
    void search_tytul();
    void search_wydawnictwo();
    void search_rok();
    void search_strony();
    void view_all_books();
    void create_file();
    void remove_file();
    void load_file();
    void path_file();
    void view_path_file();
    void information();
};

books::books( string xautor, string xtytul, string xwydawnictwo, int xrok, int xstrony ) //konstruktor nr 1
    : autor( xautor )
    , tytul( xtytul )
    , wydawnictwo( xwydawnictwo )
    , rok( xrok )
    , strony( xstrony )
{
}

books::books() //konstruktor nr 2
{
    n = 0;
}

#include "function.hpp" //dolaczenie pliku z kodem do programu

int main() //glowna czesc programu
{
    books book;
   
    do
    {
        do
        {
            system( "cls" );
            book.header();
            cout << "[1] Dodaj nowa ksiazke" << endl;
            cout << "[2] Edytuj ksiazke" << endl;
            cout << "[3] Usun ksiazke" << endl;
            cout << "[4] Wyswietl/Wyszukaj ksiazki" << endl;
            cout << "[5] Opcje" << endl;
            cout << "[6] Wykonanie/Kontakt" << endl;
            cout << "[0] Zakoncz" << endl << endl;
            cout << "Twoj wybor: ";
            book.clear();
            cin >> book.wybor1;
            book.sprawdz = cin.good();
            system( "cls" );
        } while( book.sprawdz == false );
       
        switch( book.wybor1 )
        {
        case 1:
            {
                book.add_book();
                break;
            }
        case 2:
            {
                book.edit_book();
                break;
            }
        case 3:
            {
                book.remove_book();
                break;
            }
        case 4:
            {
                do
                {
                    do
                    {
                        system( "cls" );
                        book.header();
                        cout << "[1] Szukaj - Tytul" << endl;
                        cout << "[2] Szukaj - Autor" << endl;
                        cout << "[3] Szukaj - Wydawnictwo" << endl;
                        cout << "[4] Szukaj - Rok wydania" << endl;
                        cout << "[5] Szukaj - Ilosc stron" << endl;
                        cout << "[6] Pokaz wszystkie" << endl;
                        cout << "[0] Powrot" << endl << endl;
                        cout << "Twoj wybor: ";
                        book.clear();
                        cin >> book.wybor2;
                        book.sprawdz = cin.good();
                        system( "cls" );
                    } while( book.sprawdz == false );
                   
                    switch( book.wybor2 )
                    {
                    case 1:
                        {
                            book.search_tytul();
                            break;
                        }
                    case 2:
                        {
                            book.search_autor();
                            break;
                        }
                    case 3:
                        {
                            book.search_wydawnictwo();
                            break;
                        }
                    case 4:
                        {
                            book.search_rok();
                            break;
                        }
                    case 5:
                        {
                            book.search_strony();
                            break;
                        }
                    case 6:
                        {
                            book.view_all_books();
                            break;
                        }
                    }
                } while( book.wybor2 );
               
                break;
            }
        case 5:
            {
                do
                {
                    do
                    {
                        system( "cls" );
                        book.header();
                        cout << "[1] Utworz plik z danymi" << endl;
                        cout << "[2] Usun plik z danymi" << endl;
                        cout << "[3] Podaj sciezke pliku" << endl;
                        cout << "[4] Zaladuj plik" << endl;
                        cout << "[5] Sprawdz aktualny odnosnik pliku" << endl;
                        cout << "[0] Powrot" << endl << endl;
                        cout << "Twoj wybor: ";
                        book.clear();
                        cin >> book.wybor3;
                        book.sprawdz = cin.good();
                        system( "cls" );
                    } while( book.sprawdz == false );
                   
                    switch( book.wybor3 )
                    {
                    case 1:
                        {
                            book.create_file();
                            break;
                        }
                    case 2:
                        {
                            book.remove_file();
                            break;
                        }
                    case 3:
                        {
                            book.path_file();
                            break;
                        }
                    case 4:
                        {
                            book.load_file();
                            break;
                        }
                    case 5:
                        {
                            book.view_path_file();
                            break;
                        }
                    }
                } while( book.wybor3 );
               
                break;
            }
        case 6:
            {
                book.information();
                break;
            }
        }
    } while( book.wybor1 );
   
    return 0; //zamkniecie programu
}

function.cpp
C/C++
#include "function.hpp"

void books::clear()
{
   
}

void books::header()
{
   
}

void books::add_book()
{
   
}

void books::edit_book()
{
   
}

void books::remove_book()
{
   
}

void books::search_autor()
{
   
}

void books::search_tytul()
{
   
}

void books::search_wydawnictwo()
{
   
}

void books::search_rok()
{
   
}

void books::search_strony()
{
   
}

void books::view_all_books()
{
   
}

void books::create_file()
{
   
}

void books::remove_file()
{
   
}

void books::load_file()
{
   
}

void books::path_file()
{
   
}

void books::view_path_file()
{
   
}

void books::information()
{
   
}

function.hpp
C/C++
#ifndef function_hpp
#define function_hpp

void books::clear();
void books::header();
void books::add_book();
void books::edit_book();
void books::remove_book();
void books::search_autor();
void books::search_tytul();
void books::search_wydawnictwo();
void books::search_rok();
void books::search_strony();
void books::view_all_books();
void books::create_file();
void books::remove_file();
void books::load_file();
void books::path_file();
void books::view_path_file();
void books::information();

#endif //function_hpp

Błąd przy kompilowaniu to:
||=== Uproject, Debug ===|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|4|error: declaration of 'void books::clear()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|5|error: declaration of 'void books::header()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|6|error: declaration of 'void books::add_book()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|7|error: declaration of 'void books::edit_book()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|8|error: declaration of 'void books::remove_book()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|9|error: declaration of 'void books::search_autor()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|10|error: declaration of 'void books::search_tytul()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|11|error: declaration of 'void books::search_wydawnictwo()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|12|error: declaration of 'void books::search_rok()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|13|error: declaration of 'void books::search_strony()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|14|error: declaration of 'void books::view_all_books()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|15|error: declaration of 'void books::create_file()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|16|error: declaration of 'void books::remove_file()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|17|error: declaration of 'void books::load_file()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|18|error: declaration of 'void books::path_file()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|19|error: declaration of 'void books::view_path_file()' outside of class is not definition|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|20|error: declaration of 'void books::information()' outside of class is not definition|
||=== Build finished: 17 errors, 0 warnings ===|

Co jest nie tak?
P-59577
kampar
» 2012-07-06 15:48:34
w main.cpp powinna być sama funkcja main, natomiast w function.hpp wywal całą tą bzdurną treść i daj tam deklarację klasy

EDIT: pliki nagłówkowe spełniają rolę łącznika między plikami .cpp. Informują plik main.cpp, że istnieje gdzieś indziej deklaracja metod klasy
P-59578
Unlimited
Temat założony przez niniejszego użytkownika
» 2012-07-06 15:53:47
Jaką bzdurną? Tak jest napisane w kursie na tej stronie... W ogóle moim zdaniem to jest trochę chore to dzielenie na pliki bo dołączamy w main plik .hpp w którym nie ma #include .cpp a jakoś ten .hpp widzi tego .cpp a zaś w .cpp jest #include .hpp chociaż już jest w main dodane .hpp i tak w kółko.
function.cpp >> function.hpp >> (tutaj jakimś cudem function.cpp chociaż w function.hpp nie ma #include .cpp) >> function.cpp >> i tak w kółko...
P-59579
kampar
» 2012-07-06 16:40:50
Nie, tak nie jest napisane w kursie. Tam w pierwszym przykładzie w pliku .hpp jest po prostu deklaracja zwykłej funkcji i analogicznie trzyma się tam też deklaracje wszystkich klas. Pisząc większe projekty będziesz musiał utworzyć kilka klas. W pliku .hpp trzyma się wszystkie deklaracje, czyli same nazwy klasy i jej metod. Teraz do pliku main.cpp dołączamy plik .hpp. Dzięki temu możemy korzystać z danej klasy. Plik main.cpp zagląda do pliku nagłówkowego i widzi tam tylko deklaracje klas i ich metod, nie wie natomiast nic o ich definicji, czyli co poszczególna metoda robi. Po prostu ślepo wierzy, że te definicje gdzieś są (jak ich niema to kompilator zwróci błąd). I gdy w pliku main.cpp wywołujemy jakąś funkcję zadeklarowaną w pliku nagłówkowym, to plik nagłówkowy szuka definicji tej funkcji w plikach do których jest dołączony.
P-59582
Unlimited
Temat założony przez niniejszego użytkownika
» 2012-07-06 17:03:26
Zrobiłem tak jak pisałeś. Usunąłem zawartość function.hpp i wstawiłem tam całą deklarację klasy books. Teraz mam takie błędy:
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|7|error: 'string' does not name a type|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|9|error: 'size_t' does not name a type|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|10|error: 'fstream' does not name a type|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|11|error: ISO C++ forbids declaration of 'vector' with no type|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|11|error: expected ';' before '<' token|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.hpp|12|error: expected ')' before 'xautor'|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|3|error: expected ')' before 'xautor'|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp||In member function 'void books::clear()':|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|19|error: 'cin' was not declared in this scope|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp||In member function 'void books::header()':|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|25|error: 'cout' was not declared in this scope|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|25|error: 'endl' was not declared in this scope|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp||In member function 'void books::information()':|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|102|error: 'cout' was not declared in this scope|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|102|error: 'endl' was not declared in this scope|
C:\Users\AcerAS-7552G\Desktop\Uproject\function.cpp|117|error: 'system' was not declared in this scope|
||=== Build finished: 13 errors, 0 warnings ===|
P-59585
kampar
» 2012-07-06 17:17:33
wystarczy przeczytać błędy, aby zobaczyć co jest nie tak. Masz kilka błędów składniowych, zobacz w której linii jest błąd i go napraw. Zamiast string musisz dać std::string i tak samo przy fstream, size_t, cout i endl. Dalej gdzieś w kodzie masz błędne deklaracje metod klasy typu void books::clear().
P-59587
DejaVu
» 2012-07-06 18:33:35
C/C++
//plik klasa.h
#pragma once

class Klasa
{
public:
    int a();
    void b();
};

C/C++
//plik klasa.cpp
#include "klasa.h"

int Klasa::a()
{
    return 0;
}

void Klasa::b()
{
}

C/C++
//plik: main.cpp
#include "klasa.h"

int main()
{
    Klasa xyz;
    return 0;
}
P-59595
« 1 »
  Strona 1 z 1