Struktura a klasy

0

Dzień Dobry
Mam za zadanie stworzyć program zawierający funkcje: WCZYTAJ_dane,
WYPISZ_dane, umożliwiający:

  • Stworzenie tablicy struktur w której będą przechowywane dane
    książek zgromadzonych w bibliotece: imię autora, nazwisko autora,
    tytuł, rok wydania, nr wydania
  • Wpisanie danych do tablicy
  • Wyświetlenie danych
    A następnie przerobić program na klasy. Pierwszą część zadania zrobiłam, lecz przy drugiej, wyskakują mi 3 błędy:
  1. Przy wersie 68: expected type-specifier before 'ksiazka' "
    2)Przy obu funkcjach: " 'wpisz'/'drukuj' was not declared in this scope"

Oto oba kody:
Struktury:

#include <iostream>

using namespace std;

struct ksiazka
{char imie_autora[15];
char nazwisko_autora[50];
char tytul[30];
int rok_wydania;
int nr_wydania;
};
void wpisz(ksiazka * ksiegozbior, int n)
{   cout<<endl;
    cout<<endl;
    cout << "---WPISYWANIE DANYCH KSIAZKI---"<< endl;
   for (int i = 0; i < n; i++){

        cout<<endl<<endl<<" Podaj dane ksiazki nr: "<<i+1<<" : "<<endl;
        cout<<"_______________________________________"<< endl<<endl;
      cout<<endl<< " Imie autora: ";
      cin >> ksiegozbior[i].imie_autora;
      cout<<endl;
      cout<< " Nazwisko autora: ";
      cin >> ksiegozbior[i].nazwisko_autora;
      cout<<endl;
      cout<<"Tytul ksiazki: ";
      cin>>ksiegozbior[i].tytul;
      cout<<endl;
      cout << " Rok wydania: ";
      cin >> ksiegozbior[i].rok_wydania;
      cout << " Nr wydania: ";
      cin >> ksiegozbior[i].nr_wydania;

   }
}

void drukuj(ksiazka * ksiegozbior, int nr) {
     int l,k;
     int x;
    cout<<endl;
        cout << "---DRUKOWANIE DANYCH KSIAZKI---"<< endl;
        cout << "__________________________________________________________________________ " << endl;

      cout << " Podaj nr ksiazki: ";
      cin >> nr;


    cout<<endl <<endl;
   cout << " Imie autora: " << ksiegozbior[nr-1].imie_autora << endl;
   cout << " Nazwisko autora: " <<ksiegozbior[nr-1].nazwisko_autora<< endl;
   cout << " Tytul ksiaki: " << ksiegozbior[nr-1].tytul << endl;
   cout << " Rok wydania: " << (ksiegozbior[nr-1].rok_wydania) << endl;
   cout << " Nr wydania: " << ksiegozbior[nr-1].nr_wydania << endl;
   cout<<endl<<endl;

    //}
}
int main()
{
    int a;
    int b;
    cout<<"                                  ---WITAJ!---                                       "<<endl;
   cout << "                   Ilu ksiazek chcesz wpisac ? "<<endl;
   cin >> a;

   ksiazka * ksiegozbior = new ksiazka[a];
   wpisz(ksiegozbior,a);
   drukuj(ksiegozbior,b);
   return 0;
} 

Oraz klasy :

 
#include <iostream>

using namespace std;

class Ksiazka
{
    char imie_autora[15];
    char nazwisko_autora[50];
    char tytul[30];
    int rok_wydania;
    int nr_wydania;
public:
    void wpisz(Ksiazka * ksiegozbior, int n)
{   int i;
    cout<<endl;
    cout<<endl;
    cout << "---WPISYWANIE DANYCH KSIAZKI---"<< endl;

        cout<<endl<<endl<<" Podaj dane ksiazki nr: "<<i+1<<" : "<<endl;
        cout<<"_______________________________________"<< endl<<endl;
        cout<<endl<< " Imie autora: ";
        cin >> imie_autora;
        cout<<endl;
        cout<< " Nazwisko autora: ";
        cin >> nazwisko_autora;
        cout<<endl;
        cout<<"Tytul ksiazki: ";
        cin>> tytul;
        cout<<endl;
        cout << " Rok wydania: ";
        cin >> rok_wydania;
        cout << " Nr wydania: ";
        cin >> nr_wydania;
}
void drukuj(Ksiazka * ksiegozbior, int nr) {

    cout<<endl;
        cout << "---DRUKOWANIE DANYCH KSIAZKI---"<< endl;
        cout << "__________________________________________________________________________ " << endl;

        cout << " Podaj nr ksiazki: ";
        cin >> nr;


    cout<<endl <<endl;
    cout << " Imie autora: " << imie_autora << endl;
    cout << " Nazwisko autora: " <<nazwisko_autora<< endl;
    cout << " Tytul ksiaki: " << tytul << endl;
    cout << " Rok wydania: " << rok_wydania << endl;
    cout << " Nr wydania: " << nr_wydania << endl;
    cout<<endl<<endl;

    //}
}
};

int main()
{
    int a;
    int b = 0;
    cout<<"                                  ---WITAJ!---                                       "<<endl;
    cout << "                   Ilu ksiazek chcesz wpisac ? "<<endl;
    cin >> a;

    Ksiazka * ksiegozbior = new Ksiazka[a];

    for(int i = 0; i < a; ++i)
          ksiegozbior[i] -> wpisz();

    ksiegozbior[b] -> drukuj();

    return 0;
}

