#include <iostream>
using namespace std;
class Port
{
private:
char *brand;
char *style;
int bottles;
public:
Port(const char *br = "none", const char *st = "none", int b = 0);
Port(const Port &p);
virtual ~Port()
{
delete [] brand;
delete [] style;
}
Port & operator=(const Port & p);
Port & operator+=(int b);
Port & operator-=(int b);
int BottleCount() const { return bottles; }
void Show() const;
friend ostream & operator<<(ostream &os, const Port & p);
};
Port::Port(const char *br, const char *st, int b)
{
brand = new char(strlen(br));
strcpy(brand,br);
style = new char(strlen(st));
strcpy(style,br);
bottles = b;
}
void Port::Show() const
{
cout << "Brand: " << brand << endl << "Kind: " << style << endl << "Bottles: " << bottles << endl;
}
ostream & operator<<(ostream &os, const Port &p)
{
os << p.brand << ", " << p.style << ", " << p.bottles;
}
class VintagePort : public Port
{
public:
char *nickname;
int year;
private:
VintagePort();
VintagePort(const char *br, int b, const char *nn, int y);
~VintagePort()
{
delete [] nickname;
}
VintagePort & operator=(const VintagePort &vp);
void Show() const;
friend ostream & operator<<(ostream & os, const VintagePort &vp);
};
void VintagePort::Show() const
{
Port::Show();
cout << "nickname " << nickname << "year " << year << endl;
}
VintagePort::VintagePort(const char *br, int b, const char *nn, int y):Port(br,"none",b)
{
nickname = new char(strlen(nn));
strcpy(nickname,nn);
year = y;
}
ostream & operator<<(ostream & os, const VintagePort &vp)
{
os << vp.nickname << ", " << vp.year << endl;
}
int main ()
{
VintagePort a = VintagePort("sdfdf",66,"dfgdsd",7667);
return 0;
}
Вот что пишет:
diman@lin ~/cpp $ c++ port.cpp
port.cpp: In function `int main()':
port.cpp:58: error: `virtual VintagePort::~VintagePort()' is private
port.cpp:88: error: within this context
port.cpp:73: error: `VintagePort::VintagePort(const char*, int, const char*, int)' is private
port.cpp:88: error: within this context
port.cpp:58: error: `virtual VintagePort::~VintagePort()' is private
port.cpp:88: error: within this context
Где трабал? Всю голову сломал.
Не компилится (Наследование.)
Модератор: Модераторы разделов
-
dey
- Сообщения: 335
- ОС: OpenSuse 11.1
Re: Не компилится
kkkggg писал(а): ↑28.04.2007 00:45Вот что пишет:
diman@lin ~/cpp $ c++ port.cpp
port.cpp: In function `int main()':
port.cpp:58: error: `virtual VintagePort::~VintagePort()' is private
port.cpp:88: error: within this context
port.cpp:73: error: `VintagePort::VintagePort(const char*, int, const char*, int)' is private
port.cpp:88: error: within this context
port.cpp:58: error: `virtual VintagePort::~VintagePort()' is private
port.cpp:88: error: within this context
Где трабал? Всю голову сломал.
А зачем конструктор и деструктор класса объявлять в закрытой части ?
В сознательных действиях должен присутствовать существенный неалгоритмический компонент.
Roger Penrose,The Emperor's New Mind
Roger Penrose,The Emperor's New Mind
-
Uncle_Theodore
- Сообщения: 3339
- ОС: Slackware 12.2, ArchLinux 64
Re: Не компилится
kkkggg писал(а): ↑28.04.2007 00:45class VintagePort : public Port
{
public:
char *nickname;
int year;
private:
VintagePort();
VintagePort(const char *br, int b, const char *nn, int y);
~VintagePort()
{
delete [] nickname;
}
};
Вот что пишет:
diman@lin ~/cpp $ c++ port.cpp
port.cpp: In function `int main()':
port.cpp:58: error: `virtual VintagePort::~VintagePort()' is private
port.cpp:88: error: within this context
port.cpp:73: error: `VintagePort::VintagePort(const char*, int, const char*, int)' is private
port.cpp:88: error: within this context
port.cpp:58: error: `virtual VintagePort::~VintagePort()' is private
port.cpp:88: error: within this context
Где трабал? Всю голову сломал.
Так все правильно пишет. Оба конструктора определены как private, как ты собираешься создавать объекты этого класса?
-
kkkggg
- Сообщения: 100
Re: Не компилится
Uncle_Theodore писал(а): ↑28.04.2007 00:55Так все правильно пишет. Оба конструктора определены как private, как ты собираешься создавать объекты этого класса?
ХА!! Сори парни, незаметил =))))).
Спосибо, вопрос решон!