【问题标题】:C++ inheritance not inheritingC++ 继承不继承
【发布时间】:2015-04-19 03:40:34
【问题描述】:

矩形类型.h

class rectangleType
{
public:
    void setDimension(double l, double w);
      //Function to set the length and width of the rectangle.
      //Postcondition: length = l; width = w;

    double getLength() const;
      //Function to return the length of the rectangle.
      //Postcondition: The value of length is returned. 

    double getWidth() const;
      //Function to return the width of the rectangle.
      //Postcondition: The value of width is returned. 

    double area() const;
      //Function to return the area of the rectangle.
      //Postcondition: The area of the rectangle is 
      //               calculated and returned.

    double perimeter() const;
      //Function to return the perimeter of the rectangle.
      //Postcondition: The perimeter of the rectangle is 
      //               calculated and returned.

    void print() const;
      //Function to output the length and width of 
      //the rectangle.

    rectangleType();
      //Default constructor
      //Postcondition: length = 0; width = 0;

    rectangleType(double l, double w);
      //Constructor with parameters
      //Postcondition: length = l; width = w;

private:
    double length;
    double width;
};

矩形类型.cpp

#include<iostream>
#include"rectangleType.h"

using namespace std;

void rectangleType:: setDimension(double l, double w){
    length = l;
    width = w;
}

double rectangleType:: getLength() const{
    return length;
}

double rectangleType:: getWidth() const{
    return width;
}

double rectangleType:: area() const{
    return (length*width);
}

double rectangleType:: perimeter() const{
    return ((length*2)+(width*2));
}

void rectangleType:: print() const{
    cout << "the width is: " << width << endl;
    cout << "the length is: " << length << endl;
}

rectangleType:: rectangleType(){
    length = 0;
    width = 0;
}

rectangleType:: rectangleType(double l, double w){
    length = l;
    width = w;
}

boxType.h

class boxType : public rectangleType
{
public:
    void setDimension(double l, double w, double h);
      //Function to set the length, width, and height 
      //of the box.
      //Postcondition: length = l; width = w; height = h;

    double getHeight() const;
      //Function to return the height of the box.
      //Postcondition: The value of height is returned. 

    double area() const;
      //Function to return the surface area of the box.
      //Postcondition: The surface area of the box is 
      //                calculated and returned.

    double volume() const;
      //Function to return the volume of the box. 
      //Postcondition: The volume of the box is 
      //               calculated and returned.

    void print() const;
      //Function to output the length, width, and height of a box.

    boxType();
      //Default constructor
      //Postcondition: length = 0; width = 0; height = 0;

    boxType(double l, double w, double h);
      //Constructor with parameters
      //Postcondition: length = l; width = w; height = h;

private:
    double height;
};

boxType.cpp

#include<iostream>
#include"boxType.h"
#include"rectangleType.h"

using namespace std;

void boxType:: setDimension(double l, double w, double h){
    length = l;
    width = w;
    height = h;
}

double boxType:: getHeight() const{
    return height;
}

double boxType:: area() const{
    return (length*width*height);
}

double boxType:: volume() const{
    return ((2*length*width)+(2*length*height)+(2*width*height))
}

void boxType:: print() const{
    cout << "the width is: " << width << endl;
    cout << "the length is: " << length << endl;
    cout << "the height is: " << height << endl;
}

boxType:: boxType() : rectangleType.cpp(){
    height = 0;
}

boxType:: boxType(double l, double w, double h) : rectangleType.cpp(double l, double w){
    height = h;
}

矩形类型测试.cpp

#include<iostream>
#include"rectangleType.h"
#include"boxType.h"

using namespace std;

int main(){

    rectangleType firstRectangle;
    rectangleType secondRectangle(2, 2);

    firstRectangle.setDimension(3, 3);
    cout << "rectangle's length is: " << firstRectangle.getLength() << endl;
    cout << "rectangle's width is: " << firstRectangle.getWidth() << endl;
    cout << "rectangle's area is: " << firstRectangle.area() << endl;
    cout << "rectangle's perimeter is: " << firstRectangle.perimeter() << endl;
    secondRectangle.print();

    boxType firstBox;
    boxType secondBox(2, 2, 2);
    firstBox.setDimension(3, 3, 3);
    cout << "box's length is: " << firstBox.getLength() << endl;
    cout << "box's width is: " << firstBox.getWidth() << endl;
    cout << "box's height is: " << firstBox.getHeight() << endl;
    cout << "box's area is: " << firstBox.area() << endl;
    cout << "box's volume is: " << firstBox.volume() << endl;
    secondBox.print();


}

