gpt4 book ai didi

c++ - 在 C++ 中创建和实现类

转载 作者:行者123 更新时间:2023-11-27 22:51:51 25 4
gpt4 key购买 nike

在我的计算机科学课上,我们正在学习如何使用类来创建虚拟电子元件……我们需要创建五样东西; main() 以及 ElectricalComponent.h/.cpp、Resistor.h/.cpp、Capacitor.h/.cpp 和 Battery.h/.cpp。

我已经创建了 Electrical 组件类,并且我知道如何创建和修复 main 函数中的任何错误,但是我在构建 Resistor 类时遇到了问题。

#include <iostream>
#include "ElectronicComponent.h"
class Resistor :
public ElectronicComponent
{
public:
Resistor(double);
virtual ~Resistor();
virtual double getValue() const = 0;
virtual std::string getUnits() const = 0;
virtual std::string to_string() const = 0;
};

Resistor::Resistor(double v)
{
value = v;
}

std::string Resistor::to_string() const
{
return "Resistor value (" + std::to_string(value) + " " + units + ")";
}

我收到的错误是命名空间 std 没有成员 to_string,并且我的变量值和单位未声明。我理解未声明变量的含义,但不理解 to_string 错误。

这是我得到的有关该类(class)的所有信息:

The Resistor class will be implemented with files Resistor.h and Resistor.cpp. The Resistor class will inherit, publicly, from ElectronicComponent. You need to implement a destructor and the three virtual functions getValue, getUnits and to_string. You will also need a constructor that takes one input parameter, the value of the resistor. The value is of type double. The getValue function needs to return the value as a double. The getUnits function needs to return “Ohm(s)” as a std::string.

其余的类应该以相同的方式构建,因此了解一个类的工作原理应该对我有很大帮助。谢谢。

最佳答案

在您的 header 中使用:

#include<string>

关于c++ - 在 C++ 中创建和实现类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465851/

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