此代码在cout行崩溃.任何人都可以解释为什么这不起作用?
#include <iostream>
#include <string>
using namespace std;
class myclass {
const string& m_str;
public:
myclass(string s) : m_str(s) {}
const string& getString() const { return m_str; }
};
int main () {
const string str("honey");
myclass mc(str);
cout << mc.getString() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在我下面的代码中,如果找到该项目,我想通知for_each?怎么样?
#include <list>
#include <algorithm>
#include <functional>
using namespace std;
class widget {
public:
widget(int id) : m_id(id) {}
private:
int m_id;
};
class findwidget {
public:
findwidget(widget* p) : m_widget(p) {}
bool operator()(widget* p) const {
return p == m_widget ? true : false;
}
widget* m_widget;
};
list<widget*> m_widgetList;
void push_back(widget* pi){
if(m_widgetList.empty()) {
m_widgetList.push_back(pi);
} else {
if(!std::for_each(m_widgetList.begin(), m_widgetList.end(), findwidget(pi)))
m_widgetList.push_back(pi);
}
}
int main(int argc, char* argv[])
{
widget w1(1);
push_back(&w1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我们有一个像这样的标准枚举:
enum Services {
RequestShower = 611,
RequestBath = 617,
RequestShave = 612,
RequestHaircut = 618
};
Run Code Online (Sandbox Code Playgroud)
但是我的老板说最新的C++标准并不认为enum等同于int,所以建议使用类似这样的类:
class VatelPrivateService {
public:
static const short
RequestShower = 611,
RequestBath = 617,
RequestShave = 612,
RequestHaircut = 618;
static const char* getName(int val);
};
ostream operator<<(ostream& os, VatelPrivateService& service);
Run Code Online (Sandbox Code Playgroud)
好吧,我试着像这样实现:
const char* VatelPrivateService::getName(int id)
{
#define CASE_NM(rq) case rq: return #rq
switch(id)
{
CASE_NM(RequestShower);
CASE_NM(RequestBath);
CASE_NM(RequestShave);
CASE_NM(RequestHaircut);
}
#undef CASE_NM
return "";
}
ostream& operator<<(ostream& os, const VatelPrivateService& service)
{
os …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的输入文件
line 1
line 2
line 3
Run Code Online (Sandbox Code Playgroud)
我要替换的所有行之前有四个空格。
我希望最终结果是:
A[line 1
A[line 2
A[line 3
Run Code Online (Sandbox Code Playgroud)
(很有趣,所以编辑器不想逐行显示)
我尝试过M-x replace-regex ^[]+ -> A\[,但出现错误invalid regex "Unmatched [ or [^"
我尝试过M-x replace-regex ^[]+ -> A[,但得到同样的错误。
更换[是个问题。如何解决这个问题?
我试图使用一个线程库,它具有如下定义的线程启动函数:
int vg_thread_create(VGThreadFunction func,
void *arg,
VGThreadDescriptor *tid,
void *stack,
size_t stack_size,
long priority,
int flags );
Run Code Online (Sandbox Code Playgroud)
VGThreadFunction是以下typedef:
typedef void* (*VGThreadFunction) ( void* )
Run Code Online (Sandbox Code Playgroud)
为了论证,我有一个这样的函数:
void doit() {
cout << "Woohoo printing stuff in new thread\n";
}
Run Code Online (Sandbox Code Playgroud)
这个函数的签名不是void*func(void*) - 我是否被迫使用这样的函数签名?
如果我尝试:
VGThreadFunction testfunc = &doit;
Run Code Online (Sandbox Code Playgroud)
我明白了
错误C2440:'初始化':无法从'void(__ cdecl*)(void)'转换为'VGThreadFunction'
我如何使用vg_thread_create?
编辑
我试过这个:
void* doit(void* donothingvar) {
cout << "printing stuff in new thread\n";
return donothingvar;
}
Run Code Online (Sandbox Code Playgroud)
然后在一个函数中:
VGThreadFunction testfunc = (VGThreadFunction)doit;
int ret = vg_thread_create(testfunc, 0, 0, 0, 0, 0, 0); …Run Code Online (Sandbox Code Playgroud) 我收到此错误:
main.cpp(10) : error C2664: 'lr::codec::codec(protocol_decoder *)'
: cannot convert parameter 1 from 'proto::protocol_decoder *' to 'protocol_decoder *'
Run Code Online (Sandbox Code Playgroud)
如果我删除了proto名称空间的使用,那么这个错误就会消失.我如何解决这个问题并仍然保留proto命名空间的使用.
这是代码:
main.cpp中:
#include "protocol_decoder_a.hpp"
#include "codec.hpp"
int main() {
//factory function create protocol decoder
proto::protocol_decoder* pro = new proto::protocol_decoder_a;
lr::codec cdc(pro);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
codec.hpp:
#ifndef __CODEC_HPP__
#define __CODEC_HPP__
#include <map>
#include <string>
class protocol_decoder;
//log replay namespace
namespace lr {
typedef bool (*c_f)(const char* id, unsigned char* rq, size_t rq_length, unsigned char*& response, size_t& resp_len);
// generic codec interface will …Run Code Online (Sandbox Code Playgroud) 对于原始类型,我可以使用if in:boolean check.但是如果我使用in语法来检查类成员的存在,我会得到一个NameError异常.有没有办法在Python中检查没有异常?或者是尝试除了块之外的唯一方法吗?
这是我的示例代码.
class myclass:
i = 0
def __init__(self, num):
self.i = num
mylist = [1,2,3]
if 7 in mylist:
print "found it"
else:
print "7 not present" #prints 7 not present
x = myclass(3)
print x.i #prints 3
#below line NameError: name 'counter' is not defined
if counter in x:
print "counter in x"
else:
print "No counter in x"
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,一组卡片编译好,但一组会计师没有.为什么?有没有办法解决行为?
struct card {
int value;
int cost;
};
struct accountant
{
const char* name;
double salary;
};
int main() {
card cards[] = { {1, 1}, {2, 2}, {3,3} }; // compiles ok
// error C2065: '“Josh”' : undeclared identifier
// error C2065: '“Kate”' : undeclared identifier
// error C2065: '“Rose”' : undeclared identifier
accountant accts[] = { {“Josh”, 2100.0}, {“Kate”, 2900.0}, {“Rose”,1700.0} };
Run Code Online (Sandbox Code Playgroud)
}
该功能是将文件内容提取到向量中,如下所示:
std::vector<uint8_t> GetFileContents(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (!file.good())
{
return {};
}
file.seekg(0, std::ios::end);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
Run Code Online (Sandbox Code Playgroud)
就行:
std::vector<uint8_t> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
Run Code Online (Sandbox Code Playgroud)
fileData 向量被复制到一个临时向量中以在此处返回?
我需要更改为:
std::vector<uint8_t> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return std::move(fileData);
Run Code Online (Sandbox Code Playgroud)
进行移动并保存矢量复制操作?
支持移动的功能还需要进行其他更改吗?
对于空的情况, std::move({}) 有任何价值吗?
我在angus.js中共享了这样的js代码
var g_colour;
function getcolour() {
return g_colour;
}
function setcolour(colour) {
g_colour = colour;
}
Run Code Online (Sandbox Code Playgroud)
这是由html第1页和第2页访问的,如下所示:
1.HTML:
<html>
<head>
<title>Global javascript example</title>
</head>
<body>
<a href="2.html">Page2</a>
<script src="angus.js"></script>
<form name="frm">
<input type="button" value="Setblue" onclick="setcolour('blue');" />
<input type="button" value="Setyellow" onclick="setcolour('yellow');" />
<input type="button" value="getcolour" onclick="alert(getcolour());" />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
2.HTML:
<html>
<head>
<title>Global javascript example page 2</title>
</head>
<body>
<a href="1.html">Page1</a>
<script src="angus.js"></script>
<form name="frm">
<input type="button" value="Setblue" onclick="setcolour('blue');" />
<input type="button" value="Setyellow" onclick="setcolour('yellow');" />
<input type="button" value="getcolour" onclick="alert(getcolour());" /> …Run Code Online (Sandbox Code Playgroud)