我有这个功能:
template<typename T>
void Inventory::insertItem(std::vector<T>& v, const T& x)
{
std::vector<T>::iterator it; // doesn't compile
for(it=v.begin(); it<v.end(); ++it)
{
if(x <= *it) // if the insertee is alphabetically less than this index
{
v.insert(it, x);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和g ++给出了这些错误:
src/Item.hpp: In member function ‘void
yarl::item::Inventory::insertItem(std::vector<T, std::allocator<_CharT> >&, const T&)’:
src/Item.hpp:186: error: expected ‘;’ before ‘it’
src/Item.hpp:187: error: ‘it’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
它必须是简单的东西,但在盯着它十分钟后我找不到任何错误.别人看到了吗?
我试图在一个模块中挑选一个类的实例,并在另一个模块中解开它.
这是我腌制的地方:
import cPickle
def pickleObject():
object = Foo()
savefile = open('path/to/file', 'w')
cPickle.dump(object, savefile, cPickle.HIGHEST_PROTOCOL)
class Foo(object):
(...)
Run Code Online (Sandbox Code Playgroud)
这是我尝试去开发的地方:
savefile = open('path/to/file', 'r')
object = cPickle.load(savefile)
Run Code Online (Sandbox Code Playgroud)
在第二行,我明白了 AttributeError: 'module' object has no attribute 'Foo'
有谁看到我做错了什么?
说我有以下代码:
import java.lang.InterruptedException;
import javax.swing.SwingWorker;
public class Test
{
private JDialog window;
public Test
{
// instantiate window
}
private class Task extends SwingWorker<Void, Void>
{
public Void doInBackground()
{
try { Thread.currentThread().sleep(5000); }
catch(InterruptedException e) {}
return null;
}
}
public void doTask()
{
Task task = new Task();
task.execute();
}
protected void process()
{
// update various GUI components here
}
public static void main(String args[])
{
Test t = new Test();
t.doTask();
System.out.println("done");
}
}
Run Code Online (Sandbox Code Playgroud)
我需要等到t.doTask() …
我有以下代码片段:
std::vector< boost::shared_ptr<Foo> >::iterator it;
it = returnsAnIterator();
// often, it will point to a shared_ptr that is NULL, and I want to test for that
if(*it)
{
// do stuff
}
else // do other stuff
Run Code Online (Sandbox Code Playgroud)
我测试正确吗?boost文档说shared_ptr可以隐式转换为bool,但是当我运行这段代码时会出现段错误:
Program received signal SIGSEGV, Segmentation fault.
0x0806c252 in boost::shared_ptr<Foo>::operator Foo*
boost::shared_ptr<Foo>::* (this=0x0)
at /usr/local/bin/boost_1_43_0/boost/smart_ptr/detail/operator_bool.hpp:47
47 return px == 0? 0: &this_type::px;
Run Code Online (Sandbox Code Playgroud) 所以我的程序运行正常.编译,链接,运行,工作.然后,我决定在我的一个文件中添加一个简单的函数,如下所示:
#ifndef UTILITY_HPP
#define UTILITY_HPP
/* #includes here. There's no circular include, I've checked. */
namespace yarl
{
namespace utility
{
(several function declarations and definitions)
bool isVowel(const char c)
{
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return true;
else return false;
}
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
该函数定义是我对代码所做的唯一更改.其他一切与现在完全一样.什么都没有叫它.我编译,它无法链接,g ++为#includes这个文件的每个文件提供了这些错误之一:
./obj/Feature.o: In function `yarl::utility::isVowel(char)':
/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src/Utility.hpp:130: multiple definition of `yarl::utility::isVowel(char)'
./obj/Events.o:/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src /Utility.hpp:130: first defined here
./obj/GameData.o: …
Run Code Online (Sandbox Code Playgroud) 我有这个for循环:
std::vector<itemPtr>::iterator it;
for(it=items.begin(); it!=items.end(); ++it)
{
investigators.addToLeaderInventory(*it);
}
Run Code Online (Sandbox Code Playgroud)
我想把它转换成这样的东西:
std::for_each(items.begin(), items.end(), investigators.addToLeaderInventory);
Run Code Online (Sandbox Code Playgroud)
但是,该行无法编译.g ++告诉我这个:
error: no matching function for call to
‘for_each(__gnu_cxx::__normal_iterator<std::tr1::shared_ptr<yarl::item::Item>*,
std::vector<std::tr1::shared_ptr<yarl::item::Item>,
std::allocator<std::tr1::shared_ptr<yarl::item::Item> > > >,
__gnu_cxx::__normal_iterator<std::tr1::shared_ptr<yarl::item::Item>*,
std::vector<std::tr1::shared_ptr<yarl::item::Item>,
std::allocator<std::tr1::shared_ptr<yarl::item::Item> > > >, <unresolved overloaded
function type>)’
/usr/include/c++/4.4/bits/stl_algo.h:4194: note: candidates are: _Funct
std::for_each(_IIter, _IIter, _Funct) [with _IIter =
__gnu_cxx::__normal_iterator<std::tr1::shared_ptr<yarl::item::Item>*,
std::vector<std::tr1::shared_ptr<yarl::item::Item>,
std::allocator<std::tr1::shared_ptr<yarl::item::Item> > > >, _Funct = void
(yarl::party::Party::*)(yarl::itemPtr)]
Run Code Online (Sandbox Code Playgroud)
至少可以说,难以阅读.我想解决方案非常简单,但我无法弄清楚g ++在抱怨什么.签名investigators.addToLeaderInventory()
是这样的:
void ClassName::addToLeaderInventory(itemPtr item);
Run Code Online (Sandbox Code Playgroud)
哪个应该合作for_each
,不应该吗?我应该改变什么?
我的程序有这个功能:
vector<itemPtr> Level::getItemsAt(const Point& pt)
{
vector<itemPtr> vect(items.size());
// copy all items at pt's position to vect
remove_copy_if(items.begin(), items.end(), vect.begin(),
boost::bind(matchesPosition<itemPtr>, _1, pt));
// update LevelMap and return
map.setHasItem(pt, false);
return vect;
}
Run Code Online (Sandbox Code Playgroud)
这编译得很好(我使用g ++,我的gcc版本是4:4.4.1-1ubuntu2),但是当我运行程序时,它会跳过return语句.
我介绍了gdb,在前一行设置断点,并得到了这个:
Breakpoint 1, yarl::level::Level::getItemsAt (this=0x80d4d58, pt=...)
at src/Level.cpp:519
519 map.setHasItem(pt, false);
(gdb) next
521 }
(gdb)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过多次重新编译,事先擦除了可执行文件和所有目标文件,但它仍然可以.
奇怪的是,如果我注释掉return语句并尝试编译,它只会给出warning: no return statement in function returning non-void
.我本以为没有在函数中提供return语句返回一些编译器错误,但我猜不是.
我意识到这不是很多,但有没有人知道为什么会这样?要检查什么?在这一点上,我甚至不知道从哪里开始寻找.
编辑:澄清,我正在编译-O0
.
根据tjm,即使使用-O0
编译器标志,我的gcc版本仍然会使用RVO ,所以这就是问题所在.伙计们,感谢您的帮助.
我有这段代码:
cerr << client->inventory.getMisc().front()->getName() << endl;
vector<itemPtr>::iterator it;
it = client->inventory.getMisc().begin();
cerr << (*it)->getName() << endl;
Run Code Online (Sandbox Code Playgroud)
让我解释一下:
client
是一个tr1::shared_ptr
指向一个对象,该对象具有一个名为的成员inventory
,该vector<itemPtr>
成员可以访问私有成员getMisc()
.itemPtr
是一个typedef tr1::shared_ptr<Item>
,并getName()
返回一个私有std::string
成员Item
.
从本质上讲,client->inventory.getMisc()
归结为a std::vector
,我试图获得第一个元素的迭代器.
问题是第四行是segfaults.显然,迭代器或它指向的shared_ptr都是无效的.我使用第一个cerr语句来测试向量本身是否有效,并且它应该打印,所以我认为它是.
有什么我做错了吗?或者,你们会做什么来调试这个?
我有一个函数加载一个精灵表,找到一个精灵块,然后将每个精灵放入一个列表.在将精灵附加到列表中之前,它会将其blit到屏幕上.一旦它完成加载精灵,它将遍历列表,随着它的进行blitting每个精灵.两组blits 应该是相同的,但是从列表中删除第一个sprite,并且重复最后一个sprite.两组blits看起来像这样:
每个精灵按照它附加到列表的顺序进行blitting,从左到右,从上到下,所以第一个精灵是左上角,最后一个是右下角.
这是加载精灵的函数:
def assembleSprites(name, screen):
"""Given a character name, this function will return a list of all that
character's sprites. This is used to populate the global variable spriteSets"""
spriteSize = (35, 35)
spritesheet = pygame.image.load("./images/patchconsprites.png")
sprites = []
start = charCoords[name]
char = list(start)
image = pygame.Surface((35,35))
# load each sprite and blit them as they're added to the list
for y in range(5):
char[0] = start[0]
for x in range(9):
rect = (char[0], char[1], char[0]+spriteSize[0], …
Run Code Online (Sandbox Code Playgroud) 我正在学习D,我有一个简单的程序,它逐行读取文本文件,将每一行分成不同的单词,并将整个内容打印到stdout.
import std.stdio;
import std.string;
void main(string args[])
{
char[][][] lines;
auto input = File(args[1], "r");
foreach(line; input.byLine())
{
auto words = split(strip(line));
lines ~= words;
}
foreach(line; lines)
{
writeln(line);
}
}
Run Code Online (Sandbox Code Playgroud)
创作words
作品的代码.如果我只是writeln
在每次分配时调用单词,我会得到我想要的输出.但是,如果我添加words
到lines
输出lines
,然后奇怪的事情发生. lines
在源文件中有每行的条目,但每行都是最后一行读取的损坏版本.例如,如果文件的最后一行如下所示:
END START * End of routine
Run Code Online (Sandbox Code Playgroud)
我得到的输出看起来像这样:
[ , END, ST, *, End , f rout, ne, , , e other]
[ , END, ST, *, End of, rout, ne, , , e othe] …
Run Code Online (Sandbox Code Playgroud) 我最近在以下函数中修复了一个错误,答案让我感到惊讶.我有以下功能(在我发现错误之前编写):
void Level::getItemsAt(vector<item::Item>& vect, const Point& pt)
{
vector<itemPtr>::iterator it; // itemPtr is a typedef for a std::tr1::shared_ptr<item::Item>
for(it=items.begin(); it!=items.end(); ++it)
{
if((*it)->getPosition() == pt)
{
item::Item item(**it);
items.erase(it);
vect.push_back(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
此函数查找Item
"项目"向量中具有特定位置的所有对象,将其从"项目"中删除,并将它们放在"vect"中.之后,名为的函数putItemsAt
执行相反的操作,并将项添加到"项".第一次通过,getItemsAt
工作正常.putItemsAt
但是,在调用之后,for循环getItemsAt
将在'items'结束时运行.'它'将指向无效Item
指针和段错误getPosition()
.在预感中,我改变it!=items.end()
了it<items.end()
,并且它起作用了.谁能告诉我为什么?环顾SO表明它可能涉及erase
使迭代器无效,但它仍然没有意义,为什么它会第一次工作.
我也很好奇,因为我计划将"项目"从向量更改为列表,因为列表的擦除效率更高.我知道我必须使用!=
列表,因为它没有<
运算符.我会使用列表遇到同样的问题吗?
我正在经历 2022 年代码降临,目前正在第 11 天第 2 部分。
我有这个解决方案:
(ns advent-of-code-2022.day11
(:require [taoensso.timbre :as log]))
(declare give-monkey-item)
(defrecord Monkey [items operation throw-item num-inspections])
(defn create-monkey [items operation divisor monkey-true monkey-false]
(->Monkey
items
operation
(fn [item monkeys]
(if (= (mod item divisor) 0)
(give-monkey-item item monkeys monkey-true)
(give-monkey-item item monkeys monkey-false)))
0))
(defn give-monkey-item [item monkeys catcher]
(update-in monkeys [catcher :items] #(conj % item)))
(defn throw-items [monkeys current-monkey]
(reduce (fn [monkeys item]
(-> item
(biginteger)
((.operation current-monkey))
((.-throw_item current-monkey) monkeys))) …
Run Code Online (Sandbox Code Playgroud) 为了节省我的代码中的一些空间,我做了这个typedef:
typedef clients.members.at(selectedTab) currentMember;
Run Code Online (Sandbox Code Playgroud)
但是,g ++给了我这个错误:
error: expected initializer before '.' token
Run Code Online (Sandbox Code Playgroud)
我认为我误用了typedef,因为它clients.members.at(selectedTab)
是一个函数调用而不是一个类型.有没有办法做我想在这里做的事情?