|
From: | Gerald Lafreniere |
Subject: | Why doesn't code from TYC++in21days work? |
Date: | Thu, 29 Jul 2004 00:00:02 GMT |
http://www.libertyassociates.com/pages/files/TYCPP4eSource.zip
In the archive above this file can be found at
TYCPP4eSource\TYCPP 4e Source\WIR\4eWir1.cpp
I use the latest version of the gcc with Bloodshed's Dev-Cpp:
gcc (GCC) 3.2 (mingw special 20020817-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying
conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.
I try to compile it just like it is, so coming from a text book on the topic, it should be right, right?
I get 32 errors in the compiler log.
ERROR LOG(source is further down the page)
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\c\4eWir1.cpp" -o "C:\c\4eWir1.exe" -I"C:\Dev-Cpp\include\c++"
-I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward"
-I"C:\Dev-Cpp\lib\gcc-lib\mingw32\3.2\include" -I"C:\Dev-Cpp\include"
-L"C:\Dev-Cpp\lib"
C:/c/4eWir1.cpp:46: variable or field `DoDrawRect' declared void
C:/c/4eWir1.cpp:46: invalid conversion from `BOOL (*)(HDC__*, int, int,
int,
int)' to `int'
C:/c/4eWir1.cpp:47: variable or field `DoGetArea' declared void
C:/c/4eWir1.cpp:47: invalid conversion from `BOOL (*)(HDC__*, int, int,
int,
int)' to `int'
C:/c/4eWir1.cpp:48: variable or field `DoGetPerim' declared void
C:/c/4eWir1.cpp:48: invalid conversion from `BOOL (*)(HDC__*, int, int,
int,
int)' to `int'
C:/c/4eWir1.cpp: In function `int main()':
C:/c/4eWir1.cpp:53: `Rectangle' undeclared (first use this function)
C:/c/4eWir1.cpp:53: (Each undeclared identifier is reported only once
for each
function it appears in.)
C:/c/4eWir1.cpp:53: parse error before `(' token
C:/c/4eWir1.cpp:69: `theRect' undeclared (first use this function)
C:/c/4eWir1.cpp:69: `DoDrawRect' cannot be used as a function
C:/c/4eWir1.cpp:72: `DoGetArea' cannot be used as a function
C:/c/4eWir1.cpp:75: `DoGetPerim' cannot be used as a function
C:/c/4eWir1.cpp:84: `DoDrawRect' cannot be used as a function
C:/c/4eWir1.cpp: At global scope:
C:/c/4eWir1.cpp:113: parse error before `)' token
C:/c/4eWir1.cpp: In function `void DoDrawRect(...)':
C:/c/4eWir1.cpp:114: `void DoDrawRect(...)' redeclared as different
kind of
symbol
C:/c/4eWir1.cpp:46: previous declaration of `int DoDrawRect'
C:/c/4eWir1.cpp:46: previous non-function declaration `int DoDrawRect'
C:/c/4eWir1.cpp:114: conflicts with function declaration `void DoDrawRect(...)'
C:/c/4eWir1.cpp: At global scope:
C:/c/4eWir1.cpp:127: parse error before `)' token
C:/c/4eWir1.cpp: In function `void DoGetArea(...)':
C:/c/4eWir1.cpp:128: `void DoGetArea(...)' redeclared as different
kind of
symbol
C:/c/4eWir1.cpp:47: previous declaration of `int DoGetArea'
C:/c/4eWir1.cpp:47: previous non-function declaration `int DoGetArea'
C:/c/4eWir1.cpp:128: conflicts with function declaration `void DoGetArea(...)'
C:/c/4eWir1.cpp: At global scope:
C:/c/4eWir1.cpp:132: parse error before `)' token
C:/c/4eWir1.cpp: In function `void DoGetPerim(...)':
C:/c/4eWir1.cpp:133: `void DoGetPerim(...)' redeclared as different
kind of
symbol
C:/c/4eWir1.cpp:48: previous declaration of `int DoGetPerim'
C:/c/4eWir1.cpp:48: previous non-function declaration `int DoGetPerim'
C:/c/4eWir1.cpp:133: conflicts with function declaration `void DoGetPerim(...)'
Execution terminated
START OF PROGRAM
#include <iostream>
using namespace std;
enum CHOICE { DrawRect = 1, GetArea, GetPerim,
ChangeDimensions, Quit};
// Rectangle class declaration
class Rectangle
{
public:
// constructors
Rectangle(int width, int height);
~Rectangle();
// accessors
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);
// Misc. methods
private:
int itsWidth;
int itsHeight;
};
// Class method implementations
void Rectangle::SetSize(int newWidth, int newHeight)
{
itsWidth = newWidth;
itsHeight = newHeight;
}
Rectangle::Rectangle(int width, int height)
{
itsWidth = width;
itsHeight = height;
}
Rectangle::~Rectangle() {}
int DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
int main ()
{
// initialize a rectangle to 30,5
Rectangle theRect(30,5);
int choice = DrawRect;
int fQuit = false;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice >
Quit)
{
cout << "\nInvalid Choice,
please try again.\n\n";
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 << "New height: ";
cin >> newLength;
theRect.SetSize(newWidth, newLength);
DoDrawRect(theRect);
break;
case Quit:
fQuit = true;
cout << "\nExiting...\n\n";
break;
default:
cout << "Error in choice!\n";
fQuit = true;
break;
} // end switch
} // end while
return 0;
} // end main
int DoMenu()
{
int choice;
cout << "\n\n *** Menu *** \n";
cout << "(1) Draw Rectangle\n";
cout << "(2) Area\n";
cout << "(3) Perimeter\n";
cout << "(4) Resize\n";
cout << "(5) Quit\n";
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 << "\n";
}
}
void DoGetArea(Rectangle theRect)
{
cout << "Area: " << theRect.GetArea()
<< endl;
}
void DoGetPerim(Rectangle theRect)
{
cout << "Perimeter: " << theRect.GetPerim()
<< endl;
}
Thanks for any help on this.
Gerald Lafreniere
[Prev in Thread] | Current Thread | [Next in Thread] |