当使用“制造”编译的程序中使用库时,应用程序崩溃
Application crash when using library from the program compiled with make
我正在用C 的命令行中的科学计算器进行使用,以供我的用法和实践。我在Windows上使用Mingw使用CMAKE对其进行编译时遇到了问题。这些是我的源文件:
main.ccp
#include <iostream>
#include <string>
#include "ExpressionCalculations/ExpressionParser.h"
int main()
{
std::string humanReadableExpression;
std::cout<<"Enter expressionn";
std::getline(std::cin, humanReadableExpression);
std::cout<<humanReadableExpression;
ExpressionCalculations::ExpressionParser parser;
auto&& expression = parser.GenerateRpnExpression(humanReadableExpression);
return 0;
}
expressionparser.h
#pragma once
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
namespace ExpressionCalculations
{
class ExpressionParser
{
public:
std::unique_ptr<std::string> GenerateRpnExpression(std::string &humanReadableExpression);
private:
// other code
};
}
expressionparser.cpp
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <iostream>
#include "ExpressionParser.h"
namespace ExpressionCalculations
{
std::unique_ptr<std::string> ExpressionParser::GenerateRpnExpression(
std::string& humanReadableExpression)
{
std::unique_ptr<std::string> rpnExpression;
*rpnExpression="3456";
return rpnExpression;
}
这些是cmake文件
主要cmakelists.txt
cmake_minimum_required (VERSION 2.8)
project (ScientificCalculator_exe)
add_subdirectory(ExpressionCalculations)
add_executable(ScientificCalculator main.cpp)
target_link_libraries(ScientificCalculator ExpressionCalculations)
模块cmakelist.txt
set(calculators ExpressionParser.h ExpressionParser.cpp)
add_library(ExpressionCalculations ${calculators})
运行它时,我可以看到输入表达式并传递输入。然后我会得到细分故障。但是,当我删除ExpressionParser的声明并自动&amp; amp;表达式显示了字符串,可以在命令中输入并显示一个字符串。我检查了CMAKE,https://cmake.org/cmake-tutorial/和https://www.codeproject.com/articles/1181455/a-cmake-tutorial-for-for-visual-visual-cplusplus-deplevelmers out https:/cmake.org/cmake.org/cmake.org/cmake.org/cmake.org/cmake.org/cmake.org/cmake-tutorial/-cmake-for-cplusplus-deplovelers outder多个目录,但看来我正确地制作了cmake文件。我不知道为什么它不起作用。我在Windows上使用最新的Mingw64使用默认的"制作汇编"参数。
from solution_ptr上的cppReference页面:
类满足可移动和可移动的要求,但不能满足CopyConstruct或CopyAssign的要求。
在ExpressionParser::GenerateRpnExpression
函数中,您正在尝试将其移动时将unique_ptr从功能中复制出来。尝试return std::move(rpnExpression)
用G 编译的调试程序后,我发现了问题。这是对唯一_ptr的默认构造函数的误解。我认为它会初始化std :: string,但是在阅读DOC并检查它之后,它不会初始化对象并生成nullptr。然后,我研究了斯科特·迈尔斯(Scott Myers(的现代有效C 如何初始化unique_ptr。我使用auto rpnExpression = std::make_unique<std::string>();
而不是std::unique_ptr<std::string> rpnExpression;
。它像魅力一样工作。我检查了通过CMAKE编译,没有任何问题。
- 使用调试/崩溃报告将应用程序部署到客户端
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么从文件获取图标时应用程序有时会崩溃?
- 在 Ubuntu 服务器上运行 QT 应用程序时崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- Qt应用程序找不到第三方DLL并崩溃
- 由于____chkstk_darwin,在Catalina上使用部署10.10编译的MacOS应用程序在HighSierra版本<崩溃
- QCompleter set模型使应用程序崩溃
- Qt 5 应用程序崩溃并出现"qLineEdit::setText"
- 切换NvAPI_Stereo_Deactivate/NvAPI_Stereo_activate会使unity应用程序崩溃
- Opencv在2013年第5季度不起作用.应用程序总是在窗口中崩溃
- eglSwapBuffers上的应用程序崩溃
- C 多线程崩溃应用程序
- 加载的 obj 模型闪烁并崩溃应用程序
- strcats func 崩溃应用程序
- 生成的Protobuf代码崩溃应用程序
- C++字符串::查找崩溃应用程序
- av_free随机崩溃应用程序-FFMPEG C++