c++ - 是否有提供 C++ <Thread> 支持的 IDE?

标签 c++ eclipse multithreading dev-c++

感谢您花时间阅读我的第一个查询。我是 C++ 的新手,老实说我已经筋疲力尽了。我花在寻找支持“线程”的 IDE 上的时间比学习这门语言的时间还多。尝试过 DevC++ 5.5.3、Eclipse 4.3.1,目前正在尝试 Visual Studio Express 2013。我认为我的代码很可靠,但当然我可能是错的。很可能是错误的,也许。这是一个示例:

    #ifdef _MSC_VER
    #define _CRT_SECURE_NO_WARNINGS
    #endif
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <string> 
    #include <chrono>
    #include <ctime>
    #include <iomanip>
    #include <thread>
    #include "IntervalSelection.h"
    #include "Acnt.h"
    using namespace std;

    int main(int argc, char *argv[])
    {
cout << "Hello, Mommy. To begin, what is your name?\n";
char NewMommyName[40];
cin >> NewMommyName;
std::thread t1 (Account()), (NewMommyName));
Supply * pS;
std::time_t NewPumpTime = PumpTime(pS);
struct tm * ptm = std::localtime(&NewPumpTime);
std::chrono::steady_clock::time_point TP =    
std::chrono::steady_clock::from_time_t(mktime(ptm));
std::this_thread::sleep_until(TP);
std::cout << " The time is now " << std::put_time (TP, "%X")<<"\nTime to pump!";
t1.join();
    //The following errors are generated:
    //"no instance of function template "std::put_time" matches the argument list"
    //"expression must have class type" (t1.join())
    //"expected a ';' - on this line: std::thread t1 (Account()), (NewMommyName));
    //"left of '.join' must have class/struct/union"
    //"syntax error : ')' - on this line: std::thread t1 (Account()), (NewMommyName));
    //error C2040: 'NewMommyName' : 'std::thread' differs in levels of indirection   
    //from 'char [40]'

似乎有很多比我经验丰富的人提示找不到线程支持。是我的错误还是我无法解决的错误?有没有我可以使用的 IDE,可以减少这方面的麻烦?

