我有一个代码,其代码如下:
Socket echoSocket = null;
... something ...
echoSocket = new Socket("taranis", 7);
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我们想要第一线.我知道Java无法从其值中定义变量的类型.这就是为什么首先我们需要告诉echoSocket是具有Socket类型的变量(第一行),而且我们认为echoSocket具有一定的值(类Socket的对象).
但我不明白为什么我们需要分配两次值?为什么我们要说echoSocket等于null?
我正在与一个图书馆合作,我必须与工会合作.具体来说,我正在使用SDL和SDL_Event联合.我需要复制SDL_Events,我找不到关于使用联合重载赋值运算符的好信息.
如果我可以重载赋值运算符,我应该手动筛选联合成员并复制相关成员,或者我可以简单地来一些成员(这对我来说似乎很危险),或者只是使用memcpy()(这看似简单快速,但有点危险)?
如果我不能超载运营商那么我最好的选择是什么呢?我想我可以制作新副本并传递一堆指针,但在这种情况下我宁愿不这样做.
欢迎任何想法!
编辑:按要求错误消息,顺便说一下,我想我已经学到了一些东西......
physworld.cpp:325: error: no match for ‘operator=’ in ‘CurrentEvent = ((physworld*)this)->physworld::SDL_UserInputEvents.std::queue<_Tp, _Sequence>::pop [with _Tp = SDL_Event, _Sequence = std::deque<SDL_Event, std::allocator<SDL_Event> >]()’ /usr/include/SDL/SDL_events.h:220: note: candidates are: SDL_Event& SDL_Event::operator=(const SDL_Event&)
编辑2:这太愚蠢......我认为Deqeues pop()成员返回了删除的项目.我认为代码非常简单,不能直接成为我的代码,但事实证明这是错误的.
我的代码看起来像:
for(SDL_Event CurrentEvent; !DequeueOfSDLEvents.empty(); CurrentEvent = DequeueOfSDLEvents.pop() )
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
所以,如果没有别的,我将学习更仔细地看看我最近没有用过的容器的成员函数.感谢您解释默认情况下的分配工作,否则需要更长的时间才能找到它.
我对Java非常熟悉,这是允许的.然而,看起来它不适用于C++.我在尝试分配valuesToGrab = updatingValues;时收到"无效的数组赋值".
//these are class attributes
int updatingValues[361] = {0};
int valuesToGrab[361] = {0};
//this is part of a function that is causing an error.
for (unsigned int i=0; i < 10; i++) {
//this fills values with 361 ints, and num_values gets set to 361.
sick_lms.GetSickScan(values,num_values);
//values has 361 ints, but a size of 2882, so I copy all the ints to an array
//of size 361 to "trim" the array.
for(int z = 0; z < num_values; …Run Code Online (Sandbox Code Playgroud) 我的程序中有多个赋值语句,如下所示,query.constraints.size()它应该返回13(constraints是一个数组,它返回它的大小)
int num,size = query.constraints.size();
Run Code Online (Sandbox Code Playgroud)
当我这样做size时,按预期变为13但num由于某种原因变为9790272.
当我单独进行下面的操作时,一切都很好,两者都是预期的13
int size = query.constraints.size();
int num = query.constraints.size();
Run Code Online (Sandbox Code Playgroud)
为什么我的多重赋值会产生奇怪的奇怪值?
我最近在做一个分数类,在重载运算符时没有得到预期的结果,我不确定为什么。希望有人可以帮忙。我试图只包含相关代码。
const fraction fraction::operator* (fraction frac)
{
return fraction(frac.numerator * numerator, frac.denominator * denominator);
}
const fraction fraction::operator* (int num)
{
return fraction(numerator*num, denominator);
}
fraction& fraction::operator= (const fraction &rightSide)
{
return *this;
}
Run Code Online (Sandbox Code Playgroud)
这些操作是我发现可以正常运行的那些操作(其中frac#是分数对象):
frac1 = frac2;
frac3 = frac4 * 2;
frac5 = frac6 * frac7;
Run Code Online (Sandbox Code Playgroud)
上面的操作按预期方式工作,但是以下操作与初始化时一样保留frac8:
fraction frac8(4, 5); // Initializes a fraction, setting numerator = 4, denominator = 5
frac8 = frac8 * 3; // This doesn't quite work, leaving frac8 with the original numerator/denominator
Run Code Online (Sandbox Code Playgroud)
我只是不太明白为什么frac3 …
我是python的新手,我使用全局变量来轻松控制我的设计,尤其是调试.
这是我的代码:
hidNow = -1
def loginFromSql(br, n=0):
global hidNow
print hidNow
hidNow = 5566
print hidNow
print(hidNow)
Run Code Online (Sandbox Code Playgroud)
我用"from myModule import*"直接运行python
在调用函数之前,打印hidNow得到-1的结果,但它仍然是-1甚至函数被调用....
它是python中的一个bug吗?我使用的是python 2.7.5
谢谢!
我无法理解如何使用该map函数分配哈希.
为什么
my %a = map {$_=>1 if $_>=2} (1..4);
给我一个Odd number of elements in hash assignment错误的同时
my %a = map {$_=>1 if $_>2} (1..4);
给我
$VAR1 = {
'' => '',
'4' => 1,
'3' => 1
};
Run Code Online (Sandbox Code Playgroud)
为什么哈希中只有一个空字符串?如果我分配给一个数组
my @a = map {$_ if $_>2} (1..4);
$VAR1 = [
'',
'',
3,
4
];
Run Code Online (Sandbox Code Playgroud)
我得到两个空字符串,这对我来说更有意义.
如果不满足条件,是否有可能不返回空字符串?
过去几个月我一直在做一个大项目.现在我终于完成了那个项目.但在我的复制赋值操作符中,我有内存泄漏.Valgrind表明它是泄漏的data_的原始值.
这是我的复制赋值操作符代码.
Value& Value::operator=(const Value& r)
{
data_ = copy(r.data_);
delete data_;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
有谁可以帮我解决这个问题?我真的很感激.
我想在声明中为类分配一个值,所以我创建了这个基本类:
class A
{
public:
A &operator=(int)
{
return (*this);
}
};
Run Code Online (Sandbox Code Playgroud)
并用这个main编译它:
int main(void)
{
A x = 1;
}
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨此错误消息:
no viable conversion from 'int' to 'A'
A x = 1;
^ ~
Run Code Online (Sandbox Code Playgroud)
但是当我用这个main编译时:
int main(void)
{
A x;
x = 1;
}
Run Code Online (Sandbox Code Playgroud)
一切顺利编译
为什么我的第一个主要不编译,如何更改类A以便编译?
在这里,我学到了一种有用的Haskell语法,可以在不完全重写记录的情况下修改记录的元素:
oldrecord { somefield = newvalue }
Run Code Online (Sandbox Code Playgroud)
元组是否有类似的可能?
type ABigTuple = (Int, Int, Double, Int, String)
aBigTuple :: ABigTuple
aBigTuple = (5, 6, 3.2, 10, "asdf")
anotherBigTuple = -- replace the 3rd elt of the prev tuple with 5.5 i/o 3.2
Run Code Online (Sandbox Code Playgroud)
这是否有可能类似于记录,还是我必须重写整个元组?