【问题标题】:How would I create a class with a variable name in the class name?如何在类名中创建一个具有变量名的类?
【发布时间】:2015-05-07 07:18:35
【问题描述】:

所以,这是我的代码:

#include <iostream>
#include <conio.h>

using namespace std;

class rectangle {
public:
    double width;
    double height;
    rectangle(double, double);
    double area() { return (width*height);}
};

rectangle::rectangle(double a, double b) {
    width = a;
    height = b;
}

int main() {
    cout << "How many rectangles would you like to create? ";
    int rectNum;
    cin >> rectNum;

    for (int counter = 0; counter < rectNum; counter++) {
        int rectCount = 1;
        rectCount = counter + 1;
        double rectWidth, rectHeight;
        cout << "\nEnter width of rectangle " << rectCount << ": ";
        cin >> rectWidth;
        cout << "\nEnter height of rectangle " << rectCount << ": ";
        cin >> rectHeight;
        rectangle rect/*integer value of rectCount at current time*/(rectWidth, rectHeight);
    }

    return 0;
}

正如评论部分所说,我想创建一个名为 rect 的矩形,其后缀是整数 rectCount 的当前值。我该怎么做?提前致谢!

【问题讨论】:

  • 数组、向量等怎么样?
  • 可能有更好的方法来完成您想要实现的任何目标。你原来的问题是什么?你需要这个做什么?
  • @deviantfan 当我尝试使用数组时,它给出了错误“表达式必须具有常量值”。尝试使用 std::vector 根本不起作用。没有错误信息或任何东西。只是一条红色的波浪线。
  • @tux3 我没有这样做的理由,只是学习 C++(特别是类)。
  • 结构体内部应该有输入输出函数。

标签: c++ class variables int


【解决方案1】:

在 C++ 中无法动态设置类名。但是,可以使用静态变量来存储矩形的运行计数:

#include <iostream>
#include <conio.h>
using namespace std;

class rectangle 
{
    public:
        double width;
        double height;
        static int rectCount; //declare static variable
        rectangle(double, double);
        double area() { return (width*height);}
};

int rectangle::rectCount=0; //initialize static variable

rectangle::rectangle(double a, double b) 
{
    width = a;
    height = b;
    rectCount++;
}

int main() 
{
    cout << "How many rectangles would you like to create? ";
    int rectNum;
    cin >> rectNum;

    for (int counter = 0; counter < rectNum; counter++) 
    {
        int rectCount = 1;
        rectCount = counter + 1;
        double rectWidth, rectHeight;
        cout << "\nEnter width of rectangle " << rectCount << ": ";
        cin >> rectWidth;
        cout << "\nEnter height of rectangle " << rectCount << ": ";
        cin >> rectHeight;
        rectangle rect=rectangle(rectWidth, rectHeight);
        cout<<"rectCount value: "<<rectCount;
      }

return 0;
}

【讨论】:

    【解决方案2】:

    这个习语在 C++ 中从来没有用处。正确的方法是将矩形存储在像std::vector 这样的容器中,该容器会自动增长和收缩以满足您的需求。然后,您可以循环遍历向量,而不必担心其中实际有多少元素。

    std::vector<rectangle> rects(rectNum);
    
    for (int counter = 0; counter < rectNum; counter++) {
        /* .. */
        rects.emplace_back(rectWidth, rectHeight);
    }
    

    实际上不需要循环。 std::vector 的构造函数会为你处理好。

    std::vector<rectangle> rects(rectNum, {rectWidth, rectHeight});
    

    您需要包含&lt;vector&gt; 才能使用std::vector

    【讨论】:

    • 在尝试使用 std::vector 时,我收到错误消息“命名空间“std”没有成员“vector”。我该如何解决?编辑:哦,好的。我假设做“使用命名空间标准;”将包括它。哎呀。
    • 谢谢,您已经修复了我的代码的第一部分!现在,我将如何让它按顺序打印每个矩形的值?我假设代码是codecout code,但这不起作用。有什么想法吗?
    【解决方案3】:

    变量名必须在编译时定义,所以你试图做的事情不会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-30
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      相关资源
      最近更新 更多