I'm making scientific calculator in command line in c++ for my usage and also for practice. I have a problem with compiling it using cmake with mingw on windows. These are my source files:
我在c ++的命令行中为我的用法和练习制作科学计算器。我有一个问题,在Windows上使用cingke和mingw编译它。这些是我的源文件:
main.ccp
#include <iostream>
#include <string>
#include "ExpressionCalculations/ExpressionParser.h"
int main()
{
std::string humanReadableExpression;
std::cout<<"Enter expression\n";
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;
}
These are cmake files
这些是cmake文件
main CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (ScientificCalculator_exe)
add_subdirectory(ExpressionCalculations)
add_executable(ScientificCalculator main.cpp)
target_link_libraries(ScientificCalculator ExpressionCalculations)
module CMakeList.txt
set(calculators ExpressionParser.h ExpressionParser.cpp)
add_library(ExpressionCalculations ${calculators})
When I run it , I can see Enter expression and pass input. Then I get Segmentation fault. However when I remove declaration of ExpressionParser and auto&& expression the string is shown, a string can be inputted and shown in the command. I checked configuration question multiple directories under cmake, https://cmake.org/cmake-tutorial/ and https://www.codeproject.com/Articles/1181455/A-CMake-tutorial-for-Visual-Cplusplus-developers but it seems that I correctly made cmake files. I have no idea why it doesn't work. I use the latest mingw64 on windows with default make compilation parameters.
当我运行它时,我可以看到Enter表达式并传递输入。然后我得到Segmentation故障。但是当我删除ExpressionParser和auto &&表达式的声明时,会显示一个字符串,可以输入一个字符串并显示在命令中。我检查了cmake下的配置问题多个目录,https://cmake.org/cmake-tutorial/和https://www.codeproject.com/Articles/1181455/A-CMake-tutorial-for-Visual-Cplusplus-developers但是好像我正确地制作了cmake文件。我不知道为什么它不起作用。我在Windows上使用最新的mingw64和默认的make编译参数。
2 个解决方案
#1
0
From the cppreference page on unique_ptr:
从unique_ptr上的cppreference页面:
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
该类满足MoveConstructible和MoveAssignable的要求,但不满足CopyConstructible或CopyAssignable的要求。
In your ExpressionParser::GenerateRpnExpression
function you are attempting to copy the unique_ptr out of the function when you should be moving it. Try return std::move(rpnExpression)
在ExpressionParser :: GenerateRpnExpression函数中,您试图在移动函数时将unique_ptr复制出函数。尝试返回std :: move(rpnExpression)
#2
0
After debugging program compiled with just g++ I found the problem. It was misunderstanding of unique_ptr' default constructor behaviour. I thought it would initialize std::string but after reading doc and checking it it does not initialize object and generates nullptr. Then I looked into Scott Myers's Modern Effective C++ how to initialize the unique_ptr. Instead of std::unique_ptr<std::string> rpnExpression;
I used auto rpnExpression = std::make_unique<std::string>();
. It works like charm. I checked compiling through cmake and there were not any problems.
在用g ++编译的调试程序后,我发现了问题。这是对unique_ptr'默认构造函数行为的误解。我认为它会初始化std :: string但是在阅读doc并检查它之后它不会初始化对象并生成nullptr。然后我查看了Scott Myers的Modern Effective C ++如何初始化unique_ptr。而不是std :: unique_ptr
#1
0
From the cppreference page on unique_ptr:
从unique_ptr上的cppreference页面:
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
该类满足MoveConstructible和MoveAssignable的要求,但不满足CopyConstructible或CopyAssignable的要求。
In your ExpressionParser::GenerateRpnExpression
function you are attempting to copy the unique_ptr out of the function when you should be moving it. Try return std::move(rpnExpression)
在ExpressionParser :: GenerateRpnExpression函数中,您试图在移动函数时将unique_ptr复制出函数。尝试返回std :: move(rpnExpression)
#2
0
After debugging program compiled with just g++ I found the problem. It was misunderstanding of unique_ptr' default constructor behaviour. I thought it would initialize std::string but after reading doc and checking it it does not initialize object and generates nullptr. Then I looked into Scott Myers's Modern Effective C++ how to initialize the unique_ptr. Instead of std::unique_ptr<std::string> rpnExpression;
I used auto rpnExpression = std::make_unique<std::string>();
. It works like charm. I checked compiling through cmake and there were not any problems.
在用g ++编译的调试程序后,我发现了问题。这是对unique_ptr'默认构造函数行为的误解。我认为它会初始化std :: string但是在阅读doc并检查它之后它不会初始化对象并生成nullptr。然后我查看了Scott Myers的Modern Effective C ++如何初始化unique_ptr。而不是std :: unique_ptr