小编Tob*_*oby的帖子

带表达式的C++模板参数

我在使用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.为什么它认为有两个参数?我该如何解决这个问题?

(对不起,如果这是一个副本我确实很好看的解决方案)

c++ templates

9
推荐指数
1
解决办法
415
查看次数

从void方法启动一个线程

使用C++,我想从void方法启动一个线程,然后在线程完成之前返回.例如:

#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试图删除,并失败.我怎样才能做到这一点?

c++ multithreading

3
推荐指数
1
解决办法
136
查看次数

C++ std :: cout和字符串打印顺序错误

我有一个使用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,而不是之后.有人知道为什么会这样吗?它很容易修复吗?

谢谢.

c++ g++

2
推荐指数
1
解决办法
1279
查看次数

标签 统计

c++ ×3

g++ ×1

multithreading ×1

templates ×1