const std::string::size_type cols = greeting.size() + pad * 2 + 2;
Run Code Online (Sandbox Code Playgroud)
为什么string::size_type
?int
应该工作!它持有数字!
所以我写了一个程序来测试64位kubuntu linux上的线程,版本13.04.实际上我从正在编写测试程序的其他人那里抢了代码.
#include <cstdlib>
#include <iostream>
#include <thread>
void task1(const std::string msg)
{
std::cout << "task1 says: " << msg << std::endl;
}
int main(int argc, char **argv)
{
std::thread t1(task1, "Hello");
t1.join();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我编译使用:
g++ -pthread -std=c++11 -c main.cpp
g++ main.o -o main.out
Run Code Online (Sandbox Code Playgroud)
然后跑了:
./main.out
Run Code Online (Sandbox Code Playgroud)
顺便说一下,当我使用-l'时,main.out会像所有可执行文件一样以绿色文本显示,但在其名称的末尾也有一个星号.为什么是这样?
回到手头的问题:当我运行main.out时,出现了一个错误,其中说:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
任何有关如何解决此问题的想法?
将一些代码从Python转移到C++.
BASEPAIRS = { "T": "A", "A": "T", "G": "C", "C": "G" }
Run Code Online (Sandbox Code Playgroud)
思维导图可能有点矫枉过正?你会用什么?
我目前正在尝试按照教程为自上而下的RPG制作一个简单的2D磁贴引擎.出于某种原因,虽然我得到了智能感知错误
vector is not a template
"向量"一词用红色加下划线.为什么这不起作用?为什么它告诉我它是一个模板,为什么程序不起作用?
#ifndef _IMAGEMANAGER_H
#define _IMAGEMANAGER_H
#include <vector>
#include <SFML\Graphics.hpp>
class ImageManager
{
private:
vector<sf::Texture> textureList;
public:
ImageManager();
~ImageManager();
void AddTexture(sf::Texture& texture);
sf::Texture& GetTexture(int index);
};
#endif
Run Code Online (Sandbox Code Playgroud)
我得到的错误(毫无疑问,其中一些错误来自上面这部分的错误):
错误1错误C2143:语法错误:缺少';' 在'<'c:\ users\vipar\dropbox\computer science\programming\visual studio 2012\projects\sfml-app\sfml-app\imagemanager.h 10 1 sfml-app
错误2错误C4430:缺少类型说明符 - 假定为int.注意:C++不支持default-int c:\ users\vipar\dropbox\computer
science\programming\visual studio
2012\projects\sfml-app\sfml-app\imagemanager.h 10 1 sfml-app错误3错误C2238:';'之前的意外令牌 c:\ users\vipar\dropbox\computer science\programming\visual studio 2012\projects\sfml-app\sfml-app\imagemanager.h 10 1 sfml-app
错误4错误C2143:语法错误:缺少';' 在'<'c:\ users\vipar\dropbox\computer science\programming\visual studio 2012\projects\sfml-app\sfml-app\imagemanager.h 10 1 sfml-app
错误5错误C4430:缺少类型说明符 - 假定为int.注意:C++不支持default-int c:\ users\vipar\dropbox\computer
science\programming\visual studio
2012\projects\sfml-app\sfml-app\imagemanager.h 10 …
在这个问题中讨论了什么时候在C++ 11中创建一个不可移动的类型,我发现Scott Meyers在comp.std.c ++上有类似的问题,其中列出的类类型下面的SG在C++ 11中不可移动.
问题是为什么all iterators / iterator adaptors
不能移动?
你能教我两者吗?
std::unordered_map::insert(const value_type&)
Run Code Online (Sandbox Code Playgroud)
和
template<class P> std::unordered_map::insert(P&&)
Run Code Online (Sandbox Code Playgroud)
存在于标准中?
我认为这insert(P&&)
可以作为insert(const value_type&)
.
我正在尝试使用std::shared_ptr
和处理对象std::weak_ptr
.场景是这样的:
我有一个类的对象,channel
它派生自一个抽象类abstract::channel
(使用纯虚函数).我有一个容器channelContainer
(std::vector
)包含对象的共享指针(std::shared_ptr
)channel
.
现在,我有一个deque (std::deque)
包含弱指针(std::weak_ptr)
的每个对象channelContainer
.让我们说出这个双端队列freeChannelQueue
.
所以我们说:
std::vector<std::shared_ptr<abstract::channel> > channelContainer;
std::deque<std::weak_ptr<abstract::channel > > freeChannelQueue;
//Assuming that both the containers are filled appropriately How do I go about implementeing the below functions?
abstract::channel& get_free_channel() {
//This should return a free channel object from 'freeChannelQueue' and pop the queue.
}
bool release_channel(abstract::channel& ch) {
//This should convert 'ch' to a …
Run Code Online (Sandbox Code Playgroud) 在unix中,似乎有两种从C运行外部可执行文件的常用方法
system()
Run Code Online (Sandbox Code Playgroud)
打电话和
pid = fork()
switch(pid)
//switch statement based on return value of pid,
//one branch of which will include and exec() command
Run Code Online (Sandbox Code Playgroud)
在函数等效的情况下,有没有理由更喜欢fork/exec而不是系统(父进程等待子进程完成,没有从子进程返回复杂的信息)?
我在SO上看到了类似的问题,但是没有回答我的问题.在这里,我试图发送和recv字符串:
我发送std :: string:
if( (bytecount=send(hsock, input_string.c_str(), input_string.length(),0))== -1)
Run Code Online (Sandbox Code Playgroud)
这可以正确接收吗?
if ((bytecount = recv(*csock, rcv.c_str(), rcv.length(), 0)) == -1)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
错误:在recv行上从'const void*'无效转换为'void*'[-fpermissive]`!
我有这个代码:
#include <iostream>
#include <functional>
struct Foo
{
int get(int n) { return 5+n; }
};
int main()
{
Foo foo;
auto L = std::bind(&Foo::get, &foo, 3);
std::cout << L() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
似乎这个:
auto L = std::bind(&Foo::get, &foo, 3);
Run Code Online (Sandbox Code Playgroud)
相当于:
auto L = std::bind(&Foo::get, foo, 3);
Run Code Online (Sandbox Code Playgroud)
为什么?