我刚刚编写了一个小单元测试,其中我使用了StringBuilder.
var stringBuilder = new StringBuilder();
stringBuilder.Append("Foo");
Assert.AreEqual(stringBuilder, "Foo");
Run Code Online (Sandbox Code Playgroud)
此测试将失败.
Expected: <Foo>
But was: "Foo"
Run Code Online (Sandbox Code Playgroud)
但是,如果我将Assert更改为
Assert.AreEqual(stringBuilder.ToString(), "Foo");
Run Code Online (Sandbox Code Playgroud)
测试将通过.
那么隐式调用和ToString()方法的显式调用之间有什么区别?或者/这些括号(<>)代表什么?
我有一个包含bool,int和float值的类(加上选定的类型和名称).
using UnityEngine;
using System.Collections;
[System.Serializable]
public class AnimXVariable {
public string name = "variable";
public enum VariableType { Bool, Int, Float };
public VariableType type = VariableType.Bool;
public bool boolVal = false;
public int intVal = 0;
public float floatVal = 0f;
public AnimXVariable() {
type = VariableType.Bool;
}
public AnimXVariable(VariableType newType) {
type = newType;
}
public AnimXVariable(string newName, VariableType newType, bool val) {
name = newName;
type = newType;
boolVal = val;
}
public AnimXVariable(string newName, VariableType …Run Code Online (Sandbox Code Playgroud) 我遇到过一个用例,std::mem_fn它不能做手工包装函数可以做的事情.当包装函数用于不属于方法类的东西,但是可以隐式转换为它的类型时,它会出现:
#include <functional>
struct A
{
};
struct B
{
B(A); // implicit conversion from A to B
void foo() const;
};
auto foo1 = std::mem_fn(&B::foo); // std::mem_fn
void foo2(const B& b) { b.foo(); } // hand-rolled wrapper
int main()
{
A a;
foo1(a); // doesn't work
foo2(a); // works fine
}
Run Code Online (Sandbox Code Playgroud)
调用foo1的编译器错误如下(使用GCC 4.8):
In file included from test.cpp:1:0:
functional: In instantiation of '_Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)const>::_M_call(_Tp&, const volatile void*, _ArgTypes ...) const [with _Tp = A; _Res …Run Code Online (Sandbox Code Playgroud) 我有这个奇怪的代码(永远不会在生产代码中使用)产生奇怪的编译错误,我想知道更多关于这种行为,
string MyMethod(string s)
{
return s == null ? null : null;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
无法确定条件表达式的类型,因为
null和之间没有隐式转换null
什么解释了这种行为?
我有一个带有选项参数的case类,让我们说:
case class Student(id: Option[Int], name: String)
Run Code Online (Sandbox Code Playgroud)
要获得Student实例,不仅我可以使用Student(Some(1), "anderson"),我还希望这个表单是一种有效的方式Student(2,"Sarah")
我想我必须创建一个Int => Option[Int]并把它放在某个地方.那么最好的方法是什么?
更新
正如评论中提到的,override apply方法将阻止调用它Student.apply _
我做了一个测验,其中用户必须输入一个数字(例如4),TextBox然后程序将检查输入的数字是否正确.不幸的是,我在使用这部分代码时遇到了一些麻烦.
所以目前我有这个代码:
if(textbox1.Text=4)
Run Code Online (Sandbox Code Playgroud)
但是4带有错误消息的下划线:
不能隐式地将类型'int'转换为'string'.
我可以麻烦大家帮我找出我的代码有什么问题吗?非常感谢!!
我在scala中创建伴随对象并尝试object在class不导入的情况下使用implictis函数.但无论何时,尝试编译代码我都会收到错误:type mismatch;似乎无法自动导入implictis.以下是我的代码:
object ImplicitTest5 {
implicit def dollarToRupa(dollar: Dollar): Rupa = {
println("calling .... dollarToEuro")
Rupa(dollar.value)
}
implicit def dollarToEuro(dollar: Dollar): Euro = {
println("calling .... dollarToEuro")
Euro(dollar.value)
}
}
case class Dollar(value: Double)
case class Euro(value: Double)
case class Rupa(value: Double)
class ImplicitTest5 {
private val value = "String"
def conversion = {
val euro: Euro = Dollar(3.1)
println(s" ----- $euro")
}
}
Run Code Online (Sandbox Code Playgroud)
当我import ImplicitTest5._在我的班级使用时,它将编译并运行正常.根据Scala中的Programming,Page:478它将正常工作,并且不需要定义导入. …
scala compiler-errors implicit implicit-conversion companion-object
首先,我使用的用户定义的转换功能,以隐式转换的目的是int,然后将其插入至cout与<<运营商.程序编译成功并打印为"0".
#include <iostream>
using namespace std;
class T {
public:
operator int () {
return 0;
}
};
int main()
{
T a;
cout << a << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后,我尝试做同样的事情,除了对象被转换为std::string.该程序出现编译错误.
#include <iostream>
#include <string>
using namespace std;
class T {
public:
operator string () {
return "";
}
};
int main()
{
T a;
cout << a << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么在第二种情况下不会发生隐式转换.
我已经定义了一个隐式类,它为foo所有实例提供了一个方法Double.奇怪的是,现在也可以在Float实例上调用此方法,这由scalac2.12.5(使用-Xscript Foo)接受的以下示例显示:
implicit class DoubleOps(value: Double) {
def foo: Double = value
}
val x: Float = 1f
val y = x.foo
Run Code Online (Sandbox Code Playgroud)
如果我尝试对我自己的类型执行相同操作,则分别替换Float和Double使用MyFloat和实例不可用.MyDoublefooMyFloat
trait MyDouble
object MyDouble {
implicit class MyFloatAsMyDouble(value: MyFloat) extends MyDouble
}
trait MyFloat
implicit class MyDoubleOps(value: MyDouble) {
def foo: MyDouble = value
}
val x: MyFloat = new MyFloat { }
val y = x.foo
Run Code Online (Sandbox Code Playgroud)
$ …Run Code Online (Sandbox Code Playgroud) 如标题所示,该问题是我在密码功能的特定部分执行程序时出错。实际上,这是一个基本的密码功能,在Turbo C ++中正常工作,但是在Visual C ++中,此错误到来
void user::password()
{
char any_key, ch;
string pass;
system("CLS");
cout << "\n\n\n\n\n\n\n\n\t\t\t\t*****************\n\t\t\t\t*ENTER
PASSWORD:*\n\t\t\t\t*****************\n\t\t\t\t";
start:
getline(cin,pass);
if (strcmp(pass, "sha") == 0) //this is where the error is!*
{
cout << "\n\n\t\t\t\t ACCESS GRANTED!!";
cout << "\n\t\t\t PRESS ANY KEY TO REDIRECT TO HOME PAGE";
cin >> any_key;
}
else
{
cout << "\n\t\t\t\t ACCESS DENIED :(,RETRY AGAIN!!\n\t\t\t\t";
goto start;
}
system("CLS");
}
Run Code Online (Sandbox Code Playgroud) c# ×4
c++ ×3
scala ×3
c++11 ×1
c-strings ×1
if-statement ×1
implicit ×1
mem-fun ×1
numbers ×1
string ×1
textbox ×1
tostring ×1
unit-testing ×1
visual-c++ ×1