标签: implicit-conversion

需要可转换为“int”类型的对象

这是我的代码:

\n\n
namespace Cinemaseats\n\n {public partial class MainForm : Form\n    {\n    private const int numOfSeats = 60;\n    private int numOfReservedSeats = 0;\n    Seat currentSeat = new Seat();\n    public MainForm()\n    {\n        InitializeComponent();\n        InitializeGUI();\n    }\n\n    private void InitializeGUI()\n    {\n        radioButton1.Checked = true;\n        namn.Text = string.Empty;\n        pris.Text = string.Empty;\n    }\n    private void button1_Click(object sender, EventArgs e)\n    {\n        **int seatNr = ReadAndValidateSeatNr();**\n\n        if (seatNr < 0)\n        {\n            MessageBox.Show("V\xc3\xa4lj ett f\xc3\xb6rem\xc3\xa5l fr\xc3\xa5n listan");\n            return;\n        }\n\n        if (radioButton2.Checked)\n            ReserveSeat(seatNr);\n        else\n            CancelSeat(seatNr);\n\n        UpdateGUI(seatNr);\n    }\n\n    public int GetnumOfSeats()\n …
Run Code Online (Sandbox Code Playgroud)

c# string int implicit-conversion

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

如果浮点常量隐式转换为int类型,何时会溢出

我有两个代码段,并且都产生不同的结果。我正在使用TDM-GCC 4.9.2编译器,而我的编译器是32位版本

(int的大小为4个字节,float的最小值为-3.4e38)

代码1:

int x;
x=2.999999999999999; // 15 '9s' after decimal point
printf("%d",x);
Run Code Online (Sandbox Code Playgroud)

输出:

2

代码2:

int x;
x=2.9999999999999999; // 16 '9s' after decimal point
printf("%d",x);
Run Code Online (Sandbox Code Playgroud)

输出:

3

为什么在这些情况下隐式转换会有所不同?

是否由于指定的Real常量中的某些溢出而引起的,怎么办?

c implicit-conversion

0
推荐指数
1
解决办法
37
查看次数

我不明白的 C++ 行为

我和我的朋友们正在玩 C++ 语言。在这样做的过程中,我们遇到了一些我们无法理解的事情。

这是代码:

#include <vector>
#include <iostream>

void print(std::vector<char> const &input)
{
    std::cout << input.size();
    for (int i = 0; i < input.size(); i++)
    {
        std::cout << input.at(i) << " - ";
    }

}

int main()
{
    char cha = 'A';
    char chb = 'B';
    char * pcha = &cha;
    char * pchb = &chb;
    try
    {
        std::vector<char> a = {pcha, pchb};
        //std::vector<char> a = {pchb, pcha};
        print(a);

    }
    catch(std::exception e)
    {
        std::cout << e.what();
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码的输出:

一种 …

c++ vector type-conversion implicit-conversion c++17

0
推荐指数
1
解决办法
91
查看次数

分数表达式的 n 项之和为整数时应为浮点数

在下面的代码中,为什么除非我放入term = 1.0/n而不是 when ,否则我不会得到有效的结果term = 1/n。我已将 term 声明为浮动,这还不够吗?

#include <stdio.h>

int main()
{    
     float sum = 0, term;
     int n, i;
     
     printf("enter the value of n:\n");
     scanf("%d", &n);
     term = 1.0 / n;
     for(i = 1; i <= n; i++)
     {  
        sum = term + sum; 
       
      }
     printf("Sum = %.3f\n", sum);   
     
     return 0; 
}
Run Code Online (Sandbox Code Playgroud)

c integer-division fractions implicit-conversion

0
推荐指数
1
解决办法
100
查看次数

Why does strlen in a if statement behave oddly?

#include <stdio.h>
#include <string.h>

int main()
{
    const char str[11]= "Hello World";
    if(-1 > strlen(str)){
        printf(str);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

This if condition should always return false. But here it's not. But if I put strlen(str) value to another variable and compare with that, then it works as expected. What I am missing here? Is it compiler dependent or something?

c c++ unsigned strlen implicit-conversion

0
推荐指数
1
解决办法
343
查看次数

std::string 隐式转换优先级,string_view 优于 const char*

我有两个重载函数:

void Set(const char * str) { std::cout << "const char * setter: " << str << "\n"; }
void Set(const std::string_view & sv) { std::cout << "string_view setter: " << sv << "\n"; }
Run Code Online (Sandbox Code Playgroud)

我在一个std::string. 它选择std::string_view重载而不是 const char * 重载,即它选择std::stringto std::string_viewover std::stringto的隐式转换const char *

这是在 中的保证行为std::string,还是他们的选择是任意的?如果这是一个有保证的行为,他们是如何使它更喜欢 string_view 转换而不是另一个?

c++ stl overload-resolution implicit-conversion c++17

0
推荐指数
1
解决办法
89
查看次数

检查(全局)函数是否存在但不允许隐式转换

考虑这个简单的检查是否定义了(全局)函数:

template <typename T>
concept has_f = requires ( const T& t ) { Function( t ); };
// later use in MyClass<T>:
if constexpr ( has_f<T> ) Function( value );
Run Code Online (Sandbox Code Playgroud)

不幸的是,这允许隐式转换。这显然是一个很大的混乱风险。

问题:如何检查 Function( const T& t ) 是否“显式”存在?

就像是

if constexpr ( std::is_same_v<decltype( Function( t ) ), void> )
Run Code Online (Sandbox Code Playgroud)

应该没有隐式转换,但我无法让它工作。

注意:概念方法的重点是摆脱旧的“检测模式”并进行简化。

c++ templates function implicit-conversion c++20

0
推荐指数
1
解决办法
135
查看次数

为什么非 const 引用不能初始化为不同类型的对象?

我正在阅读这本书,它写道我们可以将一种类型的常量引用分配给任何其他类型的对象,原因是,内部编译器将Rvalue分配给与引用相同类型的对象,然后const引用被初始化为相同类型的对象,但是,如果这种类型的隐式转换有助于将常量引用分配给不同类型的对象,那么为什么不能隐式地进行相同的转换,因为对于这种显式转换。

#include<iostream>
using namespace std;
int main()
{
    int a = 10;

    double temp = (double)a;
    double &x = temp;
    cout << x << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它的工作方式相同,为什么它没有在编译器中预先配置?

c++ reference implicit-conversion explicit-conversion

0
推荐指数
1
解决办法
83
查看次数

没有隐式参数的函数可以使用隐式 def 吗?

我在这个博客上遇到了这个代码块。我想知道h1("Hello World")h1(children: Frag*).

object TheirCode{
  trait Frag{
    def render: String
  }
  // HTML constructors
  def div(children: Frag*): Frag
  def p(children: Frag*): Frag
  def h1(children: Frag*): Frag
  ...
  implicit def stringFrag(s: String): Frag
}

object MyCode{
  import TheirCode._
  val frag = div(
    h1("Hello World"),
    p("I am a paragraph")
  )
  frag.render // <div><h1>Hello World</h1><p>I am a paragraph</p></div>
}
Run Code Online (Sandbox Code Playgroud)

我猜会implicit def stringFrag自动转换h1(String)为,h1(Frag)但我一直认为方法参数需要隐式前缀才能工作(即implicit h1(children: Frag*)

scala implicit implicit-conversion

0
推荐指数
1
解决办法
39
查看次数

如何拥有一个主要用作单个字符串的字符串数组?

在我的项目中,我有一个类(可能有多个派生类),其中一个字段是一个字符串。

我们也有实例化这个类的许多对象的代码(出于技术原因,每个实例都有单独的代码)。所以代码文件可能很长。

对于大多数情况,这是可以的。但在极少数情况下,我需要有一个字符串数组作为 TestValue。我知道我可以将其声明为string[]. 但是因为通常我们只分配单个字符串,所以当只需要一个字符串时,有可能不必总是在代码中显式创建一个数组,就像这样:

public class Datapoint
{
    public uint Id;

    public string[] TestValue;
}

public void CreateEntries()
{
   Table.Add(new Datapoint { Id = 1, TestValue = "123" });
   Table.Add(new Datapoint { Id = 2, TestValue = "12.9" });
   Table.Add(new Datapoint { Id = 3, TestValue = "Enabled" });
   Table.Add(new Datapoint { Id = 4, TestValue = "Temperature" });
   Table.Add(new Datapoint { Id = 5, TestValue = { "12.3", "9.8", "7.3" } });
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢!

c# arrays string implicit-conversion

0
推荐指数
1
解决办法
51
查看次数