【发布时间】: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 的新代码:
main.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++