- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我创建了一个 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/
我的应用程序中有一个 settings.php 页面,它使用 $GLOBALS 来存储网络应用程序中使用的配置。 例如,他是我使用的一个示例设置变量: $GLOBALS["new_login_page
我正在尝试编译我们在 OS 类上获得的简单操作系统代码。它在 Ubuntu 下运行良好,但我想在 OS X 上编译它。我得到的错误是: [compiling] arch/i386/arch/start
我知道distcp无法使用通配符。 但是,我将需要在更改的目录上安排distcp。 (即,仅在星期一等“星期五”目录中复制数据),还从指定目录下的所有项目中复制数据。 是否有某种设计模式可用于编写此类
是否可以在config.groovy中全局定义资源格式(json,xml)的优先级,而不是在每个Resource上指定?例如,不要在@Resource Annotation的参数中指定它,例如: @R
是否有一些简单的方法来获取大对象图的所有关联,而不必“左连接获取”所有关联?我不能只告诉 Hibernate 默认获取 eager 关联吗? 最佳答案 即使有可能有一个全局 lazy=false(谷歌
我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作...... 我尝试了以下操作,但对话框没有更新...最后一次,
当我偶然发现 this question 时,我正在阅读更改占位符文本。 无论如何,我回去学习了占位符。一个 SO 的回答大致如下: Be careful when designing your pl
例如,如果我有这样的文字: "hello800 more text 1234 and 567" 它应该匹配 1234 和 567,而不是 800(因为它遵循 hello 的 o,这不是一个数字)。 这
我一直在尝试寻找一种无需使用 SMS 验证系统即可验证电话号码(Android 和 iPhone)的方法。原因纯粹是围绕成本。我想要一个免费的解决方案。 我可以安全地假设 Android 操作系统会向
解决此类问题的规范 C++ 设计模式是什么? 我有一些共享多个类的多线程服务器。我需要为大多数类提供各种运行时参数(例如服务器名称、日志记录级别)。 在下面的伪 C++ 代码中,我使用了一个日志记录类
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 9 年前。 我是 python 的新手,所以可能有一个简单的答案,但我
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Does C++ call destructors for global and class static
我正在尝试使用 Objective-C 中的 ArrayList 的等价物。我知道我必须使用 NSMutableArray。我想要一个字符串列表 (NSString)。关键是我的列表应该可以从我类(c
今天刚开始学习 Android 开发,我找不到任何关于如何定义 Helper 类或将全局加载的函数集合的信息,我会能够在我创建的任何 Activity 中使用它们。 我的计划是创建(至少目前)2 个几
为什么这段代码有效: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) fun
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS
我想知道如何实现一个可以在任何地方使用您自己的设置的全局记录器: 我目前有一个自定义记录器类: class customLogger(logging.Logger): ... 该类位于一个单独的
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
每次我遇到单例模式或任何静态类(即(几乎)只有静态成员的类)的实现时,我想知道这是否实际上不是一种黑客行为,因此只是为了设计而严重滥用类和实例的原则单个对象,而不是设计类和创建单个实例。对我来说,看起
这个问题在这里已经有了答案: Help understanding global flag in perl (2 个回答) 7年前关闭。 my $test = "There was once an\n
我是一名优秀的程序员,十分优秀!