如何解决未执行C ++类功能
这是一个非常简单的错误: 我有一个小项目,应该在其中生成一堆文件夹和文件。它用作我应该制造的实际存储引擎的伪数据。数据是关于“人类”的,这些人类以及其他属性具有置信度和高度。每次创建新文件时,都会更新选定的“人类”,以使它们的高度变大并且置信度会发生变化。该程序可以编译,但是当我检查文件时,我注意到人类永远不会更改这些属性,因此不会被更新。我不明白为什么,由于代码看起来如此之小和简单,我必须忽略一些东西,但几周来我一直在努力寻找这个小错误。
Human.cpp:
#include "human.h"
Human::Human(const std::string& _fname,const std::string& _lname,int _gender,const std::string& _position) {
m_firstname = _fname;
m_lastname = _lname;
m_height = (rand() % 271) + 0.1; // a person's height between 30 and 300 cm
m_height = m_height + 30; // writing this in one line gives a warning for arthimetic overflow
m_gender = _gender;
m_position = _position;
m_confidence = (rand() % 100) + 0.01 * (rand() % 101);
}
void Human::UpdateValues() {
m_height = m_height + 0.1* (rand()% 11); // new packages each hour: grow 1 cm max
if (m_height >= 300) { // don't grow over 3 meter
m_height = 300;
}
// T or F: random number is even or uneven
int is_even = round(m_height);
if (( is_even % 2) == 0) {
m_confidence = m_confidence + (rand() % 20) + 0.01 *(rand() % 100); // fluctuates by 20% max
if (m_confidence > 100) {
m_confidence = 100;
}
}
else {
m_confidence = m_confidence - (rand() % 20) + 0.01 * (rand() % 100); // fluctuates by 20% max
if (m_confidence < 0) {
m_confidence = 0;
}
}
}
main.cpp:
#pragma warning(push,0) //header files from third party cause errors in console,ignore these since it is not our code
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#pragma warning(pop)
#include "human.h"
namespace fs = std::filesystem;
// issues left to resolve:
// - humans don't get updated?
fs::path createSubFolder(const fs::path& _parent,const std::string &_subfolder) {
fs::path sub_path = _parent / _subfolder;
fs::create_directories(sub_path);
return sub_path;
}
std::string createString(int number) { // for the generation of file names: single digit days/months/hours need to be converted to double digit string
if (number >= 10) {
return std::to_string(number);
}
// else
return "0" + std::to_string(number);
}
int main() {
cv::Mat blank(500,700,CV_8UC3,cv::Scalar(230,230,230)); // blank image to write the imagestring on later for each package file
fs::path root = "C:\\data_system_programming"; // path of the main directory where we will generate all data
// generate some Humans for the packages
std::vector<std::string> names = { "Tom","Paul","Anna","Cindy","Mina","Skye","Justin" }; //will uses these vectors to generate names
std::vector<std::string> lastnames = { "Jansen","Johnson","Smith","Hathaway","Merckx","Granger","Versendaal" };
std::vector<std::shared_ptr<Human>> Humans; //size 6
for (int i = 0; i <= 6; i++) {
Humans.push_back(std::shared_ptr<Human>(new Human(names[i],lastnames[i],1,"Unemployed")) );
}
for (int i = 2; i <= 4; i++) {
Humans.push_back(std::shared_ptr<Human>(new Human(names[i],"Employed")));
}
for (int i = 5; i <= 6; i++) {
Humans.push_back(std::shared_ptr<Human>(new Human(names[i],2,"Student")));
}
for (int year = 2018; year <= 2020; year++) {
bool is_leapyear = 0; // need this bool to know if februari has 28 or 29 days
if (year % 4 == 0) { // a year is a leapyear when it is divisible by 4 but not by 100
is_leapyear = (year % 100 != 0);
}
if (year % 400 == 0) { // unless it is divisible by 400
is_leapyear = 1;
}
std::string string_year = std::to_string(year);
fs::path year_path = createSubFolder(root,string_year); // create folder for the year
for (int month = 1; month <= 12; month++) {
int month_length = 31;
switch (month) {
case 2:
if (is_leapyear) {
month_length = 29;
}
else {
month_length = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
month_length = 30;
break;
}
std::string string_month = createString(month);
fs::path month_path = createSubFolder(year_path,string_month); // create folder for the month in year folder
for (int day = 1; day <= month_length; day++) {
std::string string_day = createString(day);
fs::path day_path = createSubFolder(month_path,string_day); // create folder for the day in month folder
for (int hour = 0; hour <= 23; hour++) {
int amount_pckgs = rand() % 4; // gives a random number between 0 and 20 = number of packages to create
for (int counter = 1; counter <= amount_pckgs; counter++) {
// create a package with txt and png:
std::string date_string = std::to_string(year) + createString(month) + createString(day);
std::string time_string = createString(hour);
// need randomised minutes,seconds (7 digits total),miliseconds
for (int digit = 0; digit < 3; digit++) {
time_string = time_string + std::to_string(rand() % 7); // append to timestring a number from 0 to 6
time_string = time_string + std::to_string(rand() % 10); // append to timestring a number from 0 to 9
}
time_string = time_string + std::to_string(rand() % 10); // append to timestring a number from 0 to 9
std::string filenamestring = date_string + "-" + time_string + "-";
// select a human
int select_human = (rand() % 7);
Human H = *(Humans[select_human]);
filenamestring = filenamestring + H.m_firstname + "-" + H.m_lastname + "-" + std::to_string((int)round(H.m_height));
// we wil use this to generate package,.txt and .jpg filename
fs::path package_path = day_path / ("p-" + filenamestring);
fs::create_directories(package_path);
// filling in the text file:
std::ofstream text_file;
text_file.open(package_path / ("t-" + filenamestring + ".txt")); //this creates said text file
text_file << "001" << date_string << std::endl;
text_file << "002" << time_string << std::endl;
text_file << "003" << H.m_firstname << std::endl;
text_file << "004" << H.m_lastname << std::endl;
text_file << "005" << std::to_string((int)round(H.m_height)) << std::endl;
text_file << "006" << std::to_string(H.m_gender) << std::endl; // 0 women,1 men,2+ others
text_file << "007" << H.m_position << std::endl;
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << H.m_confidence; // keep only 2 decimal points
text_file << "008" << ss.str() << std::endl;
H.UpdateValues(); // update Human so they grow and change confidence
text_file.close();
// creating the image file
std::string text = date_string + "-" + time_string + "-" + H.m_firstname + "-" + H.m_lastname;
cv::Mat image = blank.clone();
cv::putText(image,text,cv::Point(50,250),cv::FONT_HERSHEY_SIMPLEX,0.5,cv::Scalar(0,0),1);
std::string path_string = (package_path / ("i-" + filenamestring + ".jpg")).u8string();
cv::imwrite(path_string,image);
}
}
}
}
}
return 0;
}
解决方法
在这一行:
Human H = *(Humans[select_human]);
您正在制作Human
中Humans
的副本。因此,当您这样做时:
H.UpdateValues(); // update Human so they grow and change confidence
您实际上并没有修改Humans
的内容,这似乎是代码的目的。
相反,您可以对要更新的Human
对象进行引用:
Human & H = *(Humans[select_human]);
// ^ reference
现在修改H
将修改Humans
的内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。