You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将C++类拆分至独立.cpp与.h文件遇问题,求技术解答

Alright, let's walk through splitting your BoxClass into proper .h and .cpp files—this is a key step for organizing C++ code, so let's fix the common pitfalls you might be hitting.

First, let's recap the core rules for splitting classes:

  • Header files (.h) hold the class declaration: member variable definitions, function prototypes, and access specifiers.
  • Implementation files (.cpp) hold the actual code for member functions, prefixed with ClassName:: to tie them to the class.
  • Always use header guards to prevent duplicate inclusion errors.
  • Avoid using namespace std; in headers—it pollutes the global namespace for every file that includes your header.

Here's the fixed split code:

1. BoxClass.h (Header File)

#ifndef BOXCLASS_H
#define BOXCLASS_H

// Only include necessary headers for the class declaration
#include <iostream>

class BoxClass {
private:
    // Explicitly mark private members (C++ defaults to private, but this is clearer)
    unsigned short int width;
    int height, i;
    float space_Value;
    float height_Count = 1;
    bool error = false;

    // Declare private member functions here (no implementation)
    void Print_Rectangle(int x, int y);

public:
    // Public function prototypes
    float Rectangle_Area();

    // Constructor declaration
    BoxClass(int x, int y, int amount);
};

#endif // BOXCLASS_H

2. BoxClass.cpp (Implementation File)

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

// It's safe to use namespace std here (only affects this .cpp file)
using namespace std;

// Implement private member functions with the BoxClass:: qualifier
void BoxClass::Print_Rectangle(int x, int y) {
    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";
}

// Implement public member function
float BoxClass::Rectangle_Area() {
    if (!error) {
        return (height_Count - 0.5) * (space_Value - 1);
    } else {
        return 0;
    }
}

// Implement 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";
    }
}

3. main.cpp (Your Main Program)

#include "stdafx.h" // Remove this if you're not using Visual Studio
#include "BoxClass.h"
#include <iostream>

using namespace std;

int main() {
    int width_Var;
    int height_Var;
    int number_of_Boxes;

    // Add input logic (since your original code cut off here)
    cout << "Enter box width (max 41): ";
    cin >> width_Var;
    cout << "Enter box height: ";
    cin >> height_Var;
    cout << "Enter number of boxes: ";
    cin >> number_of_Boxes;

    // Create your BoxClass object
    BoxClass myBox(width_Var, height_Var, number_of_Boxes);
    cout << "Area of inner spaces: " << myBox.Rectangle_Area() << endl;

    return 0;
}

Common Mistakes You Might Have Made (And Fixes)

  • Forgot the BoxClass:: qualifier: Without this, the compiler thinks your functions are global, leading to "undefined reference" errors. Always prefix class member functions with the class name in the .cpp file.
  • No header guards: Without #ifndef BOXCLASS_H etc., including the header multiple times will cause "redefinition of class BoxClass" errors.
  • Private functions not declared in the header: If you tried to implement Print_Rectangle in the .cpp without declaring it in the .h, the constructor would fail to find the function.
  • Using using namespace std; in the header: This forces every file that includes BoxClass.h to use the std namespace, which can cause naming conflicts. Keep it limited to .cpp files.
  • Missing necessary headers in the .h: The class uses cout, so we need to include <iostream> in the header so the compiler recognizes the stream types.

内容的提问来源于stack exchange,提问作者Zack Oliver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:03:31