Pliki źrodłowe a projekt

0

Witam ;) Stworzyłem już projekt który działa w 100% jeżeli jest w jednym pliku. Teraz muszę podzielić go na pliki źródłowe, ale po podzieleniu już niestety wyskakują mi błędy że np struktura jest zredefiniowana, poczytałem w błędzie i wynika to prawdopodobnie z tego że źle połączyłem pliki lub coś.

Tutaj wrzucam moje kody.

Task.h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
 
 
struct Task
{
    string question;
    string answers[ 5 ];
    char correct_answer;
    int question_score;
    void showTask();

 
    Task( string q, string a1, string a2, string a3, string a4, string a5, char ca, int qs )
    {
        question = q;
        answers[ 0 ] = a1;
        answers[ 1 ] = a2;
        answers[ 2 ] = a3;
        answers[ 3 ] = a4;
        answers[ 4 ] = a5;
        correct_answer = ca;
        question_score = qs;
    }
};

Task.cpp

void Task::showTask()
    {
        cout << "\n";
        cout << question << "\n";
        for( int i = 0; i < 5; i++ ) {
            cout << answers[ i ];
            cout << endl;
        }
    }

TestContent.h

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

class TestContent
{
	private:
    char guess;
    int total=0;
    vector<Task> tasks;
 
public:
    int get_total();
    char get_guess();
    string get_question(int it);
    void set_total(int t);
    void set_guess(char g);
    void addTask(const Task& t);
    void removeTask(size_t idx);
    void accessQuestion(int it);
    void showAllQuestions();
    void askQuestion(const int& it);
};

TestContent.cpp


	int TestContent::get_total(){
		return total;
	}
    
	char TestContent::get_guess(){
		return guess;
	}

	string TestContent::get_question(int it) //getter do vectora pytań
	{
   return tasks[it].question;
	}
	
	void TestContent::set_total(int t)
	{
	total = t;
	}

	void TestContent::set_guess(char g)
	{
	guess = g;
	}
     void TestContent::addTask(const Task& t) // funkcja dodawania Task do wektora
	{
        tasks.push_back(t);
	}

	void TestContent::removeTask(size_t idx) //funkcja usuwająca pytanie z wektora
	{
    assert(idx < tasks.size());
    tasks.erase(begin(tasks) + idx);
	}
 
    void TestContent::accessQuestion(int it) // funkcja wyświetlania po indeksie w wektorze
	{
		assert(it < tasks.size());
        tasks[it].showTask();
	}

	void TestContent::showAllQuestions() // funkcja wyswietlajaca wszystkie pytania z wektora
	{
        for(int i=0; i<tasks.size(); i++)   
		{
            askQuestion(i);
        }
    	cout<<"Your score is: "<<total<<endl;
		if (total > 30)
		cout <<"exam passed"<<endl;
        else
        cout<<"exam failed"<<endl;
	}
    
	void TestContent::askQuestion(const int& it) // funkcja showTask + żądanie odpowiedzi
{

no i main.cpp

 Task t1( stworzone pytanie..);
    Task t2(stworzone pytanie..);
    Task t3(stworzone pytanie..);

//    t2.showTask(); // działa

 
    TestContent test; // działa
    test.addTask(t1); // działa
    test.addTask(t2);
    test.addTask(t3);

    
    test.showAllQuestions(); // działa
	test.accessQuestion(1); // działa
	test.removeTask(1); //działa
	test.askQuestion(1); // działa

    return 0;
}

Może mi ktoś powiedzieć, jaki #include mam zrobić do jakiego pliku? Pozdrawiam

1

Pliki nagłówkowe powinny mieć dyrektywy #ifndef: https://pl.wikipedia.org/wiki/Plik_nagłówkowy
Nie powinny natomiast mieć using namespace, co za tym idzie w pliku nagłówkowym zawsze stosujemy pełną nazwę, np std::string.

Może mi ktoś powiedzieć, jaki #include mam zrobić do jakiego pliku?

Plik .cpp dołącza plik .h o tej samej nazwie, TestContent.h dołącza Task.h, main dołącza TestContent.h i powinno działać.

0

Aktualne kody .h

Task.h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#ifndef _TASK_H
#define _TASK_H

struct Task
{
    std::string question;
    std::string answers[ 5 ];
    char correct_answer;
    int question_score;
    void showTask();

 
    Task( std::string q, std::string a1, std::string a2, std::string a3, std::string a4, std::string a5, char ca, int qs )
    {
        question = q;
        answers[ 0 ] = a1;
        answers[ 1 ] = a2;
        answers[ 2 ] = a3;
        answers[ 3 ] = a4;
        answers[ 4 ] = a5;
        correct_answer = ca;
        question_score = qs;
    }
};

#endif

TestContent.H

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#ifndef _TESTCONTENT_H
#define _TESTCONTENT_H
#include "Task.h"

class TestContent
{
	private:
    char guess;
    int total=0;
    std::vector<Task> tasks;
 
public:
    int get_total();
    char get_guess();
    std::string get_question(int it);
    void set_total(int t);
    void set_guess(char g);
    void addTask(const Task& t);
    void removeTask(size_t idx);
    void accessQuestion(int it);
    void showAllQuestions();
    void askQuestion(const int& it);
};

#endif

Dodatkowo:

Plik Task.cpp zawiera
#include Task.h

Plik TestContent.cpp zawiera
#include TestContent.h

main.cpp zawiera:
#include "TestContent.h"

i przy próbie kompilacji wywala mi coś takiego:
https://zapodaj.net/3f90ec4dcf5e6.jpg.html

0

Przecież widzisz błąd - undefined reference do funkcji.

Pokaż jak budujesz "projekt".

0

Masz na myśli funkcje wywoływane w mainie? Czy "budowanie" masz na myśli wszystkie kody których używam w projekcie?
Jak main to tutaj jest, a pozostałe kody są w postach wyżej. Poprawiony header w 2 poście, cpp bez zmian w 1 poście.

#include "TestContent.h"

int main() {
 
    Task t1( stworzone pytanie );
    Task t2(stworzone pytanie);
    Task t3(stworzone pytanie);
    Task t4(Stworzone pytanie);
    Task t5(Stworzone pytanie);
    Task t6(Stworzone pytanie);
    Task t7(Stworzone pytanie);
    Task t8(Stworzone pytanie);
    Task t9(Stworzone pytanie);
    Task t10(Stworzone pytanie);
    
//    t2.showTask(); // działa

 
    TestContent test; // działa
    test.addTask(t1); // działa
    test.addTask(t2);
    test.addTask(t3);
    test.addTask(t4);
    test.addTask(t5);
    test.addTask(t6);
    test.addTask(t7);
    test.addTask(t8);
    test.addTask(t9);
    test.addTask(t10);
    
    test.showAllQuestions(); // działa
	test.accessQuestion(1); // działa
	test.removeTask(1); //działa
	test.askQuestion(1); // działa

    return 0;
}

Dodam tylko że jak miałem wszystko w 1 pliku to wszystko działało, więc nie rozkminiam teraz do końca co się dzieje ;)

1

Po pierwsze #ifndef powinno być na samym początku pliku, a nie po iluś tam #include.

Błąd oznacza brak implementacji dla metod klasy TestContent, więc albo plik .cpp nie dołącza pliku .h, albo nie dodałeś go do projektu.

0

Dziękuje Twonek za pomoc.

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