【发布时间】:2022-01-10 03:34:44
【问题描述】:
如何制作一个对象并将其放入数组中?我想用参数化的构造函数创建一个DailyStats 的数组。我想使用循环添加到对象数组中。
#ifndef DAILYSTATS_H
#define DAILYSTATS_H
#include <iostream>
#include <vector>
using namespace std;
class DailyStats
{
public:
DailyStats();
DailyStats(string content);
void parse(const string& line);
double mean();
string getDate();
void setDate(string newDate);
private:
string date;
double temperatureValues[];
};
#endif
#include <iostream>
#include "DailyStats.hpp"
#include <vector>
#include <cmath>
using namespace std;
DailyStats::DailyStats()
{
date = "";
count = 0;
temperatureValues[0];
}
DailyStats::DailyStats(string content)
{
parse(content);
temperatureValues[24];
count = 0;
}
void DailyStats::parse(const string& line)
{
string random = "";
string random1 = "";
char del = ' ';
int count2 = 0;
for(int i = 0; i <= (int)line.size(); i++)
{
if(count < 10)
{
random1 += line[i];
setDate(random1);
count++;
}
if(line[i] != del && count == 10)
{
random += line[i];
}
else if (line[i] == del && count == 10)
{
temperatureValues[count2] = stod(random);
random = "";
count2++;
}
}
}
double DailyStats::mean()
{
count = 1;
double num = 0;
while(count <= 24)
{
num += temperatureValues[count];
if(count == 24)
{
num = num/24;
}
count++;
}
return ceil(num * 100.0) / 100.0;
}
string DailyStats::getDate()
{
return date;
}
void DailyStats::setDate(string newDate)
{
date = newDate;
}
#include <iostream>
#include <string>
#include "DailyStats.hpp"
using namespace std;
int main()
{
string str0 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str1 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str2 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
string str3 = "06/01/2021 74.85 71.58 78.68 71.55 78.14 72.36 76.89 71.35 79.94 78.87 78.07 75.78 77.86 74.04 76.56 72.96 75.07 74.02 70.21 75.56 79.61 72.97 75.29 73.33 ";
DailyStats info[4];
info[1] = { {str0} };
cout << info[1].mean() << endl;
return 0;
}
正如您在我的代码中看到的那样,我有不同的字符串,我想将它们放入我的对象中,以用作我的参数化构造函数的变量。所以,当我完成这些后,我可以调用我的mean() 函数,以便它可以获取不同数据的不同方式。
我在没有数组的情况下尝试过,一切正常,但我不知道如何让数组工作。
【问题讨论】:
-
这段代码中没有数组。请显示您尝试过的不适合您的代码。但是,不可能声明具有不同构造函数值的对象的 fixed 数组,因此您必须为该数组分配一块原始内存,然后使用
placement-new构造其中的每个对象根据需要记忆。否则,只需使用std::vector,然后您可以将构造的对象推入其中,并传入您想要的任何构造函数值。 -
@RemyLebeau 我已经更新了我的代码以显示我尝试过的内容
-
@RemyLebeau --
struct S { S(const std::string&) {} }; S s[] = { str0, str1, str2, str3 };用定义的值构造一个数组。 -
请阅读stackoverflow.com/help/minimal-reproducible-example。除非您有理由期望
DailyStats类的内容与导致问题相关,否则您可以向我们展示该类的虚拟版本的示例。如果您“在没有数组的情况下尝试了它并且一切正常”,那么您应该向我们展示一个没有数组的代表性代码版本,然后分别展示使用数组的尝试并解释到底出了什么问题我>。 “我不知道如何让它工作”没有帮助; 什么不工作? -
@NixJoe 关于错误,
double temperatureValues[];不是类成员数组的有效声明。这样的数组不能解绑。如果您在编译时不知道数组大小而只在运行时不知道数组大小,请改用std::vector。