标签: initializer

有没有办法在for循环初始化器中定义两种不同类型的变量?

您可以在for循环中定义2个相同类型的变量:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是定义不同类型的变量是违法的:

int main() {
  for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?(我不需要i在循环内使用,只是j.)

如果你完全被黑客攻击并且模糊不清,那对我来说没问题.

在这个人为的例子中,我知道你可以使用double这两个变量.我正在寻找一般答案.

请不要建议移动body之外的任何变量,可能对我不可用,因为一个迭代器必须在循环之后消失并且for语句将被包含在我的foreach宏中:

#define foreach(var, iter, instr) {                  \
    typeof(iter) var##IT = …
Run Code Online (Sandbox Code Playgroud)

c++ scope for-loop initializer variable-declaration

6
推荐指数
7
解决办法
2824
查看次数

初始化shared_ptr成员变量,new vs make_shared?

初始化shared_ptr成员变量时:

// .h
class Customer
{
public:
  Customer();

private:
  std::shared_ptr<OtherClass> something_;
}

// .cpp
Customer():
  something_(new OtherClass())
{
}
Run Code Online (Sandbox Code Playgroud)

Customer():
  something_(std::make_shared<OtherClass>())
{
}
Run Code Online (Sandbox Code Playgroud)

是否允许使用make_shared版本?我似乎总是看到第一个版本,这是首选?

c++ initializer shared-ptr make-shared c++11

6
推荐指数
2
解决办法
1万
查看次数

C:警告:数组初始化程序中的多余元素; 接近初始化'xxx'; 期望'char*',但是类型'int'

尝试用C语言编译程序时,我有一些警告:

13:20: warning: excess elements in array initializer [enabled by default]

13:20: warning: (near initialization for ‘litera’) [enabled by default]

22:18: warning: excess elements in array initializer [enabled by default]

22:18: warning: (near initialization for ‘liczba’) [enabled by default]

43:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

45:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

55:2: warning: format ‘%s’ expects argument of …
Run Code Online (Sandbox Code Playgroud)

c arrays initializer char

6
推荐指数
1
解决办法
6万
查看次数

Java字符数组初始化器

我试图创建一个分隔字符的程序.问题是:

"创建一个char数组并使用数组初始值设定项来初始化数组,字符串为'Hi there'.使用for-statement显示数组的内容.用空格分隔数组中的每个字符".


我制作的节目:

String ini="Hi there";
  char[] array=new  char[ini.length()];


  for(int count=0;count<array.length;count++){

  System.out.print(" "+array[count]);


  }
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

java arrays character initializer

6
推荐指数
1
解决办法
17万
查看次数

如何在Rails中使用Rspec测试配置/初始化程序脚本?

所以我在网上搜索解决方案,但似乎有关于测试在Rails中创建的初始化程序的稀缺信息.

目前,我在config/initializers/retrieve_users.rb文件中编写了一个非常大的API call-n-store.它发出API请求,解析JSON,然后将数据存储为用户.由于非常实质,我还没有找到最简洁的测试方法.由于我需要在运行任何函数之前检索用户,我不相信我可以在其他地方移动此脚本(尽管欢迎其他建议).我有几个问题:

  1. 我是否必须将它包装在函数/类中以测试我的代码?
  2. 我在哪里放我的spec文件?
  3. 我如何格式化RSpec风格?

谢谢!

rspec config ruby-on-rails initializer rspec-rails

6
推荐指数
2
解决办法
6150
查看次数

Python - 初始化程序与构造函数

我听说__init__python 中的函数不是构造函数,它是一个初始化器,实际上__new__函数是构造函数,不同之处在于__init__函数是在创建对象和__new__之前调用之后调用的.我对吗?你能解释的区别更好,我们为什么需要两个__new____init__

python constructor initializer

6
推荐指数
1
解决办法
1万
查看次数

如何实现符合Mappable的NSManagedObject类

我有一个类直接映射JSON实现Mappable(ObjectMapper框架)协议,我试图继承NSManagedObject.

class AbstractModel: NSManagedObject, Mappable {

@NSManaged var uuid: String?
@NSManaged var updatedAt: String?
@NSManaged var createdAt: String?

required init?(_ map: Map) {
    mapping(map)
}

func mapping(map: Map) {
    uuid        <- map["uuid"]
    updatedAt   <- map["updatedAt"]
    createdAt   <- map["createdAt"]

}
}
Run Code Online (Sandbox Code Playgroud)

这个实现的问题是编译器抱怨mapping(map)在超级初始化器之前使用self: AbstractModel.swift:19:9: Use of 'self' in method call 'mapping' before super.init initializes self

不幸的是我以前不能调用super initializer(super.init(entity: NSEntityDescription, insertIntoManagedObjectContext: NSManagedObjectContext?))mapping(map)因为我需要self得到它NSManagedObjectContext.

我该怎么解决这个问题?

initializer nsmanagedobject ios swift

6
推荐指数
1
解决办法
976
查看次数

泛型类型的初始化器不会在swift中继承吗?

这是我的代码:

public class A<T : Any> { 
    public init(n : Int) { 
        print("A")
    } 
} 
public class B : A<Int> {
}
public class C : B {
}
let x = C(n: 123)
Run Code Online (Sandbox Code Playgroud)

这无法编译并大喊这样的错误:

repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers
Run Code Online (Sandbox Code Playgroud)

可以编译以下代码.

public class A { 
    public init(n : Int) { 
        print("A")
    } 
} 
public class B : A {
}
public class C : B {
}
let x = C(n: 123)
Run Code Online (Sandbox Code Playgroud)

是否应该继承要求类型指定的泛型类型的初始值设定项? …

generics initializer swift

6
推荐指数
1
解决办法
519
查看次数

当我运行rails console时,是否会调用Rails初始化程序

我想把我的chargify conf放在初始化器中,但我发现初始化器不会在我的rails c中执行,有没有办法调用我的初始化器,所以我可以在我的控制台中测试?

    Chargify.configure do |c|
      c.api_key   = "keykey"
      c.subdomain = "test-site"
    end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails initializer

6
推荐指数
3
解决办法
3097
查看次数

如何在Objective C中使用多个param调用Swift初始化方法

我在我的Swift文件中有一个Initializer方法,如下所示:

public init(frame: CGRect, type: NVActivityIndicatorType? = nil, color: UIColor? = nil, padding: CGFloat? = nil) {
        self.type = type ?? NVActivityIndicatorView.DEFAULT_TYPE
        self.color = color ?? NVActivityIndicatorView.DEFAULT_COLOR
        self.padding = padding ?? NVActivityIndicatorView.DEFAULT_PADDING
        super.init(frame: frame)
        isHidden = true
}
Run Code Online (Sandbox Code Playgroud)

我想从我的Objective-C文件中调用此方法,但在编译时抛出它的错误.

错误:

/ Users/Desktop/old data/ChatScreenViewController.m:396:92:'NVActivityIndi​​catorView'没有可见的@interface声明选择器'initWithFrame:type:color:padding:'

Obj-C调用代码:

NVActivityIndicatorView *objNVActivityIndicatorView = [[NVActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) type:NVActivityIndicatorTypeLineScalePulseOut color:[UIColor blueColor] padding:10]
Run Code Online (Sandbox Code Playgroud)

我试过的:

Objective-C在我的快速课程中添加

@objc public final class NVActivityIndicatorView: UIView
Run Code Online (Sandbox Code Playgroud)

仍然无法访问上述方法.

我的Swift文件:NVActivityIndi​​catorView.swift

任何想法出了什么问题?

提前致谢!

objective-c initializer swift

6
推荐指数
1
解决办法
2623
查看次数