下面是两个头文件中更重要的部分以及我在尝试构建时遇到的错误:

    #ifndef ACNT_H
    #define ACNT_H

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <string>
    #include <chrono>
    #include <ctime>
    #include <iomanip>
    #include <thread>
    #include "IntervalSelection.h"
    using namespace std;

    inline
    std::string timeString(const std::chrono::steady_clock::time_point& tp)
    {
std::time_t t = std::chrono::steady_clock::to_time_t(tp);
std::string ts = ctime(&t);
ts.resize(ts.size() - 1);
return ts;
    }
    inline
     std::chrono::steady_clock::time_point MakeTime(int year, int mon, int day, int    
    hour, int min, int sec = 0)
    {
struct std::tm t;
t.tm_sec = sec;
t.tm_min = min;
t.tm_hour = hour;
t.tm_mday = day;
t.tm_mon = mon - 1;
t.tm_year = year - 1900;
t.tm_isdst = -1;
std::time_t tt = std::mktime(&t);
if (tt == -1){ throw "Not a valid system time."; }
return std::chrono::steady_clock::from_time_t(tt);
    }

    class Supply
    {
friend time_t PumpTime(Supply*);
    public:
   Supply()
    {
    double MilkOz = 0.0;
    long long tempInterval = 0;
    char* dayOrnight;
    int year, mon, day, hour, min, sec = 0;
    char morning[5] = "a.m.";
    char  evening[5] = "p.m.";
    cout << "\nHow many ounces of milk do you currently have in your    
            supply?\n";
    cout << "For greater accuracy, feel free to enter this value in decimal  
            form (e.g. 4.5): ";
    cin >> MilkOz;
    cout << "\nIdeally, how often would you like to pump?\n";
    tempInterval = IntervalSelection();
    std::chrono::steady_clock::time_point NOW =  
            std::chrono::steady_clock::now();
    time_t currentTp = std::chrono::steady_clock::to_time_t(NOW);
    struct tm* local;
    local = localtime(&currentTp);
    switch (local->tm_hour){ case 12: dayOrnight = evening; break; case 0: 
            local->tm_hour = (local->tm_hour) + 12; dayOrnight = morning; break; }
    if (local->tm_hour > 12){ local->tm_hour = (local->tm_hour) - 12;  
            dayOrnight = evening;; }
    else if (local->tm_hour < 12){ dayOrnight = morning; }
    printf("The time is now %d: %d: %d %s", local->tm_hour, local->tm_min, 
            local->tm_sec, dayOrnight);
    cout << "\n";
    std::chrono::steady_clock::time_point tNew = 
            std::chrono::steady_clock::from_time_t(currentTp);
    static std::chrono::steady_clock::time_point tt = tNew +    
            std::chrono::hours(tempInterval);
    std::time_t structable, structable2 = 
            std::chrono::steady_clock::to_time_t(tt);
    struct tm* localNew;
    localNew = localtime(&structable);
    switch (localNew->tm_hour){ case 12: dayOrnight = evening; break; case 0: 
           localNew->tm_hour = (localNew->tm_hour) + 12; dayOrnight = morning; break; }
    if (localNew->tm_hour > 12){ localNew->tm_hour = (localNew->tm_hour) - 12; 
            dayOrnight = evening; }
    else if (localNew->tm_hour < 12){ dayOrnight = morning; }
    printf("Your new pump time is scheduled for %d: %d: %d %s", 
            localNew->tm_hour, localNew->tm_min, localNew->tm_sec, dayOrnight);
    cout << "\nTo accept this time enter 'yes'. To specify a different time, 
            enter 'no'.\n";
    char choice[4];
    const char* yes = "yes";
    const char* no = "no";
    cin >> choice;
    if (strncmp(choice, yes, 1) == 0)
    {
        std::string date = ctime(&structable);
        date.resize(date.size() - 1);
        cout << "Thank you. Your next pump time is confirmed for " << date;
        std::time_t tpNewest = structable2;
    }
    else if (strncmp(choice, no, 1) == 0)
    {

        cout << "Please enter an exact date to schedule your next pump 
                    time.\n";
        cout << "Enter the current year: ";
        cin >> year;
        cout << "\nEnter a numerical value for the month. For example, the 
                    number one is equivalent to the month of January: ";
        cin >> mon;
        cout << "\nEnter the numerical day of the month: ";
        cin >> day;
        cout << "\nEnter the hour: ";
        cin >> hour;
        cout << "\nEnter the minutes: ";
        cin >> min;
        static auto tpNew = MakeTime(year, mon, day, hour, min, sec);
        cout << "Your next pump time is scheduled for " << 
                    timeString(tpNew) << endl;
        std::time_t tpNewest = std::chrono::steady_clock::to_time_t(tpNew);
            }
                }

                    double entry = 0;
                double getSupply(double MilkOz, double entry)
                {
            TotalSupply = MilkOz + entry;
            return TotalSupply;
                 }
               ~Supply(){}
                    private:
                double TotalSupply;
                std::time_t tpNewest;
                    };

                    time_t PumpTime(Supply* pS)
                    {
                return pS->tpNewest;
                    }

                   class Baby
                   {
                    public:
                Baby()
                {
          double lbs = 0.0;
          double oz = 0.0;
          char pbName[40];
          char pbGender[40];
          cout << "\nWhat is your Baby's name?" << endl;
    cin >> pbName;
    strncpy(BabyName, pbName, 40);
    cout << "And is " << this->BabyName << " a Boy or a Girl?\n";
    cin >> pbGender;
    strncpy(BabyGender, pbGender, 5);
    if (strncmp(this->BabyGender, "boy", 1) == 0)
    {
        cout << "\nWhat is his weight in pounds and ounces?\n";
        cout << "For example: My baby weighs 16 lbs and 4 oz. \n";

        cout << "Pounds: ";
        cin >> lbs;
        cout << "\n Ounces: ";

        cin >> oz;
        Baby::getWeight(lbs, oz);
    }
    else if (strncmp(this->BabyGender, "girl", 1) == 0)
     {
        cout << "\nWhat is her weight in pounds and ounces?" << endl;
        cout << "For example: My baby weighs 16 lbs and 4 oz. \n";
        cout << "Pounds: ";
        cin >> lbs;
        cout << "\nOunces: ";
        cin >> oz;
        Baby::getWeight(lbs, oz);
    }
    cout << "\n" << this->BabyName << "'s current weight is " << 
            this->BabyWeight << " pounds";
        }
        double getWeight(double pounds, double ounces)
        {
    ounces = ounces * 1 / 16;
    BabyWeight = pounds + ounces;
    return BabyWeight;
        }
        ~Baby(){}
             private:
         char BabyName[40];
        char BabyGender[5];
         double BabyWeight;
            };

            class Mommy
             {
            public:
          Mommy(char* paName)
        {
      strncpy(MommyName, paName, 40);
        }
        ~Mommy(){}
            private:
        char MommyName[40];
        Baby b;
            Supply s;
             };

           class Account
           {
           public:
       Account(char* paName) :m(paName)
       {
    strncpy(Name, paName, 40);
       }
       ~Account(){}
           private:
        char Name[40];
       Mommy m;
           };
       #endif
       //ERRORS:
       //expression must have class type -t1.join();
       //expected a type specifier -std::thread t1 (Account()), (NewMommyName));
       //left of '.join' must have class/struct/union   
       //syntax error : '(' -std::thread t1 (Account()), (NewMommyName));

最佳答案

您在这一行中有额外的括号(包括未闭合的括号):

std::thread t1 (Account()), (NewMommyName));

应该是:

std::thread t1 (Account, (NewMommyName));

此外 - 您调用 std::put_time是不正确的。 put_time 采用 const std::tm* 而不是 time_point。您需要将其转换为 std::tm 才能将其传递给 put_time

如果没有其余代码,就很难诊断出这对错误。我怀疑您的问题不是编译器支持。

关于c++ - 是否有提供 C++ <Thread> 支持的 IDE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20648919/

相关文章:

eclipse - 如何用Bnd创建osgi片段?

java - Galileo 可以使用在 Helios 中创建的工作区/项目吗?

sql - Grails 服务中的数据源注入(inject)

c++ - 如何将参数传递给工厂元素构造函数?

apache-flex - 为什么 Flash Builder 4 使用 Flash Player 而不是浏览器来运行应用程序?

multithreading - 多线程

java - 如何在多线程 Java 应用程序中安全地使用 OracleDriver.defaultConnection()?

c++ - 将结构指针传递给带索引与不带索引的函数

c++ - 用 std::addressof 存储指针地址

c++ - 始终返回的地址为0而不是实际值