我通过线程获得C++错误:
terminate called without an active exception
Aborted
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
template<typename TYPE>
class blocking_stream
{
public:
blocking_stream(size_t max_buffer_size_)
: max_buffer_size(max_buffer_size_)
{
}
//PUSH data into the buffer
blocking_stream &operator<<(TYPE &other)
{
std::unique_lock<std::mutex> mtx_lock(mtx);
while(buffer.size()>=max_buffer_size)
stop_if_full.wait(mtx_lock);
buffer.push(std::move(other));
mtx_lock.unlock();
stop_if_empty.notify_one();
return *this;
}
//POP data out of the buffer
blocking_stream &operator>>(TYPE &other)
{
std::unique_lock<std::mutex> mtx_lock(mtx);
while(buffer.empty())
stop_if_empty.wait(mtx_lock);
other.swap(buffer.front());
buffer.pop();
mtx_lock.unlock();
stop_if_full.notify_one();
return *this;
}
private:
size_t max_buffer_size;
std::queue<TYPE> buffer;
std::mutex mtx;
std::condition_variable stop_if_empty,
stop_if_full;
bool …
Run Code Online (Sandbox Code Playgroud) 编辑:解决看到评论 - 不知道如何标记解决与答案.
在c ++ 0x中观看有关完美转发/移动语义的第9频道视频之后,我认为这是编写新分配运算符的好方法.
#include <string>
#include <vector>
#include <iostream>
struct my_type
{
my_type(std::string name_)
: name(name_)
{}
my_type(const my_type&)=default;
my_type(my_type&& other)
{
this->swap(other);
}
my_type &operator=(my_type other)
{
swap(other);
return *this;
}
void swap(my_type &other)
{
name.swap(other.name);
}
private:
std::string name;
void operator=(const my_type&)=delete;
void operator=(my_type&&)=delete;
};
int main()
{
my_type t("hello world");
my_type t1("foo bar");
t=t1;
t=std::move(t1);
}
Run Code Online (Sandbox Code Playgroud)
这应该允许将r值和const分配给它.通过使用适当的构造函数构造一个新对象,然后使用*this交换内容.这对我来说听起来很合理,因为没有数据被复制超过它需要的数量.指针算术很便宜.
但是我的编译器不同意.(g ++ 4.6)我得到了这些错误.
copyconsttest.cpp: In function ‘int main()’:
copyconsttest.cpp:40:4: error: ambiguous overload for ‘operator=’ in ‘t = …
Run Code Online (Sandbox Code Playgroud) 我希望能够将我的bin和我的代码文件拆分成单独的目录,因为它在当前状态下变得难以管理.
我理想的是希望拥有
project_dir
|-Makefile
|-run_tests.sh
|
|__source
| |-program1.cpp
| |-program2.cpp
|
|__bin
|-program1
|-program2
Run Code Online (Sandbox Code Playgroud)
但是我无法使用我当前的系统,而不必手动写出每个程序的规则(请记住,每个程序都是一个单独的程序,而不是一系列链接在一起的对象)
#Current make system
BIN=./bin/
SOURCE=./source/
LIST=program1 program2...
all: $(LIST)
%: $(SOURCE)%.cpp
$(CC) $(INC) $< $(CFLAGS) -o $(BIN)$@ $(LIBS)
Run Code Online (Sandbox Code Playgroud)
这工作除了它我无法在当前路径中看到目标,因此它认为它总是重建二进制文件,即使源文件没有更改.
我目前唯一想到的是编写一个程序来制作一个makefile,但我不想这样做.
Hi I am trying to serialize an array of objects which are derived from a class and I keep hitting the same error using c#. Any help is much appreciated.
obviously this example has been scaled down for the purpose of this post in the real world Shape would contain a plethora of different shapes.
Program.cs
namespace XMLInheritTests
{
class Program
{
static void Main(string[] args)
{
Shape[] a = new Shape[1] { new Square(1) };
FileStream fS = new …
Run Code Online (Sandbox Code Playgroud) 这不是一个技术问题,而是一个c ++设计问题.
通常,似乎我必须设计程序,必须管理一些具有某种连接的协议,解析阶段和抽象视图.通常情况下,我会尝试将我的程序设计为最前沿的关注点.
我一直以对象的"堆栈"结束,系统位于解析器的顶部,而解析器又位于连接的顶部(通常有更多的层).然后,这些对象使用成员函数调用来调用它下面的层(Tx),并使用回调(std::function
通常)来捕获来自其他方向(Rx)的信息.
这种设计似乎非常低,因为它增加了复杂性,每层都必须有一个逐渐变大的构造函数等等.另外因为连接通常使用类似ASIO的东西,回调通常在不同的线程上,因此很难推断线程安全.
是否有更好的设计模式或成语代表这种结构/功能?
一个简单的例子
class basic_connection {
basic_connection(std::string address);
void send(std::string);
std::function<void(std::string)> on_receive;
};
Run Code Online (Sandbox Code Playgroud)
我有一些像这样的类,它拥有该层的状态,并通过它们的公共成员函数和回调粘合在一起.
上面的层接收网络和调用的命令数据进程basic_connection::send
.并从原始数据中获取原始数据basic_connection
并转换为未处理的上层图层的命令.
我忘了提到的另一个问题是你最终通过堆栈转发了一些接口,例如,顶层仍然需要知道连接状态.
我有一个函数,它产生一种昂贵的对象(包含向量和非固定大小的映射),所以我真的想避免调用copy c'tors.
到目前为止,我刚刚从方法中返回了一个std :: shared_ptr并使用了它,但我觉得它很难看,并且需要使用typedeffing才能真正使用它.
我知道有两件事可以帮助我.首先复制elision,第二个是移动语义.
我的问题是我知道如何正确使用.我的研究告诉我,复制省略完全由编译器完成,并不是st'd的一部分.我真的不想完全依赖于此.
那么我如何确保调用移动分配并确保它已到位以防止编译器复制elision.
ResultSet &&generateResults()
{
//ResultSet a(); :S
ResultSet a;
a.populat(...
//blah blah blah
return a;
}
//else where (where the && assignment operator is overloaded
ResultsSet b = generateResults();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,这是最正确的编码方式吗?如果不是,我怎么能改进它.我很高兴使用C++ 0x only构造.
BTW:我的编译器是gcc 4.6
好的,所以我在c ++ 0x中编写了一个DB包装器,API在C中.
我有prerepared语句,我可以在运行时绑定.
我想绑定并执行1函数调用中的语句到包装器.
我最初的是使用变体模板.但是根据我看到的文档,我还没有找到如何将作为模板类型输入的类型限制为固定集(int,string,double),以及如何能够对这些类型执行基本逻辑.
像(伪代码)
foreach arg in args
if arg1==std::string
bindToString(arg);
else if int...
Run Code Online (Sandbox Code Playgroud)
谢谢
我目前正在用C++ 0x编写一个程序,我对它很新.
我正在设置对象之间的回调并使用lambda来匹配类型(就像boost::bind()
方式一样)
如果我在asio库中调用函数,如:
socket_.async_read_some(buffer(&(pBuf->front()), szBuffer),
[=](const boost::system::error_code &error, size_t byTrans) {
this->doneRead(callBack, pBuf, error, byTrans); });
Run Code Online (Sandbox Code Playgroud)
这编译得很好,并按预期运行,'doneRead'从'async_read_some'回调
所以我在自己的代码中有类似的回调:
client->asyncRead([=](string msg){this->newMsg(msg); });
Run Code Online (Sandbox Code Playgroud)
这只需要一个字符串,而asyncReads原型如下所示
void ClientConnection::asyncRead(void(*callBack)(string))
Run Code Online (Sandbox Code Playgroud)
但我得到这个编译错误:
Server.cpp:在成员函数'void Server :: clientAccepted(std :: shared_ptr,const boost :: system :: error_code&)'中:Server.cpp:31:3:错误:没有用于调用'ClientConnection ::的匹配函数asyncRead(Server :: clientAccepted(std :: shared_ptr,const boost :: system :: error_code&)::)'Server.cpp:31:3:注意:候选者是:ClientConnection.h:16:9:注意:void ClientConnection :: asyncRead(void(*)(std :: string))ClientConnection.h:16:9:注意:来自'Server :: clientAccepted(std :: shared_ptr,const boost :: system ::)的参数1没有已知的转换error_code&)::'to'void(*)(std :: string)'
如何解决这个问题?
嗨,我正在尝试为事件处理程序编写lambda.所以我可以为被调用的方法提供更多信息.
所以我在做:
button.Click+=new EventHandler ((object sender, EventArgs args) =>
{ button_click (i, sender, args); });
Run Code Online (Sandbox Code Playgroud)
哪里:
public void button_click (int i, object sender, EventArgs eventArgs)
Run Code Online (Sandbox Code Playgroud)
好的,所以这个方法在调用方法中起作用,但i
始终是最后已知的值i
,我真的想要将lambda传递给事件的那个值.你是怎样做的?
谢谢
我试图解析一个嘈杂的输入,理想情况下,我将能够看到一个节是否匹配规则,如果它确实得到我需要的数据并丢弃其余的.
我想要的数据如下.
Event: Newstate
Channel: SIP/104-000001bb
ChannelState: 6
ChannelStateDesc: Up
Run Code Online (Sandbox Code Playgroud)
我想确保事件是否为新状态.
我需要频道状态.其余的我不关心(刚才)所以我想忽略它,我希望它是灵活的,并接受重要的东西之间的任何旧垃圾,真的我不想说忽略这一行,而是忽略事件和信道状态结束之间的任何内容,我捕获值.
到目前为止我有:
typedef boost::fusion::vector2<std::string, std::string> vect;
qi::rule<std::string::iterator, vect(), space> rule_ =
lit("Event: ") >> *char_("a-zA-Z") >>
qi::omit[ *char_ ] >>
"ChannelState: " >> *char_("0-9") >>
qi::omit[ *char_ ];
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,这不起作用,当我这样做时,我总是会回复:
vect v;
bool r=qi::parse(it, str.end(), rule_, v);
Run Code Online (Sandbox Code Playgroud)
编辑:Boost版本1.42编译器g ++ 4.4精神0x2020