【问题标题】:Is there an IDE that offers C++ <Thread> support?是否有提供 C++ <Thread> 支持的 IDE?
【发布时间】:2014-01-06 02:04:55
【问题描述】:

感谢您花时间阅读我的第一个查询。我是 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));

【问题讨论】:

  • 这不是 IDE,而是编译器/库。只要您使用的实现支持它,任何 IDE 都可以工作。甚至是一个在线界面works
  • 克里斯,谢谢您的回复。但是,我对您发布的链接感到有些困惑。我从未使用过 lambda 函数。有没有你可以指点我的资源——你会保证的资源?在过去的几个月里,我正在自己学习所有这些,但学习速度很快。您可以建议我如何调整编译器的任何帮助也将有很大帮助。再次,非常感谢。
  • 如果有帮助的话,我在上面添加了一些代码
  • 我从 Internet 上的随机文章、我自己的测试(开始工作)、阅读 SO 和阅读标准中学到了我所知道的大部分 C++11 知识。一本好书没有的选项,也许是 Bjarne's Tour of C++。

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


【解决方案1】:

您在此行中有多余的括号(包括未闭合的括号):

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

应该是:

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

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

如果没有其余的代码,除了这几个错误之外,很难诊断出更多的错误。我怀疑你的问题不是编译器支持。

【讨论】:

  • Account() 是正确的。更好的是,Account{} 如果编译器支持。
  • @MohammadAliBaydoun - 我的印象是你正在传递一个 RValue std::function,因此不想要括号。
  • 哦。正确的。傻我:v。
  • 谢谢你,pstrjds。 put_time 问题是一个愚蠢的忽视。 Visual Studio 似乎仍然对此语法有问题,并且需要一个类型说明符:std::thread t1 (Account, (NewMommyName));
  • @Novice - 你能发布你得到的错误吗? Account 方法的签名是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多