【问题标题】:Dev C++ simple threading programDev C++ 简单线程程序
【发布时间】:2013-11-22 17:11:51
【问题描述】:

我创建了一个简单的程序来学习如何使用线程。这是我创建的代码

#include <iostream>
#include <stdlib.h>
#include <thread>

using namespace std;

void count_asc();
void count_desc();

int main() {
    thread first(count_asc);
    thread second(count_desc);

    first.join();
    second.join();

    system("pause");
    return 0;
 }

void count_asc(){
    int ctr;
    for(ctr=1;ctr<=10;ctr++){
        cout<<"First thread: "<<ctr<<endl;
    }
}

void count_desc(){
    int ctr;
    for(ctr=10;ctr>=1;ctr--){
        cout<<"Second thread: "<<ctr<<endl;
    }
}

我正在使用 Dev C++ 5.5.3。我已经阅读了有关此的其他问题,但我作为编程初学者并不能真正理解高级指令。编译此代码时会产生以下错误

main.cpp: In function 'int main()':
main.cpp:11:2: error: 'thread' was not declared in this scope
main.cpp:11:9: error: expected ';' before 'first'
main.cpp:12:9: error: expected ';' before 'second'
main.cpp:14:2: error: 'first' was not declared in this scope
main.cpp:15:2: error: 'second' was not declared in this scope

我已经在 c++ 编译器中包含了 -std=c++11 在 Dev C++ 的项目选项中的附加命令行选项,但我仍然无法删除错误。你能检查一下我做错了什么吗?我也尽可能不想开始使用其他库,因为我很难构建它们(例如 boost)

【问题讨论】:

  • Dev C++ 使用哪个编译器(包括版本)?
  • 我认为这是一个 TDM-GCC 4.7.1 32/64bit
  • 我已经用Windows 标记了你的问题,正如你所说的编译器所暗示的那样。

标签: c++ windows gcc c++11 dev-c++


【解决方案1】:

此问题很可能是由于 TDM-GCC 随附的 GCC 4.7.1 构建中缺乏对 C++11 的 std::thread 的支持。详情请查看this question。您的代码在 GCC 4.8.1 下编译得很好(尽管它仍然存在运行时错误):

http://ideone.com/oUhvi3

因此,我建议您尝试更新到更新版本的编译器来解决您的问题。根据this linkthis link 的说法,这样做应该很简单,只需将最新版本的编译器安装到它当前所在的文件夹中,或者将其安装到新文件夹中并将 Dev C++ 中的设置更新为指向新的编译器。

但是,由于您是 C++ 新手(和一般编程),因此对 Dev C++ 没有特别的依恋,我建议您改用更现代和更广泛使用的 IDE。 MS Visual Studio 对于 Windows 来说是一个不错的选择,但是有很多开源和跨平台的 IDE 可用于 C++。建议初学者使用流行的 IDE,因为当您遇到困难时,您更有可能在线找到帮助和支持来源,并且更有可能在 Stackoverflow 等网站上获得答案。有大量与 IDE 相关的 Stackoverflow 问题。示例(来自 Google 的快速搜索):

What is the good cross platform C++ IDE?

Best C++ IDE or Editor for Windows

https://stackoverflow.com/questions/535369/what-is-the-best-free-windows-c-ide-compiler

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多