小编Sla*_*zer的帖子

Qt Creator 2.7.0(Qt 5.0.2)中的C++ 11线程支持

我试图让线程在Ubuntu下的Qt Creator中运行.我订了

QMAKE_CXXFLAGS += -std=c++11 -pthread -lpthread  
CXXFLAGS += -std=c++11 -pthread -lpthread
Run Code Online (Sandbox Code Playgroud)

但它仍然不会工作,会写

terminate called after throwing an instance of ‘std::system_error’ 
  what(): Operation not permitted
Run Code Online (Sandbox Code Playgroud)

我尝试编译的文件就是这个

#include <iostream>
#include <thread>
using namespace std;

void fun(){
}

int main()
{
    thread th(&fun);
    cout << "Hello World!" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ ubuntu qt multithreading c++11

2
推荐指数
1
解决办法
2714
查看次数

检查C++ 11中的字符串是否为T类型

是否有标准库函数来检查字符串S是否为T类型,因此可以转换为T类型的变量?

我知道有一个istringstream STL类可以使用operator >>,用一个从字符串转换的值填充T类型的变量.但是,如果字符串内容没有类型T的格式,它将被填充无意义.

c++ string types

1
推荐指数
1
解决办法
160
查看次数

错误:在';'之前预期的primary-expression 代币

我收到了错误

error: expected primary-expression before ';' token
Run Code Online (Sandbox Code Playgroud)

当我尝试编译以下代码时.问题是什么?

#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;

class MyException : public std::invalid_argument{};

int main() {
    try {
        throw MyException; //here is the problem
    }
    catch (...){
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我也试过这段代码

#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;

class MyException : public std::invalid_argument{};

int main() {
    try {
        throw MyException(); //here is the problem
    }
    catch (...){
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但后来又出现了另一个错误

main.cpp: In constructor ‘MyException::MyException()’:
main.cpp:6:7: error: …
Run Code Online (Sandbox Code Playgroud)

c++ exception

1
推荐指数
3
解决办法
5153
查看次数

在priority_queue内部排序

我的priority_queue是向后排序的,我不明白为什么.这是来自cplusplus.com

表达式comp(a,b),其中comp是这种类型的对象,a和b是容器中的元素,如果a被认为在函数定义的严格弱排序中的b之前,则返回true.

现在在我的Comparator的类operator()函数中,如果a小于b,则a应该在b之前.因此,如果a小于b,则返回true.但最后我得到序列"321",但我反而选择了"123"!

#include <iostream>
#include <queue>
using namespace std;

class Number{
    int x;
public:
    Number(int _x):x(_x){}
    int getX()const{return x;}
};

class Comparator{
public:
    bool operator()(const Number& a,const Number& b){
        if (a.getX()<b.getX()){
            return true;
        } else {
            return false;
        }
    }
};

int main(){
    priority_queue<Number,vector<Number>,Comparator> pq;
    pq.push(2);
    pq.push(1);
    pq.push(3);

    while (!pq.empty()){
        cout<<pq.top().getX();
        pq.pop();
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
96
查看次数

使用 WHEN 时如何避免类型预期错误?

我正在尝试编写Int 和 Priority 之间的类型转换器。这是一个房间数据库。

class PriorityConverter {
    companion object {
        @TypeConverter
        @JvmStatic
        fun intToPriority(number: Int?): Priority? = when (number) {
            is 1 -> Priority.Low
            is 2 -> Priority.Medium
            is 3 -> Priority.High
            else -> Priority.Low
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我收到以下错误。

预期类型

可能是什么问题?

android type-conversion converters kotlin android-room

1
推荐指数
1
解决办法
374
查看次数

释放动态数组的最后一个元素

我有

int * array=new int[2];
Run Code Online (Sandbox Code Playgroud)

我想释放最后一个元素的内存,从而将分配的内存减少到只有 1 个元素。我试着打电话

delete array+1;
Run Code Online (Sandbox Code Playgroud)

但它给出了错误

*** 检测到 glibc *** skuska: free(): 无效指针: 0x000000000065a020 *

这可以在没有显式重新分配的情况下在 C++03 中完成吗?

注意:如果我想使用类而不是原始数据类型(如 int),我该如何释放内存以便也调用类的析构函数?

注2:我正在尝试实施 vector::pop_back

c++ arrays dynamic delete-operator

0
推荐指数
1
解决办法
3170
查看次数

使用std :: cout时的反向顺序

下面的程序输出10.我希望它首先打印0(函数f的else分支)然后打印1.为什么订单被颠倒?

#include <iostream>     
using namespace std;

int f(bool& b){
    if (b==true){
        return 1;
    } else {
        b=true;
        return 0;
    }
}

int main () {
    bool b=false;
    cout<<unitbuf<<f(b)<<unitbuf<<f(b);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出

10
Run Code Online (Sandbox Code Playgroud)

c++ cout

0
推荐指数
1
解决办法
1259
查看次数

char变量是否始终表示为带有{'x',\ 0}的解除引用的char*[2]?

我尝试了这种方法将char数字转换为整数位('3' - > 3),它似乎工作.

char c='x';
int i=atoi(&c);
Run Code Online (Sandbox Code Playgroud)

我的问题是,这总是有效吗?

第一个字符后是否总是有一个NULL字符?

使用隐式类型转换还有另一种方法可以做到这一点,但我不确定这是一个好习惯.实际上,我很惊讶即使使用-Wall和-Wextra也没有任何警告.

int i = c - '0';
Run Code Online (Sandbox Code Playgroud)

注意我正在使用GCC 4.8.2和MinGW.

c c++ casting char c++11

0
推荐指数
1
解决办法
106
查看次数