我一直在尝试使用for_each将字符串向量打印到cout.但是,在制定语句时,我发现std::ostream::operator<<(const std::string &);未定义导致编译器错误.以下代码说明了该问题:
#include <iostream>
#include <string>
int main()
{
std::string message = "Hello World!\n";
// This works
std::cout << message;
// Compiler error
std::cout.operator <<(message);
}
Run Code Online (Sandbox Code Playgroud)
我认为这两个语句看起来应该与编译器完全相同.显然他们不是.那么有什么不同呢?
正如Tomalak和Prasoon表示我需要调用此函数:
std::ostream& operator<<(std::ostream&, const std::string&);
Run Code Online (Sandbox Code Playgroud)
所以以下示例将起作用:
#include <iostream>
#include <string>
int main()
{
std::string message = "Hello World!\n";
operator<<(std::cout, message);
}
Run Code Online (Sandbox Code Playgroud)
至于我原来的目标(用的for_each打印字符串的向量):现在看来似乎是更好地使用std::copy具有std::ostream_iterator如下图所示:如何使用的for_each输出cout的?
我目前正在设计一个看起来大致如下的类层次结构:
struct Protocol
{
// Pass lower-layer protocol as a reference.
Protocol(Protocol & inLLProtocol) :
mLLProtocol(inLLProtocol)
{
}
// A protocol "always" has a LLProtocol.
Protocol & mLLProtocol;
};
struct Layer1Protocol : Protocol
{
// This is the "bottom" protocol, so I pass a fake reference.
Layer1Protocol() : Protocol(*static_cast<Protocol*>(nullptr)) {}
};
Run Code Online (Sandbox Code Playgroud)
*nullptr只要永远不会访问引用,IIRC绑定引用就是安全的.所以我现在有责任以这种方式设计我的Layer1Protocol类来防止这种情况发生.
我喜欢这种方法,因为我确保所有用户协议实例都会引用它们各自的低层协议(Layer1Protocol是例外,但它是核心库的一部分).我认为这比使用指针更可取,因为一旦引入指针,就可以传递空指针,然后可能需要在运行时检查,结果是很多指针检查代码和偶尔的错误.
你认为我的基于参考的方法是可以辩护的吗?或者使用空引用总是一种不好的做法?
为了好玩,我正在为Windows的XUL实现工作.在XUL中,UI元素可以用XML编写,如下所示:
<window width="800" height="600"></window>
Run Code Online (Sandbox Code Playgroud)
我正在考虑一个获取和设置元素属性的系统.它工作得很好,但我不确定钻石继承的使用是否有潜在危险.我在下面发布了一个完整的代码示例:
#include <boost/lexical_cast.hpp>
#include <string>
#include <map>
class Attribute
{
public:
virtual void get(std::string & outValue) = 0;
virtual void set(const std::string & inValue) = 0;
static int String2Int(const std::string & inString)
{
return boost::lexical_cast<int>(inString);
}
static std::string Int2String(int inValue)
{
return boost::lexical_cast<std::string>(inValue);
}
};
class Width : public Attribute
{
public:
Width(){}
virtual void get(std::string & outValue)
{
outValue = Int2String(getWidth());
}
virtual void set(const std::string & inValue)
{
setWidth(String2Int(inValue));
}
virtual int getWidth() const …Run Code Online (Sandbox Code Playgroud) 我想能够使用标准的技术,如性病:: stringstream的连载我的C++类或升压:: lexical_cast的.
例如,如果我有一个Point对象(2,4),那么我想将它序列化为"(2,4)",并且还能够从该字符串构造一个Point对象.
我已经有一些代码,但有一些问题.指向字符串有效,但有时输入未完全从流中读取.Point转换的字符串会导致bad_cast异常.
class Point
{
public:
Point() : mX(0), mY(0) {}
Point(int x, int y) : mX(x), mY(y){}
int x() const { return mX; }
int y() const { return mY; }
private:
int mX, mY;
};
std::istream& operator>>(std::istream& str, Point & outPoint)
{
std::string text;
str >> text; // doesn't always read the entire text
int x(0), y(0);
sscanf(text.c_str(), "(%d, %d)", &x, &y);
outPoint = Point(x, y);
return str;
}
std::ostream& operator<<(std::ostream& str, const Point & …Run Code Online (Sandbox Code Playgroud) 我试图能够单步执行名为 SVNPublisher 的 Hudson 插件的代码。我查看了 SVNPublisher 的代码,使用 Netbeans 打开项目,然后单击“调试主项目”。这会导致 Firefox 窗口打开地址http://localhost:8080,其中显示了 Hudson 主页。单击“新建作业”链接会导致错误页面:
HTTP ERROR: 500
jar:file:/home/francis/svn/svnpublisher/target/work/webapp/WEB-INF/lib/hudson-core-1.319.jar!/lib/hudson/newFromList/form.jelly:43:47: <j:forEach> hudson.scm.SubversionTagAction and hudson.scm.SubversionTagAction$DescriptorImpl disagree on InnerClasses attribute
RequestURI=/newJob
Caused by:
org.apache.commons.jelly.JellyTagException: jar:file:/home/francis/svn/svnpublisher/target/work/webapp/WEB-INF/lib/hudson-core-1.319.jar!/lib/hudson/newFromList/form.jelly:43:47: hudson.scm.SubversionTagAction and hudson.scm.SubversionTagAction$DescriptorImpl disagree on InnerClasses attribute
at org.apache.commons.jelly.impl.TagScript.handleException(TagScript.java:713)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:282)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
...
Run Code Online (Sandbox Code Playgroud)
我对 Hudson 很陌生,对 Java 不是很熟悉,所以我对这个错误的含义几乎一无所知。
任何人都可以帮忙吗?
我想在Vim中创建一个宏或脚本来执行以下操作:
例如:
<html>
<head></head>
<body>
<h1>High Score Server</h1>
<table>
ROWS
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
会成为:
"<html> "
"<head></head> "
"<body> "
"<h1>High Score Server</h1>"
"<table> "
"ROWS "
"</table> "
"</body> "
"</html> ";
Run Code Online (Sandbox Code Playgroud)
我能够通过宏实现这一点,但没有右侧引号的垂直对齐.任何人都可以帮我这个吗?
我希望能够将两个文本流引导到一个具有垂直拆分视图的控制台窗口.理想的解决方案是,如果我可以简单地写入两个不同的std :: ostream对象.
我需要它的原因是比较两个不同版本的程序的输出.我知道有一些简单的解决方法,如重定向到文件和使用diff程序来查看差异.但这并不重要,因为这个项目主要是为了好玩.
但我不确定如何实现这一目标.假设左半部分在右半部分之前写入了20行输出.如何将光标向上移动以写入右半部分?
任何人都可以给我一些关于如何开始的指示?这可以在纯C++中完成,还是需要特定于平台的功能?
在包含定义函数的命令的字符串上使用eval可以正常工作:
$ eval "p4() { echo 4; }"
$ p4
4
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时它不再起作用:
$ echo 'p3() { echo 3; }' | while read line ; do eval "$line"; done
$ p3
-bash: p3: command not found
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
如果公司在线转储其代码库(即在Google Project Hosting上),则允许其使用并链接第三方GPL代码.
当然,管理层不允许公开代码,因为他们认为竞争对手会窃取他们的代码.
但是,根据我的经验,对于没有经验的人来说,庞大的公司代码库是无用的.我记得在我的第一份工作中,至少花了三个月的时间才能让任何人熟悉公司代码.
所以,这似乎是一个简单的修复:只需将它全部转储到网上就可以了.
这会有用吗?或者我在这里缺少任何其他GPL要求吗?
对这篇文章的某些Machiavellian内容表示道歉.
编辑:我不考虑未经管理层许可在线提供任何代码.我只是想知道我是否应该说服他们这样做.
我正在尝试使用 Ruby 设置一个简单的 UDP 客户端和服务器。代码如下所示:
require 'socket.so'
class UDPServer
def initialize(port)
@port = port
end
def start
@socket = UDPSocket.new
@socket.bind(nil, @port) # is nil OK here?
while true
packet = @socket.recvfrom(1024)
puts packet
end
end
end
server = UDPServer.new(4321)
server.start
Run Code Online (Sandbox Code Playgroud)
这是客户:
require 'socket.so'
class UDPClient
def initialize(host, port)
@host = host
@port = port
end
def start
@socket = UDPSocket.open
@socket.connect(@host, @port)
while true
@socket.send("otiro", 0, @host, @port)
sleep 2
end
end
end
client = UDPClient.new("10.10.129.139", 4321) # 10.10.129.139 …Run Code Online (Sandbox Code Playgroud)