我正在编译它:

g++ -o rectangleTest rectangleTypeTest.cpp rectangleType.cpp boxType.cpp

我收到错误消息:

In file included from boxType.cpp:2:0:
boxType.h:2:1: error: expected class-name before ‘{’ token
 {
 ^
boxType.cpp: In member function ‘void boxType::setDimension(double, double, double)’:
boxType.cpp:8:2: error: ‘length’ was not declared in this scope
  length = l;
  ^
boxType.cpp:9:2: error: ‘width’ was not declared in this scope
  width = w;
  ^
boxType.cpp: In member function ‘double boxType::area() const’:
boxType.cpp:18:10: error: ‘length’ was not declared in this scope
  return (length*width*height);
          ^
boxType.cpp:18:17: error: ‘width’ was not declared in this scope
  return (length*width*height);
                 ^
boxType.cpp: In member function ‘double boxType::volume() const’:
boxType.cpp:22:13: error: ‘length’ was not declared in this scope
  return ((2*length*width)+(2*length*height)+(2*width*height))
             ^
boxType.cpp:22:20: error: ‘width’ was not declared in this scope
  return ((2*length*width)+(2*length*height)+(2*width*height))
                    ^
boxType.cpp:23:1: error: expected ‘;’ before ‘}’ token
 }
 ^
boxType.cpp: In member function ‘void boxType::print() const’:
boxType.cpp:26:30: error: ‘width’ was not declared in this scope
  cout << "the width is: " << width << endl;
                              ^
boxType.cpp:27:31: error: ‘length’ was not declared in this scope
  cout << "the length is: " << length << endl;
                               ^
boxType.cpp: In constructor ‘boxType::boxType()’:
boxType.cpp:31:23: error: type ‘rectangleType’ is not a direct base of ‘boxType’
 boxType:: boxType() : rectangleType(){
                       ^
boxType.cpp: In constructor ‘boxType::boxType(double, double, double)’:
boxType.cpp:35:51: error: type ‘rectangleType’ is not a direct base of ‘boxType’
 boxType:: boxType(double l, double w, double h) : rectangleType(double l, double w){
                                                   ^
boxType.cpp:35:65: error: expected primary-expression before ‘double’
 boxType:: boxType(double l, double w, double h) : rectangleType(double l, double w){
                                                                 ^
boxType.cpp:35:75: error: expected primary-expression before ‘double’
 boxType:: boxType(double l, double w, double h) : rectangleType(double l, double w){
                                                                       ^

这个错误似乎主要是没有在这个范围内声明的错误。在我看来,它不是继承自 rectangleType。不知道我做错了什么。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 看看 www.sscce.org

标签: c++ inheritance compiler-errors


【解决方案1】:

多个错误

  1. 您希望被继承类访问的​​变量必须受到保护; 不是私人的。
  2. 您希望“多态”调用的函数必须是虚函数;除非您试图创建恰好具有相同名称的特定于类的函数;在这种情况下,如果您有一个基类指针,则将调用基类函数,而不管指向的 abject 是框还是矩形。
  3. 如果这是家庭作业,那么这是一个不好的例子。盒子不是一种矩形。 “是类型”是定义类层次结构时要遵循的一种规则。例如盒子的面积是多少?有点无厘头。

【讨论】:

  • 头文件是我的教授给出的,所以我假设它们是正确的。所以,我不能覆盖一个函数,除非它是虚拟的?
  • 你可以定义一个与基类同名的函数。没有什么能阻止你。阅读我写的内容 - 它不会以多态方式运行。有区别。有一个术语可以解释这种差异;这个词现在不只是点击我。任务是什么?从矩形派生一个类?我怀疑。
  • 他给了我两个头文件,并告诉我为它们编写类文件。我没有对头文件进行任何更改。你能解释一下你的意思不会多态吗?你的意思是我不能访问父类的变量/函数?
  • 我没有上下文;但如果是这样,即没有其他上下文,那就换教授。比改变 c++ 让它做你想做的事情更容易。
猜你喜欢
  • 2012-10-30
  • 1970-01-01
  • 2017-03-31
  • 2023-04-06
  • 2010-11-05
  • 2016-03-02
  • 1970-01-01
  • 2014-02-03
  • 2012-02-13
相关资源
最近更新 更多