标签: operator-keyword

C++重载了运算符解析

g ++ 4.4.5

我有一个扩展类std :: ofstream的类来添加一些功能.

 MyStream& MyStream::operator<<(const bool& val) {
  if(this->pos == 8) {
    this->pos = 0;
    ofstream::operator<<(this->curbyte); //call the parent method
  }
  curbyte = curbyte + (val << pos++);
  return *(this);
}
Run Code Online (Sandbox Code Playgroud)

这基本上允许你将单个位写为bool然后它将使用父<<方法写入每组8.我不得不在这里使用这个调用语法,因为我正在调用基本方法,但在我使用这个类的实际main方法中,我尝试调用以下行:

bout << (unsigned char) 255u;
Run Code Online (Sandbox Code Playgroud)

我想要调用<<方法已经为ofstream和unsigned char定义了但是它给了我一个很长的模糊的重载错误,列出了已经为ofstream定义的所有char相关的候选者(char,unsigned char,signed char)和我自己的bool方法,即使我明确地转向char.但是我确实设法让它与以下工作:

bout.operator<<((unsigned char) 255u);
Run Code Online (Sandbox Code Playgroud)

这必须与g ++如何进行隐式转换有关(我的猜测是在第一种情况下我的用户定义的转换之后还有一次可能的转换,这使得函数调用语法避免不明确).有没有人确切知道为什么会发生这种情况,或者是否有更好的语法来避免错误?

c++ iostream overloading resolution operator-keyword

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

为什么operator []不允许使用map但允许使用int数组?

我有一个有以下私人成员的班级:

 private:
  int *vals_;
  size_type *cidx_;
  std::map< size_type, std::pair<size_t, unsigned int> > ridx_;
Run Code Online (Sandbox Code Playgroud)

现在我试图在operator << overload中访问这些变量:(注意m是const)

std::ostream& operator<<(std::ostream &os, const SMatrix &m) 
{
    os << m.cidx_[0] << endl;
    os << m.ridx_[0].first << endl;

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

我发现m.cidx_ [0]会起作用,但m.ridx_ [0] .first会出错:

错误:将'const std :: map,std :: less,std :: allocator >>>'作为'_Tp&std :: map <_Key,_Tp,_Compare,_Alloc> :: operator []的'this'参数传递( const _Key&)[with _Key = unsigned int,_Tp = std :: pair,_Compare = std :: less,_Alloc = std :: allocator >>]'丢弃限定符

我认为这意味着operator []是一个修改操作符,因此与m是const的事实相矛盾.但是为什么它适用于vals_和cidx_,它们是int和size_type数组?

c++ map operator-keyword

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

C++运算符重载 - 调用函数以获取重载内的值

有没有办法可以在C++中调用运算符重载并在比较期间调用参数的函数?

例如:

class MyClass{
  private:
    int x;
    int y;
  public:
    MyClass(int x, int y);
    int getX();
    int getY();
    bool operator < (const MyClass &other) const {
        return (x < other.getX()); //this does not work!
        // this would work, though, if x was public:
        // return x < other.x;
    }
};
Run Code Online (Sandbox Code Playgroud)

基本上,在我调用other.getX()的地方,如何让它通过一个函数返回自己的x值,以便与本地函数进行比较,而不是让x公共?有没有办法做到这一点?

感谢你的帮助!

c++ overloading function operator-keyword

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

operator << C++中的类为类中的类重载

我有以下课程:

class mypipe {
    class node {
        public:
            char ch;
            node* next;

            node(){...}
            node(char c){..}
    } ;      
public:
    unsigned int size;
    node* head;
Run Code Online (Sandbox Code Playgroud)

我需要重载运算符<<,以便像现在一样打印mypipe.

然后,我正在尝试编写以下内容:

friend ostream& operator<< (ostream& stream, mypipe p)  {
     node* curr = p.head -> next;
...
Run Code Online (Sandbox Code Playgroud)

变量定义后立即.
问题是我收到错误"标识符节点未定义".
我试图声明运算符并在类之外实现它,这没有帮助.

有没有人有任何想法?
提前感谢任何可以帮助的人:)

c++ operator-overloading operator-keyword

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

Javascript:变量名中的冒号运算符是什么?

我有这样的代码:

var db: name = dbFunction(true);

dbFunction returning Object.
Run Code Online (Sandbox Code Playgroud)

我有疑问,在变量名中做这个冒号运算符是什么?

javascript operator-keyword

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

目标C:使用带有双打的'%'运算符

我今天遇到了一个有趣的问题.作为练习的一部分,我熟练掌握了Objective-c,当我坐在数学课上时,我为每个在板上完成的问题编写程序,希望增加我的数学和编程能力.

但是,今天我遇到了一个问题.其中一个问题是"查找10564245(< - 示例编号)可被整除的最大素数"

所以,我进去制作了这个节目.我做了要检查的值的循环,然后开始编码它检查提醒的部分,如果它是0,它会记录它,如果不是,它会跳过它.

但是,由于这个数字太大而不能成为一个int,它必须是一个double.当我尝试插入数字时,程序在我想使用%带有double 的运算符时给了我错误.如果你有一个非常大的数字,有没有办法找到剩余的?

谢谢

错误: Invalid operands to binary expression

编辑:解决了!

我从每个答案中拿了一点.我们有fmod功能,但我最终使用long而不是int,我不知道为什么我没有想到原始

math xcode objective-c operator-keyword

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

PHP' - >'into'.'

是否可以将对象运算符" - >"更改为".",因此访问类方法看起来像这样:$user.add()而不是$user->add()

我想这不是一个非常聪明的问题,但你必须同意,当你输入" - >"时你有两个符号,其中一个你必须按SHIFT,这是比"."更复杂的方法.这个圆点看起来更漂亮.

php object operator-keyword

0
推荐指数
3
解决办法
141
查看次数

java中的递增和递减运算符

我对增量和减量运算符有疑问.我无法理解为什么java会给出这些输出.

    x = 5;  y = 10;
    System.out.println(z = y *= x++); // output is 50
    x = 2; y = 3; z = 4;
    System.out.println("Result = "+ z + y++ * x); // output is Result = 46
    x = 5;
    System.out.println( x++*x); // output is 30
    x = 5;
    System.out.println( x*x++); // output is 25
Run Code Online (Sandbox Code Playgroud)

例如,在第二个println函数中,y在不增加1的情况下被乘法,在第三个函数中,x与x + 1相乘.因为我知道一元递增和一元递减运算符比算术运算符具有更高的优先级所以为什么第二个算术计算而不增加1(y ++*x = 3*2 = 6那里为什么不(y + 1)*x = 8?

java increment decrement operator-keyword

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

C++,三元运算符和cout

这段代码不起作用

int main(){
cout << 5 ? (5 ? 0 : 2) : 5;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效

int main(){
cout << (5 ? (5 ? 0 : 2) : 5);
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)

不明白为什么?

c++ ternary-operator conditional-operator operator-keyword

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

如何重载+ =运算符来返回一个字符串?

我只是有一个简单的问题:如何重载+ =运算符以返回一个字符串.这是我尝试过的,但没有成功.

// 'Student' is the class that this function is in
// 'get_name()' returns the name of the student
// 'get_grade()' returns the grade of the student
// Description:
// Ultimately I will be creating a list of students and their grades in
// the format of (Student1, Grade1) (Student2, Name2) ... (StudentN, GradeN)
// in a higher level class, and thus I need an overloaded += function.
Student& Student::operator+=(const Student& RHS)
{
    string temp_string;
    temp_string = "( " …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

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