感谢您抽出宝贵时间阅读我的第一个查询。我是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(¤tTp);
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));
答案 0 :(得分:3)
此行中有额外的括号(包括未闭合的括号):
std::thread t1 (Account()), (NewMommyName));
应该是:
std::thread t1 (Account, (NewMommyName));
此外 - 您对std::put_time的来电不正确。 put_time
需要const std::tm*
而不是time_point
。您需要将其转换为std::tm
才能将其传递给put_time
如果没有其余的代码,很难诊断出那些错误。我怀疑你的问题不是编译器支持。