Upadek programu podczas proby swtorzenia obiektu.

0

Siema,
posiadam następujący kod:

#pragma once
#include <string>
class Person
{
private:
	std::string name_;
	std::string sirname_;
public:
	virtual ~Person() {}
	Person();
	Person(const std::string&, const std::string&);
	Person(const Person&);
	virtual void Show();
};
class Gunslinger :virtual public Person
{
private:
	double time_of_reaction;
	int scares;
protected:
	virtual void data();
public:
	Gunslinger() :Person(), time_of_reaction(1) {}
	Gunslinger(const std::string n, const std::string s, double t);
	Gunslinger(const Person& p, double t);
	Gunslinger(const Gunslinger& G);
	virtual ~Gunslinger() {};
	virtual void Show();
	long making_scares();
};
class Card
{
private:
	std::string color;
	int number;
	std::string& color_of_c();
	int draw_number();
public:
	virtual ~Card() {}
	Card();
	virtual void Show();
};
class PokerPlayer :virtual public Person, private Card
{
protected:
	virtual void data();
public:
	PokerPlayer() :Person(), Card() {}
	PokerPlayer(const std::string& n, const std::string& s);
	PokerPlayer(const Person& p);
	virtual ~PokerPlayer() {}
	virtual void Show();

};
class BadDUDE :public Gunslinger, public PokerPlayer
{
private:
	static std::string extra_;
protected:
	virtual void data();
public:
	BadDUDE(const std::string& n, const std::string& s, double t);
	BadDUDE(const Person& P, double t);
	virtual ~BadDUDE() {}
	void Gdraw();
	void Cdraw();
	virtual void Show();
};
#include "Person.h"
#include <string>
#include <ctime>
#include <cstdlib>
#include <iostream>

std::string BadDUDE::extra_ = "FULL OPCJA";
Person::Person()
{
    name_ = "brak";
    sirname_ = "brak";
}
Person::Person(const std::string& n, const std::string& s)
{
    name_ = n;
    sirname_ = s;
}
Person::Person(const Person&)
{
    name_ = name_;
    sirname_ = sirname_;
}
void Person::Show()
{
    std::cout << "Imie: " << name_ << std::endl;
    std::cout << "Nazwisko: " << sirname_ << std::endl;
}
Gunslinger::Gunslinger(const std::string n, const std::string s, double t)
    : Person(n, s)
{
    time_of_reaction = t;
    scares = making_scares();
}
Gunslinger::Gunslinger(const Person& p, double t)
    : Person(p)
{
    time_of_reaction = t;
    scares = making_scares();
}
Gunslinger::Gunslinger(const Gunslinger& G)
    : Person(G)
{
    time_of_reaction = G.time_of_reaction;
    scares = G.scares;
}
void Gunslinger::Show()
{
    Person::Show();
    data();
}

long Gunslinger::making_scares()
{
    std::srand(std::time(NULL));
    long x = rand() % 10;
    return x;
}
void Gunslinger::data()
{
    std::cout << "Czas reakcji: " << time_of_reaction << std::endl;
    std::cout << "Liczba naciec: " << scares << std::endl;
}
Card::Card()
{
    color = color_of_c();
    number = draw_number();
}
std::string& Card::color_of_c()
{
    std::string C;
    std::srand(std::time(NULL));
    int x = rand() % 2 + 1;
    switch (x) {
    case 1:
        C = "czerwona";
        break;
    case 2:
        C = "czarna";
        break;
    }
    return C;
}
int Card::draw_number()
{
    std::srand(std::time(NULL));
    int x = rand() % 52 + 1;
    return x;
}
void Card::Show()
{
    std::cout << "Kolor karty to: " << color << std::endl;
    std::cout << "Numer karty to: " << number << std::endl;
}
void PokerPlayer::data()
{
    Card::Show();
}
PokerPlayer::PokerPlayer(const std::string& n, const std::string& s)
    : Person(n, s)
    , Card()
{
}

PokerPlayer::PokerPlayer(const Person& p)
    : Person(p)
    , Card()
{
}
void PokerPlayer::Show()
{
    Person::Show();
    data();
}
void BadDUDE::Gdraw()
{
    Gunslinger::data();
}
void BadDUDE::Cdraw()
{
    PokerPlayer::data();
}
void BadDUDE::data()
{
    Gunslinger::data();
    PokerPlayer::data();
}
void BadDUDE::Show()
{
    Person::Show();
    BadDUDE::data();
    std::cout << extra_ << std::endl;
}
BadDUDE::BadDUDE(const std::string& n, const std::string& s, double t)
    : Person(n, s)
    , Gunslinger(n, s, t)
    , PokerPlayer(n, s)
{
}
BadDUDE::BadDUDE(const Person& P, double t)
    : Person(P)
    , Gunslinger(P, t)
    , PokerPlayer(P)
{
}
```cpp

Problem w tym ze program sypie sie przy probie stworzenia obiektu PokerPlayer, wyrzucajac blad o treści:
Nieobsłużony wyjątek w lokalizacji 0x73BE2CD2 w 2v3.exe: wyjątek języka Microsoft C++: std::length_error w lokalizacji pamięci 0x010FEF0C.
Ktos moze spojrzy świeżym okiem i powie mi gdzie znajduje sie luka?

1

To może pokaż w jaki sposób tworzysz ten obiekt? ;)

3

Zamień

std::string& color_of_c();    

na

std::string color_of_c();    

To co w tej chwili robisz to zwracasz referencję do obiektu tymczasowego (utworzonego na stosie), który po wyjściu z funkcji color_of_c przestaje istnieć i w efekcie prowadzi do UB.

Dodatkowo kod powyższej funkcji możesz skrócić do następującej postaci.

std::string Card::color_of_c()
{   
    return (rand()%2)==0 ? "czerwona" : "czarna";
}
4

Na marginesie: źle podchodzisz do tematu - dziedziczenie jest tutaj tragicznym wyjściem: https://gameprogrammingpatterns.com/component.html, https://gameprogrammingpatterns.com/type-object.html

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