我收到以下错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'Munchkin.Model.PlayerProfiles.Profile'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
Profile currentProfile;
public Profile ActiveProfile()
{
currentProfile = new Profile();
return currentProfile =
(from profiles in xmlDoc.Element("PlayerPofiles").Element("Online").Elements("Player")
where (string)profiles.Element("Active") == "True"
select new Profile
{
Name = (string)profiles.Element("Name"),
Sex = (string)profiles.Element("Sex"),
Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
Created = (DateTime)profiles.Element("Created"),
Birthday = (string)profiles.Element("Birthday"),
Wins = (string)profiles.Element("Ratio").Element("Win"),
Losses = (string)profiles.Element("Ratio").Element("Loss"),
Abandoned = (string)profiles.Element("Ratio").Element("Abandoned")
});
}
Run Code Online (Sandbox Code Playgroud) 为什么以下重载函数调用不明确?编译错误:
调用重载'test(long int)'是模棱两可的,候选者是:void test(A)| 无效测试(B)|
代码:
class A
{
public:
A(int){}
A(){}
};
class B: public A
{
public:
B(long){}
B(){}
};
void test(A a)
{
}
void test(B b)
{
}
void main()
{
test(0L);
return;
}
Run Code Online (Sandbox Code Playgroud) 这段代码:
class foo
{
int x;
public:
foo(int x) : x(x) { }
int get() const { return x; }
//...
};
class bar
{
int x;
public:
bar(const foo& x) : x(x.get()) { }
int get() const { return x; }
bar& operator =(const foo& rhs) { x = rhs.get(); return *this; }
//...
};
void func()
{
foo f = 3;
bar b = 3;
b = 7;
//...
}
Run Code Online (Sandbox Code Playgroud)
在线上出错bar b = 3(g ++ 4.7.1 …
c++ constructor implicit-conversion copy-initialization c++11
我正在定义一些Scala implicits,以便更轻松地使用特定的不可更改的Java类集.下面的Scala代码是一个简单的例子,显然看起来很疯狂,在现实世界中,我试图从Monkey,Tree&Duck中隐含地获取特定资源(而不是数字时代),以便在各种方法中使用purchaseCandles():
// actually 3 Java classes I can not change:
case class Monkey(bananas: Int)
case class Tree(rings: Int)
case class Duck(quacks: Seq[String])
// implicits I created to make my life easier...
implicit def monkey2Age(monkey: Monkey): Int = monkey.bananas / 1000
implicit def tree2Age(tree: Tree): Int = tree.rings
implicit def duck2Age(duck: Duck): Int = duck.quacks.size / 100000
// one of several helper methods that I would like to define only once,
// only useful if they can use …Run Code Online (Sandbox Code Playgroud) 你能告诉我SQL Server中的隐式转换和显式转换之间的区别吗?
我用谷歌搜索了这个,但我无法得到它.
有什么办法可以实现一个隐含的从转换string到bool使用C#?
例如,我有str值为Y的字符串,当我尝试转换(强制转换)时boolean,必须返回true.
.net c# extension-methods type-conversion implicit-conversion
我想定义一个从Iterator[T]我定义的类的隐式转换:ProactiveIterator[A].
问题不在于如何做到,而是如何正确地做到这一点,即在何处放置方法,以使其尽可能透明和不引人注目.理想情况下它应该是从隐式转换String到StringOpsin scala.Predef如果转换是从库中的类转换到其他类,那么它可以在该类中定义,但是AFAIK在这里是不可能的.
到目前为止,我已经考虑添加一个包含这些转换的对象,类似于JavaConversions,但是更好的选项可能是可能的.
我目前正在阅读"The C++ Programming Language"一书.以下是相关代码
class complex {
public:
complex(double r, double i): re{r}, im{i} {}
complex(double r): complex{r,0} {}
complex(): complex{0,0} {}
complex& operator-=(complex z) {
this->re -= z.re;
this->im -= z.im;
return *this;
}
private:
double re,im;
};
inline complex operator-(complex a, complex b) {
return a -= b;
}
inline complex operator-(complex z) {
return{ 0, 0 } - z;
}
Run Code Online (Sandbox Code Playgroud)
一元operator-给出错误 -
语法错误:缺少';' 在' - '之前
但是,编译器认为以下两种变体都是正确的
inline complex operator-(complex z) {
return 0 - …Run Code Online (Sandbox Code Playgroud) 有没有办法在Ruby/Rails中删除/停用/猴子补丁隐式转换?
我厌倦了像这样的代码生成的错误:
t = Time.now
t + 3600 == t + 3600.seconds
Run Code Online (Sandbox Code Playgroud)
但
dt = DateTime.now
dt + 3600 == dt + 3600.days #(why it's days here and not seconds as with Time ?)
Run Code Online (Sandbox Code Playgroud)
根据添加(或减法)中的日期类型,结果是不同的,因为在时间的情况下,Integer被隐式转换为几秒,而在DateTime的情况下则是天.
编辑:
好.我在这里有一些很棒的答案.
也许更好的方法来"纠正"这种非常不一致的Ruby行为,如果有人试图将Integer/Fixnum添加到日期/时间,则会引发异常.只接受持续时间,你不这么认为吗?
有没有办法做到这一点 ?
我正在尝试在Scala中重现C#语法:
C#代码:
val myValue = myObject ?? new MyClass();
Run Code Online (Sandbox Code Playgroud)
基本上'myValue'将包含'myObject'(如果它不为null),或者一个新的'MyClass'实例(如果它为null).
这是我试图运行的Scala代码:
case class ShortCond(original: Any){
def ??(fallback : Any) {
if (original == null)
return fallback
return original
}
}
implicit def any2ShortCond(original: Any) = ShortCond(original)
val myString = null
val value = myString ?? "myString is null"
Run Code Online (Sandbox Code Playgroud)
它在myString不为null时有效,但在myString为null时失败,它返回:
error: value ?? is not a member of Null
val value = myString ?? "myString is null"
Run Code Online (Sandbox Code Playgroud)
我理解错误,但有没有办法在Scala中实现这一点,或者它是不可能的?
谢谢
c++ ×3
scala ×3
c# ×2
c++11 ×2
.net ×1
constructor ×1
linq ×1
methodology ×1
ruby ×1
sql ×1
sql-server ×1