Będę wdzięczna za jakąkolwiek pomoc
Pozdrawiam

6
  1. Fatalne formatowanie, jak nie umiesz sam to polecam http://format.krzaq.cc ,
  2. drukuj(ksiegozbior,b); ⟵ nigdzie nie wczytujesz b, to jest UB,
  3. funkcja drukuj nie powinna nic wczytywać,
  4. funkcja wpisz nie powinna nic wypisywać,
  5. nazewnictwo funkcji jest niezgodne z przedstawionym zadaniem,
  6. w drugiej wersji funkcje drukuj i wpisz są niestatycznymi członkami klasy bez żadnego wyraźnego powodu, do tego są wołane ze złą ilością argumentów,
  7. ksiegozbior[b] nie jest wskaźnikiem, użyj . zamiast ->,
  8. piszesz w C++, używaj std::string zamiast tablic charów o ile nie ma wyraźnych powodów aby tak robić (tutaj: nie ma),
  9. http://4programmers.net/Pomoc/Forum_dyskusyjne/Dlaczego_nikt_nie_odpowiada_w_moim_w%C4%85tku ⟵ punkty 1, 4, 5

tyle na szybko

2

Masz trochę widzę podejście z czystego C. Mając metodę wewnątrz klasy nie trzeba do niej przekazywać obiektu tej klasy, żeby coś na tym obiekcie robiła. Zamiast tego stosuj this. Możesz np. zastosować taką strukturę:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
struct author{
	string name;
	string surname;
	author(const string& name, const string& surname):name(name),surname(surname){}
};

struct book{
	author auth;
	string title;
	int releaseYear;
	int releaseNo;
	book(const string& authorName, const string& authorSurname, const string& title, int year, int no)
		:auth(author(authorName, authorSurname)),title(title),releaseYear(year),releaseNo(no){}
};

class library{
private:
	vector<book> books;

public:
	void add(const book& book){ this->books.push_back(book); }
	void print(){
		for(unsigned i=0;i<this->books.size();++i){
			cout << i + 1 << ". " <<  this->books[i].title << endl
				 << "Author: " <<  this->books[i].auth.name << " " << this->books[i].auth.surname << endl
				 << "Release date: " << this->books[i].releaseYear << endl
				 << "Release no: " << this->books[i].releaseNo << endl << endl;
		}
	}
};

int main()
{
	library lib;
	lib.add(book("Adam", "Mickiewicz", "Pan Tadeusz", 1834, 12));
	lib.add(book("Boleslaw", "Prus", "Lalka", 1887, 3));
	lib.print();
    return 0;
}

http://ideone.com/Xgt2ba

0

Bardzo przepraszam, że tak piszę, ale trochę mam nóż na gardle, a nie do końca rozumiem jak ostatecznie powinien wyglądać program z klasą. Starałam się działać wg. poprzednich podpowiedzi, lecz wyskakiwały mi kolejne błędy :( :(

2

@_13th_Dragon nooo w sumie masz rację. Teraz chyba będzie lepiej:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct book{
	string title;
	int releaseYear;
	int releaseNo;
	book(const string& title, int year, int no):title(title),releaseYear(year),releaseNo(no){}
};

struct author{
	unsigned id;
	string name;
	string surname;
	vector<book> books;
	author(const string& name, const string& surname):id(0),name(name),surname(surname){}
};

class library{
private:
	vector<author> authors;

public:
	void add(const author& author){ 
		this->authors.push_back(author);
		this->authors[this->authors.size()-1].id = this->authors.size()-1;
	}

	void add(const book& book, unsigned authorId){
		for(unsigned i=0;i<this->authors.size();++i)
			if(this->authors[i].id == authorId) {
				this->authors[i].books.push_back(book);
				break;
			}
	}

	void showAuthor(unsigned authorId){
		for(unsigned i=0;i<this->authors.size();++i)
			if(this->authors[i].id == authorId){
				cout << "Selected author: " << this->authors[i].name << " " << this->authors[i].surname << endl;
				break;
			}
	}

	void showBooks(unsigned authorId){
		for(unsigned i=0;i<this->authors.size();++i)
			if(this->authors[i].id == authorId){
				cout << "Author: " << this->authors[i].name << " " << this->authors[i].surname 
					 << endl << endl << "-- Books --" << endl;
				for(unsigned j=0;j<this->authors[i].books.size();++j){
					cout << "Title: " << this->authors[i].books[j].title << endl
						 << "Release year: " << this->authors[i].books[j].releaseYear << endl
						 << "Release no: " << this->authors[i].books[j].releaseNo << endl << endl;
				}
				break;
			}
	}
};

int main()
{
	library lib;
	lib.add(author("Henryk","Sienkiewicz"));
	lib.add(author("Adam","Mickiewicz"));

	lib.add(book("W pustyni i w puszczy", 1999,12), 0);
	lib.add(book("Jakas ksiazka Sienkiewicza", 1999,12), 0);
	lib.add(book("I kolejna Sienkiewicza", 1999,12), 0);

	lib.add(book("Pan Tadeusz", 1999,12), 1);

	lib.showBooks(0);
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1