Witam. Robię pewien program jako zadanie, i napotkałem się na problem.
error C2594: "=": niejednoznaczne konwersje z "BadDude *" do "Person *"
#include "stdafx.h"
#include "Person.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void Person::Set()
{
cout << "Podaj imie ";
cin >> Name;
cout << "Podaj nazwisko ";
cin >> Surrname;
}
void Person::Show()
{
cout << "Imie: " << Name << endl;
cout << "Nazwisko: " << Surrname << endl;
}
void Gunslinger::Set()
{
Person::Set();
Ustaw();
}
void Gunslinger::Ustaw()
{
cout << "Podaj czas wyciagania pistoletu: ";
cin >> time_toPullgun;
cout << "Podaj ilosc naciec na rewolwerze: ";
cin >> Cuts_onGun;
}
void Gunslinger::Data()
{
cout << "Czas wyciagania pistoletu: " << time_toPullgun << endl;
cout << "Ilosc naciec na pistolecie: " << Cuts_onGun << endl;
}
void Gunslinger::Show()
{
cout << "Kategoria Gunslinger\n";
Person::Show();
Data();
}
void Card::Set()
{
cout << "Podaj kolor karty: ";
cin >> color;
cout << "Podaj numer karty: ";
cin >> number;
}
std::istream & operator>>(std::istream & is, Card & st)
{
cout << "Podaj kolor karty: ";
is >> st.color;
cout << "Podaj numer karty ";
is >> st.number;
return is;
}
void PokerPlayer::Set()
{
Person::Set();
cin >> Karta;
}
void PokerPlayer::Show()
{
cout << "Kategoria PokerPlayer\n";
Person::Show();
Data();
}
void PokerPlayer::Data()
{
cout << "Kolor karty: " << Karta.Color();
cout << "Numer karty: " << Karta.Number();
}
int PokerPlayer::Draw()
{
srand(time(NULL));
int liczba = rand() % 52;
return liczba;
}
void PokerPlayer::Ustaw()
{
Karta.Set();
}
void BadDude::Set()
{
Person::Set();
PokerPlayer::Ustaw();
Gunslinger::Ustaw();
}
void BadDude::Show()
{
cout << "Kategoria BadDude\n";
Person::Show();
PokerPlayer::Data();
Gunslinger::Data();
}
#pragma once
#ifndef Person_h
#define Person_h
#include <string>
#include <cstdlib>
#include <ctime>
class Person
{
private:
std::string Name, Surrname;
public:
Person(const char * l, const char* t) : Name(l), Surrname(t) {}
Person();
~Person();
void Set();
void Show();
};
class Gunslinger :public Person
{
private:
int Cuts_onGun;
double time_toPullgun;
public:
Gunslinger(const char * l, const char* t, int cuts, double time) : Person(l,t),
Cuts_onGun(cuts), time_toPullgun(time) {}
Gunslinger() :Person(), Cuts_onGun(0), time_toPullgun(0) {}
~Gunslinger();
double Draw() { return time_toPullgun; }//czas wyciagania spluwy
void Set();
void Show();
void Data();
void Ustaw();
};
class Card
{
private:
std::string color;
int number;
public:
Card(const char * l, int nr) : color(l), number(nr) {}
std::string & Color() { return color; }
int & Number() { return number; }
void Set();
friend std::istream & operator>>(std::istream & is, Card & st);
};
class PokerPlayer :virtual public Person
{
private:
Card Karta;
public:
PokerPlayer(const char * l, const char* t, const char * c, int nr) :Person(l, t), Karta(c, nr) {}
PokerPlayer() :Person(), Karta("",0) {}
~PokerPlayer();
int Draw();
void Set();
void Show();
void Data();
void Ustaw();
};
class BadDude : public PokerPlayer, public Gunslinger
{
private:
public:
BadDude() :Gunslinger(), PokerPlayer() {}
BadDude(const char * l, const char* t, const char * c, int nr, int cuts, double time) :
Gunslinger(l, t, cuts, time), PokerPlayer(l, t, c, nr) {}
~BadDude();
double Gdraw() { return Gunslinger::Draw(); }
int Cdraw() { return PokerPlayer::Draw(); }
void Set();
void Show();
};
#endif // ! Person_h
// r14z04.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Person.h"
#include <iostream>
int main()
{
using std::cin;
using std::cout;
using std::strchr;
using std::endl;
const int SIZE = 5;
Person *lolas[SIZE];
int ct;
for (int ct = 0;ct < SIZE;ct++)
{
char choice;
cout << "podaj kategorie osoby:\n"
<< "O: osoba, G: rewolwerowiec "
<< "P, pokerzysta, B: zły człowiek, W: aby zakonczyć " << endl;
cin >> choice;
while (strchr("kpsw", choice) == NULL)
{
cout << "wpisz k, p, s lub w: ";
cin >> choice;
}
if (choice == 'w')
break;
switch (choice)
{
case 'O': lolas[ct] = new Person;
break;
case 'G': lolas[ct] = new Gunslinger;
break;
case 'P': lolas[ct] = new PokerPlayer;
break;
case 'B': lolas[ct] = new BadDude;
break;
}
cin.get();
lolas[ct]->Set();
}
cout << "\nLista pracownikow:\n";
int i = 0;
for(i=0;i<ct;i++)
{
cout << endl;
lolas[i]->Show();
}
for (i = 0;i < ct;i++)
delete lolas[i];
cout << "Koniec.\n";
return 0;
}
Problem występuje w 3 części kodu w 40 linijce. Próbowałem zamienić
case 'B': lolas[ct] = new BadDude;
break;
Na
case 'B': lolas[ct] = new (Person *)BadDude;
break;
Albo
case 'B': lolas[ct] = new static_cast<Person>BadDude;
break;
//////
case 'B': lolas[ct] = new<Person*>BadDude;
break;
Przy static cast;scie wyświetlał się jedynie komunikat, błędu składniowego(oczekiwano specyfikatora typu), więc rozumiem, że robiłem jakiś błąd składniowy po prostu.