标签: operator-keyword

C++运算符重载错误

#include<iostream>
using namespace std;

class complex {
    double real;
    double image;
public:
    complex(double r=0,double i=0) : real(r), image(i) { };
    complex(const complex& c) : real(c.real), image(c.image) { };
    ~complex(){};
    double re() const {
        return real;
    };
    double im() const{
        return image;
    };
    const complex& operator =(const complex&c)
    {
        real = c.real;
        image = c.image;
        return *this;
    };
    const complex& operator +=(const complex&c)
    {
        real += c.real;
        image += c.image;
        return *this;
    };
    const complex& operator -=(const complex&c)
    {
        real -= …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

0
推荐指数
2
解决办法
7702
查看次数

重载all-const类型的复制赋值运算符的正确方法是什么?

假设我有这样的结构:

struct Foo
{
  const int bar;
  const char baz;

  Foo& operator=(const Foo& other)
  {
    memcpy(this,&other,sizeof(Foo)); //How am I supposed to write this decently?
    return *this;
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望Foo的所有字段都是final,我希望Foo类型的变量与其他原始值类型一样.说,int,当然我们可以写这样的东西:

 int i = 0;
 i = 42;

 Foo foo = {007,'B'}
 foo = {42,'X'}
Run Code Online (Sandbox Code Playgroud)

然而,对于我可怜的Foo类型,我是否必须使用memcpy这样的方法来解决类型安全检查?我知道我可以删除const修饰符,将字段标记为私有并添加一些getter,但这不是重点.我只想知道是否有一种不错的方式来编写=运算符的内容.

提前致谢!

~~~~~

查看以下示例:

//If the = op is not implemented, this won't compile
Foo stat;
for(int i=0;i!=100;++i)
{
  stat = func(i);
  if(stat.bar == 0)...
}

//But weird thing is, if I declare the 'stat' inside the …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

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

operator <<找不到匹配项

    class Shape
    {
        virtual void out() = 0;
    };

    std::ostream& operator<<(std::ostream& os, Shape& a)
    {
        return os << a.out();
    }
Run Code Online (Sandbox Code Playgroud)

我想创建一个抽象基类,并且稍后可以简单地使用cout << Triangle/Square等,其中Triangle,Square是来自Shape的派生类.

如果我只是这样说它可以正常工作return os << "test"; 我猜它与a.out()没有被正确调用有关,我似乎无法查明问题.

c++ operator-keyword

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

分数运算符

我需要编写一个程序来计算分数,这是我的头文件:

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
using namespace std;

class Fraction {

    private:
        int *numer;
        int *denom;
        int gcd(int, int);
    public:
        void reduce();
        int getNum();
        int getDen();
        Fraction();
        Fraction(int numerator);
        Fraction(int num, int den);
        Fraction(string s);  // take string parameter of the form of "numerator/defnominator
        Fraction(Fraction &other);  // copy constructor
        Fraction & operator=(Fraction & rhs);
        ~Fraction();
        // overloading arithematic operation
        Fraction & operator+ (Fraction & rhs);
        Fraction & operator- (Fraction & rhs);
        Fraction & operator* (Fraction & …
Run Code Online (Sandbox Code Playgroud)

c++ string class operator-keyword

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

是否可以添加课程?

我知道String是java.lang中的最后一个类,所以像string类一样可以使用plus(+)运算符追加其他类.

例如我有一个类:

 public class Foo{
    public int x;
    public int y;
    public Foo(int x,int y){
       this.x=x;
       this.y=y;
    }
    public Foo append(int x,int y){
       return new Foo(this.x+x,this.y+y);
    }
 }
Run Code Online (Sandbox Code Playgroud)

现在,是否可以添加两个类,如下所示:

 Foo a=new Foo(2,3),b=new Foo(3,4);
 Foo c=a+b;
 System.out.println(c.x+"  "+c.y);
Run Code Online (Sandbox Code Playgroud)

得到这样的输出:

 5  7
Run Code Online (Sandbox Code Playgroud)

如果是的话,我还需要做什么,如果没有原因?

java addition operator-keyword

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

运算符*不能应用于double

public void onClick(View v){
    switch(v.getId()){
        case R.id.bereken:

           EditText basis = (EditText)findViewById(R.id.Basis);
              String tussenBasis = basis.getText().toString();
                Double.valueOf(tussenBasis);
            //

            EditText hoogte = (EditText)findViewById(R.id.Hoogte);
              String tussenHoogte = hoogte.getText().toString();
                Double.valueOf(tussenHoogte);

            double half = 1 / 2;
            //half = 0,5

            double einde = half * basis * hoogte;
            //eind antwoord formule

            ((TextView)findViewById(R.id.antwoord)).setText("Het antwoord is: " + einde);
            break;
    }

     }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试为三角形区域制作一个简单的计算器,但我似乎被困在这里.

java android multiplying operator-keyword

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

什么时候使用(--i)?

我明白,虽然循环将继续运行"虽然事情是真的"
但我什么时候会使用

while (--i)
Run Code Online (Sandbox Code Playgroud)

我明白这说"虽然减少我是真的"

我确信我对这一陈述的理解是不正确的.

我希望有人可以举个例子说明何时使用它.

我认为这就像一个if语句,当我减少时,它会继续发生.

javascript decrement operator-keyword

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

使用+运算符时类型不匹配

我目前正在尝试学习如何使用Scala,但我遇到了一些语法问题.

当我输入scala提示时:

import scala.collection.immutable._
var q = Queue[Int](1)
println((q+1).toString)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

<console>:12: error: type mismatch;
 found   : Int(1)
 required: String
              println((q+1).toString)
Run Code Online (Sandbox Code Playgroud)

我只是想使用如下定义的队列的重载+运算符:

def + [B>:A](elem:B):队列[B]创建一个新队列,并在旧队列的末尾添加元素.参数elem - 要插入的元素

但似乎scala做了字符串连接.那么,你能帮助我理解如何将一个元素添加到队列中(不使用完美的enqueue;我想使用+运算符)?也许,你能否给我一些关于那种对初学者来说有点奇怪的行为的解释?

谢谢

scala overloading operator-keyword

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

使用C#三元运算符

可能是一个简单的语法问题.这是对控制台程序的尝试,该程序读取通过用户输入接收的字符串的长度.如果长度大于144,则通知用户字符串长度太长,否则输入的字符串仅输出到控制台.

string input = Console.ReadLine();
(input.Length > 144) ? Console.WriteLine("The message is too long"); : Console.WriteLine(input);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

在第2行获取当前状态的语法错误.我错过了括号吗?

c# ternary operator-keyword

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

C#在里面使用!=运算符?:

我正在尝试学习不等式运算符(!=)的工作原理.我可以理解操作员的基本知识.如果操作数不相等则返回"true",否则返回"false".

但我似乎无法围绕编程类中给出的这个特定示例:

  1. 我为Unity编辑器设置了公共字符串,但我没有输入任何文本.
  2. 两个引号("")表示文本在公共字符串上输入.
  3. 但在这种情况下,条件必须是真的吗?因为我没有输入任何文字

为什么条件返回"false"并且控制台调试"Hello Player 1"??? 不是条件"true"因此条件运算符必须返回第一个表达式而不是第二个表达式吗?

public string playerName;

void OnDisable() {
    playerName = (playerName != "") ? playerName : "Player 1";
    Debug.Log("Hello " + playerName);
Run Code Online (Sandbox Code Playgroud)

c# inequality conditional operator-keyword

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