无法将类移动到单独的.cpp和.h文件中。

时间:2018-03-23 21:50:57

标签: c++ class visual-c++

我创建了一个基本的consol程序,它创建了一个用户所需高度和宽度的框。我想学习类的工作方式,所以我只使用了一个文件。现在我试图将类正确地放入.h和.cpp文件中。有人可以指出我做错了什么以及我该如何解决它?

原始代码只有一个main.cpp:

lstm

使用main.cpp,BoxClass.h,Boxclass.cpp的新代码:

main.cpp中:

$columntoextract = '30','31','39'
$endcol = $($columntoextract[2])
$endcol

BoxClass.h:

#include "stdafx.h"
#include <iostream>
using namespace std;

class BoxClass {
    //prv variables
    unsigned short int width;
    int height, i;
    float space_Value;
    float height_Count = 1;
    bool error = false;

    //prv functions
    void Print_Rectangle(int x, int y) {

        //calc
        space_Value = (3 * x) - 4;

        //draw top of box
        for (width = 1; width < x; width += 1) {
            cout << "...";
        }
        cout << "\n";

        //draw sides
        for (height = 1; height < y; height += 1) {
            cout << ":";
            height_Count++;
            for (width = 1; width < space_Value; width += 1) {
                cout << " ";
            }

            cout << ":\n";
        }

        //draw bottom
        cout << ":";

        for (width = 1; width < space_Value; width += 1) {
            cout << ".";
        }
        cout << ":\n";
    }

public:
    //function shows area of individual spaces
    float Rectangle_Area() {
        if (error == false) {
            return (height_Count - .5)*(space_Value - 1);
        }
        else {
            return 0;
        }
    }
    //function shows area of individual spaces

    // constructor
    BoxClass(int x, int y, int amount) {

        if (x <= 41) {
            for (i = 1; i <= amount; i += 1) {
                Print_Rectangle(x, y);
            }
        }
        else {
            error = true;
            cout << "Error - width must be below 42!\n";
        }
    };
};

int main() {
    //variable declaration/definition
    int width_Var;
    int height_Var;
    int number_of_Boxes;

    //object declaration/Body
    cout << "Enter width of rectangle/box\nWidth = ";
    cin >> width_Var;
    cout << "Enter height of rectangle/box\nHeight = ";
    cin >> height_Var;
    cout << "How many rectangles/boxes do you want?\n";
    cin >> number_of_Boxes;

    BoxClass box1(width_Var, height_Var, number_of_Boxes);

    cout <<"Box Area = "<<box1.Rectangle_Area() << endl;

//exit
    cout << "\n\n\n\n\n";
    system("pause");
    return 0;
}

BoxClass.cpp:

#include "BoxClass.h"
#include "stdafx.h"
#include <iostream>
using namespace std;


int main() {
    //variable declaration/definition
    int width_Var;
    int height_Var;
    int number_of_Boxes;

    //object declaration/Body
    cout << "Enter width of rectangle/box\nWidth = ";
    cin >> width_Var;
    cout << "Enter height of rectangle/box\nHeight = ";
    cin >> height_Var;
    cout << "How many rectangles/boxes do you want?\n";
    cin >> number_of_Boxes;

    BoxClass box1(width_Var, height_Var, number_of_Boxes);

    cout <<"Box Area = "<<box1.Rectangle_Area() << endl;

//exit
    cout << "\n\n\n\n\n";
    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

您忘记了类型说明符,因此您应该在BoxClass.cpp中更改它:

BoxClass::Print_Rectangle(int x, int y)
...
BoxClass::Rectangle_Area()

到此:

void BoxClass::Print_Rectangle(int x, int y)
...
float BoxClass::Rectangle_Area()

也有点偏离主题,我可以建议你将来的事情是使用#pragma once而不是包括警卫。

答案 1 :(得分:0)

您需要一个构造函数,您可以在其中设置height_Count和error的值。你应该也可以定义一个析构函数,即使在这一点上可能没有什么可做的。这只是一个好主意。根据Scott Meyers的Effective C ++,您还应该定义一个复制构造函数和一个复制赋值运算符。

这是已发布的类型说明符建议的补充。