Не компилится (Наследование.)

Модератор: Модераторы разделов

Аватара пользователя
kkkggg
Сообщения: 100

Не компилится

Сообщение kkkggg »

#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: Не компилится

Сообщение dey »

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
Спасибо сказали:
Аватара пользователя
Uncle_Theodore
Сообщения: 3339
ОС: Slackware 12.2, ArchLinux 64

Re: Не компилится

Сообщение Uncle_Theodore »

kkkggg писал(а):
28.04.2007 00:45
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;
}

};


Вот что пишет:

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: Не компилится

Сообщение kkkggg »

Uncle_Theodore писал(а):
28.04.2007 00:55
Так все правильно пишет. Оба конструктора определены как private, как ты собираешься создавать объекты этого класса?


ХА!! Сори парни, незаметил =))))).
Спосибо, вопрос решон!
Спасибо сказали: