这是简单的代码:
QByteArray ba = jdoc.toBinaryData();
QJsonDocument jdoc2;
jdoc2.fromBinaryData(ba);
qDebug() << jdoc.isNull();
qDebug() << jdoc2.isNull();
Run Code Online (Sandbox Code Playgroud)
结果:jdoc
不为空,而是jdoc2
为空。我究竟做错了什么?看起来jdoc2.fromBinaryData(ba);
根本不起作用。我使用 Qt 5.5.0
我阅读了有关智能指针,移动语义和迭代器的所有可能的引用,但我仍然在努力理解为什么以下C++ 14代码段错误:
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
int main(int argc, char **argv)
{
vector<unique_ptr<int>> ints (10);
for(int i = 0; i < 10; ++i) {
unique_ptr<int> number = make_unique<int>(5);
ints.push_back(move(number));
}
for(auto& n : ints) {
cout << *n << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
g++ <filename> -std=c++14 -o <executable>
Run Code Online (Sandbox Code Playgroud) 我是新手使用CMake.我正在尝试创建一个简单的CMakeList文件,并从QT5 5.7添加对QPrinter和QTextDocument的支持.从我发现的不好,我必须将以下库添加到我的CMakeList文件:
这就是我现在所拥有的:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
PROJECT (photobooth)
find_package( Qt5Core )
find_package( Qt5PrintSupport )
find_package( Qt5Gui )
find_package( Qt5Widgets )
set( NAME_SRC
src/main.cpp
src/photobooth.cpp
)
set( NAME_HEADERS
include/photobooth.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( photobooth ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( photobooth Qt5::Widgets )
target_link_libraries( photobooth Qt5::Core )
target_link_libraries( photobooth Qt5::Qt5PrintSupport )
target_link_libraries( photobooth Qt5::Qt5Gui )
Run Code Online (Sandbox Code Playgroud)
这是Cmake的输出:
Configuring done
CMake Warning (dev) in CMakeLists.txt:
Policy CMP0020 is not set: Automatically link Qt executables to qtmain …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Qt 5.7,并且我正在尝试在名为 MyClass 的类中使用带有超时事件的计时器。我在我的项目中添加了这个类。我在 mainwindow.h 中创建了这个类的公共变量。我将计时器的插槽连接放在 MyClass 的构造函数中。我在 MyClass 中还有一个函数来启动计时器。但是当我执行该函数时,定时器槽不起作用。我在构建时没有错误,但我在运行时在应用程序窗口上得到以下评论: QObject::connect: No such slot QObject::on_timeout()
这是我的代码:
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "myclass.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MyClass mc;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myclass.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
} …
Run Code Online (Sandbox Code Playgroud) 我真的很难得到递归,但我尝试递归来匹配字符串中的模式.
假设我有一个极客的字符串极客,我有一个模式eks匹配.我可以使用很多方法,如正则表达式,查找字符串类的方法,但我真的想通过递归来做这件事.
为实现这一点,我试过这段代码:
void recursion(int i,string str)
{
if(!str.compare("eks"))
cout<<"pattern at :"<<i<<'\n';
if(i<str.length() && str.length()-1!=0)
recursion(i,str.substr(i,str.length()-1));
}
int main()
{
string str("geeks for geeks");
for(int i=0;i<str.length();i++)
recursion(i,str.substr(i,str.length()));
}
Run Code Online (Sandbox Code Playgroud)
输出:
期望的输出:
pattern at 2
pattern at 12
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么,以及通过递归来做这件事的好方法是什么?
我理解cpp中的很多主题,但是通过递归,我知道它们是如何工作的,甚至每当我尝试使用递归编写代码时,它都无法工作.是否有任何地方可以帮助我进行递归?
我正在研究一个问题,要求创建一个类Rational
,变量为分子和分母,以及一个函数来添加两个有理数.
这是我创建的课程:
class Rational
{
public:
// n stands for numerator and d stands for denominator
double n,d;
// add stands for addition
void add(Rational r1, Rational r2);
};
Run Code Online (Sandbox Code Playgroud)
这是我的功能:
void Rational::add(Rational r1, Rational r2)
{
// ar stands for rational number after addition
double ar;
// new object which takes the value after addition
Rational r3;
// formula for numerator after addition
r3.n=(r1.n*r2.d)+(r2.n*r1.d);
// formula for denominator after addition
r3.d=(r1.d*r2.d);
// ratio equals divisor plus quotient multiplied …
Run Code Online (Sandbox Code Playgroud) 我有一个子列表列表.每个子列表都有自己的子项.我需要将样式应用于除最后一个子列表之外的所有样式.我用:
.list:not(:last-child) > .sublist {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
但它适用于所有子列表.这是一个演示代码(小提琴:http: //jsfiddle.net/8m72m53r/3/):
:not(:last-child) > .row {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
<ul class="box">
<li class="row">One</li>
<li class="row">Two</li>
<li class="row">Three</li>
</ul>
<ul class="box">
<li class="row">FOur</li>
<li class="row">Five</li>
<li class="row">Six</li>
</ul>
<ul class="box">
<li class="row">Seven</li>
<li class="row">Eight</li>
<li class="row">Nine</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我遇到以下输出的编译时错误:
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename main.cpp .cpp` main.cpp StringMethods/StringMethods.cpp `pkg-config --libs opencv` -std=c++0x
In file included from main.cpp:2:0:
VideoFile.hpp:32:10: error: ‘bool VideoFile::fileExists(const string&)’ cannot be overloaded
bool fileExists(const std::string & path)
^
VideoFile.hpp:15:10: error: with ‘bool VideoFile::fileExists(const string&)’
bool fileExists(const std::string & path);
Run Code Online (Sandbox Code Playgroud)
但是,我没有看到该错误是如何有意义的,因为我只有函数声明,我在编写定义时直接复制和粘贴.
class VideoFile
{
private:
std::string filePath;
bool fileExists(const std::string & path);
public:
VideoFile(const std::string & path)
:filePath(path)
{
filePath = StringMethods::trim(path);
if (!fileExists(filePath))
throw std::runtime_error("The file: " + filePath + " was …
Run Code Online (Sandbox Code Playgroud) 下面的代码解释了一个怪癖,我无法理解为什么它是这样的.代码中的注释解释了这个怪癖:
#include <iostream>
#include <string>
using namespace std;
int main() {
string a,b;
a = "abc";
b = "def";
// On the fly calculation of diff does not lead to desirable results.
cout<<"a size: "<<a.size()<<" b size: "<<b.size()<<" diff: "<<a.size()-b.size()-1<<endl;
// Following assignment works as expected.
int diff = a.size()-b.size()-1;
cout<<"diff "<<diff<<endl;
for (int i=a.size()-1; i>a.size()-b.size()-1; --i) {
cout<<"running"<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
a size: 3 b size: 3 diff: 4294967295
diff: -1
Run Code Online (Sandbox Code Playgroud) 我对此问题有些困惑:
int a = 5, b = 2;
double c = a / b;
cout << c;
Run Code Online (Sandbox Code Playgroud)
输出:
2
Run Code Online (Sandbox Code Playgroud)
为什么呢
我可以通过使用以下方式通过:
double aa = a,
bb = b;
c = aa / bb;
Run Code Online (Sandbox Code Playgroud)
输出:
2.5
Run Code Online (Sandbox Code Playgroud)
救命 !:(
#include <stdio.h>
int main(void){
double arr[] = { 1.1, 2.2, 3.3, 4.4 };
int i;
for (i=0; i<=4; i++) {
printf("%d\n", i);
arr[i] = 0;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用gcc(c90)编译得到一个无限循环(1,2,3,4,1,2,3,4 ...),而用gcc(c99)编译只产生(0,1,2,3,4) .有什么可归因于这种差异?