Не компилируется программа, выдает две ошибки:
1) Error 1 error LNK2019: unresolved external symbol "public: __thiscall Rectangle::Rectangle(int,int)" (??0Rectangle@@QAE@HH@Z) referenced in function _main WR01.obj
2) Error 2 fatal error LNK1120: 1 unresolved externals D:\Microsoft Visual Studio 9.0\Projects\Итоги первой недели\Debug\Итоги первой недели.exe
Программа написана в учебнике, и перепечатана вроде бы верно, проверял много раз. В поиске по коду ошибки толком ничего не нашел, понял лишь, что (возможно) мне нужно подключить какие-то библиотеки. Использую Microsoft Visual Studio 2008. Подскажите, что делать, пожалуйста.
#include <iostream>
using namespace std;
enum CHOICE {
DrawRect = 1,
GetArea,
GetPerim,
ChangeDimensions,
Quit };
class Rectangle
{
public:
Rectangle(int width, int height);
~Rectangle();
int GetHeight() const { return itsHeight; }
int GetWidth() const { return itsWidth; }
int GetArea() const {return itsHeight * itsWidth; }
int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
void SetSize(int newWidth, int newHeight);
private:
int itsWidth;
int itsHeight;
};
void Rectangle::SetSize(int newWidth, int newHeight)
{
itsWidth = newWidth;
itsHeight = newHeight;
}
Rectangle::~Rectangle()
{
}
int DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
int main()
{
Rectangle theRect(30,5);
int choice = DrawRect;
int fQuit = false;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > fQuit)
{
cout << "\nInvalid Choice, please try again. ";
cout << endl << endl;
continue;
}
switch (choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
int newLength, newWidth;
cout << "\nNew width: ";
cin >> newWidth;
cout << "\nNew height: ";
cin >> newLength;
theRect.SetSize(newWidth, newLength);
DoDrawRect(theRect);
break;
case Quit:
fQuit = true;
cout << "\nExiting..." << endl << endl;
break;
default:
cout << "Error in choice!" << endl;
fQuit = true;
break;
}
}
return 0;
}
int DoMenu()
{
int choice;
cout << endl << endl;
cout << " *** Menu ***" << endl;
cout << "(1) Draw Rectangle" << endl;
cout << "(2) Area" << endl;
cout << "(3) Perimeter" << endl;
cout << "(4) Resize" << endl;
cout << "(5) Quit" << endl;
cin >> choice;
return choice;
}
void DoDrawRect(Rectangle theRect)
{
int height = theRect.GetHeight();
int width = theRect.GetWidth();
for (int i=0; i<height; i++)
{
for (int j=0; j<width; j++)
cout << "*";
cout << endl;
}
}
void DoGetArea(Rectangle theRect)
{
cout << "Area: " << theRect.GetArea() << endl;
}
void DoGetPerim(Rectangle theRect)
{
cout << "Perimeter: " << theRect.GetPerim() << endl;
}
Здравствуйте, PolyakaMorph, Вы писали:
PM>Не компилируется программа, выдает две ошибки:
PM>1) Error 1 error LNK2019: unresolved external symbol "public: __thiscall Rectangle::Rectangle(int,int)" (??0Rectangle@@QAE@HH@Z) referenced in function _main WR01.obj
PM>2) Error 2 fatal error LNK1120: 1 unresolved externals D:\Microsoft Visual Studio 9.0\Projects\Итоги первой недели\Debug\Итоги первой недели.exe
PM>Программа написана в учебнике, и перепечатана вроде бы верно, проверял много раз. В поиске по коду ошибки толком ничего не нашел, понял лишь, что (возможно) мне нужно подключить какие-то библиотеки. Использую Microsoft Visual Studio 2008. Подскажите, что делать, пожалуйста.
PM>PM>#include <iostream>
PM>using namespace std;
PM>enum CHOICE {
PM> DrawRect = 1,
PM> GetArea,
PM> GetPerim,
PM> ChangeDimensions,
PM> Quit };
PM>class Rectangle
PM> {
PM> public:
PM> Rectangle(int width, int height);
PM> ~Rectangle();
PM> int GetHeight() const { return itsHeight; }
PM> int GetWidth() const { return itsWidth; }
PM> int GetArea() const {return itsHeight * itsWidth; }
PM> int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
PM> void SetSize(int newWidth, int newHeight);
PM> private:
PM> int itsWidth;
PM> int itsHeight;
PM> };
PM>void Rectangle::SetSize(int newWidth, int newHeight)
PM>{
PM> itsWidth = newWidth;
PM> itsHeight = newHeight;
PM>}
PM>Rectangle::~Rectangle()
PM>{
PM>}
PM>
у вас не реализован конструктор для класса Rectangle(int width, int height) на что линкер и ругается, потому что определение есть, а реализации нет. либо в учебнике это забыли либо вы пропустили. обе ошибки из-за этого. допишите конструктор и все будет ок
Дело было не в бобине... Действительно, пропустил описание конструктора, в книге это есть. Все работает, спасибо за помощь. =)
Rectangle::Rectangle (int width, int height)
{
itsWidth = width;
itsHeight = height;
}
Здравствуйте, Trinity_sch, Вы писали:
T_>Здравствуйте, PolyakaMorph, Вы писали:
PM>>Не компилируется программа, выдает две ошибки:
PM>>1) Error 1 error LNK2019: unresolved external symbol "public: __thiscall Rectangle::Rectangle(int,int)" (??0Rectangle@@QAE@HH@Z) referenced in function _main WR01.obj
PM>>2) Error 2 fatal error LNK1120: 1 unresolved externals D:\Microsoft Visual Studio 9.0\Projects\Итоги первой недели\Debug\Итоги первой недели.exe
PM>>Программа написана в учебнике, и перепечатана вроде бы верно, проверял много раз. В поиске по коду ошибки толком ничего не нашел, понял лишь, что (возможно) мне нужно подключить какие-то библиотеки. Использую Microsoft Visual Studio 2008. Подскажите, что делать, пожалуйста.
PM>>PM>>#include <iostream>
PM>>using namespace std;
PM>>enum CHOICE {
PM>> DrawRect = 1,
PM>> GetArea,
PM>> GetPerim,
PM>> ChangeDimensions,
PM>> Quit };
PM>>class Rectangle
PM>> {
PM>> public:
PM>> Rectangle(int width, int height);
PM>> ~Rectangle();
PM>> int GetHeight() const { return itsHeight; }
PM>> int GetWidth() const { return itsWidth; }
PM>> int GetArea() const {return itsHeight * itsWidth; }
PM>> int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
PM>> void SetSize(int newWidth, int newHeight);
PM>> private:
PM>> int itsWidth;
PM>> int itsHeight;
PM>> };
PM>>void Rectangle::SetSize(int newWidth, int newHeight)
PM>>{
PM>> itsWidth = newWidth;
PM>> itsHeight = newHeight;
PM>>}
PM>>Rectangle::~Rectangle()
PM>>{
PM>>}
PM>>
T_>у вас не реализован конструктор для класса Rectangle(int width, int height) на что линкер и ругается, потому что определение есть, а реализации нет. либо в учебнике это забыли либо вы пропустили. обе ошибки из-за этого. допишите конструктор и все будет ок
Дело было не в бобине... Действительно, пропустил описание конструктора. Спасибо за помощь =)
Здравствуйте, PolyakaMorph, Вы писали:
PM>Дело было не в бобине... Действительно, пропустил описание конструктора, в книге это есть. Все работает, спасибо за помощь. =)
PM>PM>Rectangle::Rectangle (int width, int height)
PM>{
PM> itsWidth = width;
PM> itsHeight = height;
PM>}
PM>
Тогда уж лучше так:
Rectangle::Rectangle (int width, int height)
: itsWidth( width )
, itsHeight( height )
{}