【问题标题】:simple thread work in c++C++中的简单线程工作
【发布时间】:2009-09-03 03:33:20
【问题描述】:

我正在创建一个简单的 C++ 程序,在主线程中向用户询问华氏温度,然后在另一个线程中将此值转换为摄氏度。

但我继续收到一个错误。这个错误一直存在

visual studio 2008\projects\cs1\cs1\cs1.cpp(16):错误 C2143:语法错误:缺少“;”在'='之前

此问题有时会消失,但不会出现运行时异常。 我使用的是 Visual Studio 2008,Windows XP。

谢谢 -Sunny 耆那教

#include "stdafx.h"
#include "stdafx.h"
#include "windows.h"
#include "stdlib.h"
#include "stdio.h"
#include "process.h"
#include "conio.h"
#include "iostream"
using namespace std;

bool flag= false;

void calculateTemperature_DegreeCelcius(void * Fahrenheit)
{
    float far;
    far=*((float*) Fahrenheit);
    float celcius = (5.0/9.0)*(far - 32);
    cout << "\nDegree Celcius :";
    cout << celcius;  
    flag = true;
}


int _tmain(int argc, _TCHAR* argv[])
{
    float temp_Fahrenheit;

    while(true){
        cout << "\nEnter Degree Fahrenheit value you want to convert to Degree Celcius\n";
        cout << "Degree Fahrenheit :";     
        cin >> temp_Fahrenheit;
        _beginthread(calculateTemperature_DegreeCelcius, 0, &temp_Fahrenheit);
    while(true){
        if(flag==false){
            Sleep(200);
        } else {
            break;
        }
    }

    char *command = (char *)NULL;
    cout<< "\nDo you want to continue ? yes/no :";
    cin>> command;

    if (strcmp("yes",command)){
        flag = false;
    } else {
        break;
    }
    }
    return 0;
}

【问题讨论】:

  • 似乎不是一项足够复杂的任务,无法在不同的线程中完成。

标签: c++ multithreading


【解决方案1】:

在许多 C/C++ 编译器,尤其是 Microsoft 编译器中,far 是保留字(关键字或在标头中定义)。

【讨论】:

  • 再解释一下:far不能用作变量名,因为它有特殊含义。就像您不能将变量命名为“for”或“if”一样。将变量名称更改为其他名称。我个人会选择 degF。
  • 这里是 Visual C++ 关键字列表,远未列出:msdn.microsoft.com/en-us/library/2e6a4at9.aspx
【解决方案2】:

far 是 WinDef.h 中的 #define

#define far
#define near
#if (!defined(_MAC)) && ((_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED))
#define pascal __stdcall
#else
#define pascal
#endif

【讨论】:

    猜你喜欢
    • 2011-01-07
    • 2010-10-10
    • 2013-01-06
    • 2022-01-10
    • 2011-05-09
    • 1970-01-01
    • 2015-03-14
    • 2013-11-22
    相关资源
    最近更新 更多