gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:08 25 4
gpt4 key购买 nike

我创建了一个基本的 consol 程序,它创建了一个用户所需高度和宽度的框。我想了解类(class)是如何工作的,所以我只使用了一个文件。现在我正在尝试将类(class)正确地放入 .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";
}
};

最佳答案

您忘记了类型说明符,所以您应该在 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 而不是包含守卫。

关于c++ - 无法将类移动到单独的 .cpp 和 .h 文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49458740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com