我在使用C++时遇到了麻烦.我希望能够将一个表达式放在模板中作为参数.这是我的代码:
#include <vector>
using namespace std;
vector< ((1>0) ? float : int) > abc() {
}
int main(void){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
main.cpp:11:14:错误:模板参数1无效
main.cpp:11:14:错误:模板参数2无效
main.cpp:11:15:错误:预期在'{'令牌之前的unqualified-id
最后,我希望能够替换1和0的任何内容以及typename T和U的float和int.为什么它认为有两个参数?我该如何解决这个问题?
(对不起,如果这是一个副本我确实很好看的解决方案)#include <thread>
using namespace std;
void longFunc(){
//stuff
}
void startThread(){
thread t(longFunc);
}
int main(void){
startThread();
//lots of stuff here...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当startThread()完成后,T试图删除,并失败.我怎样才能做到这一点?
我有一个使用g ++编译器在Linux上运行的简单程序:
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv){
fstream file;
string s;
file.open("sample/dates.dat", fstream::in);
if(!file.good())
return 0;
getline(file, s);
cout << s << "." << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:g++ -o test test.cpp.当我运行它时,在字符串s之前打印fullstop,而不是之后.有人知道为什么会这样吗?它很容易修复吗?
谢谢.