c++ - 在 .cpp 文件之间传递全局字符串变量?

标签 c++ string function

我创建了一个 globals.h 头文件,其中包含声明为“new_name”的外部字符串。在 image_output.cpp (main) 中,我在函数顶部将 string(new_name) 声明为全局变量。然后我将该变量传递给 erode_image.cpp(函数文件)并更新它。我已经将 globals.h 包含在主文件和函数文件中。问题是“new_name”变量没有在函数文件(erode_image.cpp)中更新。有谁知道我在哪里做错了吗?

全局变量.h:

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

extern string new_name;

erode_image.cpp(函数):

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "functions.h"
#include "globals.h"

using namespace cv;
using namespace std;


Mat erode_image(Mat image, int erosion_size, string new_name) {

    new_name += "_eroded";
    Mat element = getStructuringElement(MORPH_RECT, Size(erosion_size, erosion_size));
    erode(image, image, element);

    return image;

}

image_output.cpp(主要):

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "functions.h"
#include "globals.h"

using namespace cv;
using namespace std;

Mat imgCrop;
string new_name;

int erosion_size = 25;
int dilate_size = 50;
int brightness = 50;
int threshold_to_zero = 3;
int sectionCount = 5;

int main()
{

    String folderpath = "*.png";
    vector<String> filenames;
    cv::glob(folderpath, filenames);

    //removes ./ from beginning of filepath
    for (int i = 0; i < size(filenames); i++) {
        filenames[i] = filenames[i].erase(0, 2);
    }

    for (int i = 0; i < size(filenames); i++) {
        if (filenames[i].find("_cropped") == string::npos) {


            string image_path = filenames[i];
            string new_name = image_path.substr(0, image_path.find_last_of('.')) + "_cropped";
            string extension = image_path.substr(image_path.find_last_of("."));
            Mat img = imread(image_path);
            cvtColor(img, img, cv::COLOR_BGR2GRAY);

            img = crop_image(img);
            img = rotate_image(img);
            img = erode_image(img, erosion_size, new_name);
            //img = average_section_threshold(img, sectionCount, new_name);
            //img = dilate_image(img, dilate_size, new_name);

            string new_file_name = new_name + extension;
            imwrite(new_file_name, img);
            cout << "Image Succesfully Saved!";


            }
    }
}

最佳答案

您已经在评论中解决了您的问题,所以我只是将其输入以使其成为正确的答案。

你原来的代码是这样的:

全局变量

extern std::string new_name;

其他文件.cpp

#include "globals.h"

void func(std::string new_name)  // <- shadows the global new_name
{
    new_name += "_stuff";
}

主要.cpp

#include "globals.h"
std::string new_name;

int main()
{
    new_name = "bla";
    func(new_name);

    std::cout << new_name << std::endl;
    return 0;
}

这里main中的new_name没有更新,输出的是bla。发生这种情况是因为 func 中的参数隐藏了全局/外部变量。

使用 extern 的正确方法(如果您真的想保留全局)是删除 func 的参数:

全局变量

extern std::string new_name;

其他文件.cpp

#include "globals.h"

void func()
{
    new_name += "_stuff";
}

主要.cpp

#include "globals.h"
std::string new_name;

int main()
{
    new_name = "bla";
    func();

    std::cout << new_name << std::endl;
    return 0;
}

现在阴影消失了,输出是 bla_stuff。然而,这使用了一个全局变量,这是要避免的,因为全局变量很难推断变量更新的位置、时间和方式(因为它可以在程序的任何地方访问)。

一个更好的解决方案是摆脱全局变量,而是像原来一样传递变量,除了通过引用。

其他文件.cpp

void func(std::string& new_name)
{
    new_name += "_stuff";
}

主要.cpp

int main()
{
    std::string new_name = "bla";
    func(new_name);

    std::cout << new_name << std::endl;
    return 0;
}

现在全局消失了(globals.h 文件也是如此),输出仍然是预期的 bla_stuff

关于c++ - 在 .cpp 文件之间传递全局字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59727356/

相关文章:

sin 到 std::sin 的 C++ 别名——需要草率的快速修复

c++ - 通过原始套接字发送源 MAC 和 IP

javascript - 将变量传递给javascript函数的问题

c++ - 数组引用参数的长度由原型(prototype)中的另一个参数描述

c++ - 以下划线 (_) 为前缀的类成员

c++ - 离散傅立叶变换的实现 - FFT

string - 如何从分隔文本中获取特定字段

c# - 从 C# 中的字符串中提取 5 位数字的最快方法是什么

c - 修剪空间 C 时的 SEGFAULT

php - 为什么 php 函数不更新数组值