【问题标题】:Having trouble moving class to seperate .cpp and .h file.移动类以分离 .cpp 和 .h 文件时遇到问题。
【发布时间】:2018-03-23 21:50:57
【问题描述】:

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

只有 main.cpp 的原始代码:

#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;
}

带有 main.cpp、BoxClass.h、Boxclass.cpp 的新代码:

ma​​in.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;
}

BoxClass.h:

#ifndef BOXCLASS_H
#define BOXCLASS_H

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);

public:
    //function shows area of individual spaces
    float Rectangle_Area();
    //function shows area of individual spaces

    // constructor
    BoxClass(int x, int y, int amount);
};

#endif

BoxClass.cpp:

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


    BoxClass::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";
    }


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

    // constructor
    BoxClass::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";
        }
    };

【问题讨论】:

  • 代码有什么症状让你觉得有问题?
  • 您是否在使用单独的 .cpp 和 .h 文件时遇到任何编译器错误?您能否更具体地说明错误是什么?
  • 摆脱#include “stdafx.h”。或者把它放在第一位。
  • @PeteBecker 非常感谢,您解决了问题!

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


【解决方案1】:

您忘记了类型说明符,因此您应该在 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 代替包括警卫。

【讨论】:

  • Kalinowski 我对所有变量都有很多错误。这里只有一个。 1>c:\users\cade\source\repos\consoleapplication1\consoleapplication1\boxclass.cpp(22): error C2065: 'width': undeclared identifier
  • @Zack Oliver 这是由于缺乏信息,没有谈论不正确的链接,如果你的“stdafx.h”甚至存在,你也不提供信息,所以我认为它存在某处。难怪删除它有效。如果您解决了问题,请选择对您有帮助的最佳答案作为解决方案。
【解决方案2】:

您需要一个构造函数,您可以在其中设置 height_Count 和 error 的值。您可能还应该定义一个析构函数,即使此时可能无事可做。这只是一个好主意。根据《Effective C++》中的 Scott Meyers,您还应该定义一个复制构造函数和一个复制赋值运算符。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多