1.class Carton : public Box
提示一大堆几十个错误,看来是类出了问题,这其实是全角符号和半角符号的区别的问题,将:改为:即可。
其他还有“与",{}与{}等 。
2.myBox.showVolume;
提示: statement cannot resolve address of overloaded function
这是调用成员函数时忘记加括号,应为myBox.showVolume();
3.#include <iosteam>
using namespace std;
cout<< "This is a example." << endl;
提示 iosteam: No such file or directory.
`cout' undeclared (first use this function) ,
一个经典的错误,将iostream写错。
4.new types may not be defined in a return type
two or more data types in declaration of `Draw'
prototype for `Rectangle Rectangle::Draw()' does not match any in class `Rectangle'
`Rectangle Rectangle::Draw()' and `virtual void Rectangle::Draw()' cannot be overloaded
出现四五条错误提示如上,这也是非常常见的错误,原因在于声明了Rectangle类后的}后没有加“;”,然后继续实现 Rectangle::Draw(),
[cpp] view plaincopy
01.class Rectangle : public Shape
02.{
03. public:
04. Rectangle(int len, int width):
05. itsLength(len), itsWidth(width){};
06. virtual ~Rectangle(){}
07. virtual long GetArea(){ return itsLength * itsWidth;}
08. virtual long GetPerim(){ return 2*itsLength + 2*itsWidth;}
09. virtual int GetLength(){ return itsLength;}
10. virtual int GetWidth(){ return itsWidth;}
11. virtual void Draw();
12. private:
13. int itsLength;
14. int itsWidth;
15.} //此处少一分号“;”
16.void Rectangle::Draw()
17.{
18. for(int i=0; i<itsLength; i++)
19. {
20. for(int j=0; j<itsWidth; j++)
21. {
22. cout << "*";
23. }
24. cout <<"/n";
25. }
26. cout << "ok" ;
27.}
5.multiple types in one declaration
此编译错误与上面的第4条类似,也是忘记类声明后忘记加分号,但是在声明的两个类之间 ,中间没有成员函数的实现。
6.cannot resolve overloaded function `precision' based on conversion to type `std::streamsize '
原语句是 streamsize prec = cout.precision;
这也是非常容易犯的错误,是调用成员函数时没有加(),上面的语句应为
streamsize prec = cout.precision();