未执行C ++类功能

如何解决未执行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]);

您正在制作HumanHumans副本。因此,当您这样做时:

H.UpdateValues(); // update Human so they grow and change confidence

您实际上并没有修改Humans的内容,这似乎是代码的目的。

相反,您可以对要更新的Human对象进行引用

Human & H = *(Humans[select_human]);
   // ^ reference

现在修改H将修改Humans的内容。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-