标签: operator-keyword

模板化类中的关系运算符重载(C++)

我正在创建一个KeyValuePair类,并在重载关系运算符时遇到一些麻烦.我的理解是,这对于使用std排序函数是必要的(我试图根据值进行排序)

这是标题:

template <typename K, typename V>
class KeyValuePair
{
public:
    //factory
    static KeyValuePair<K,V>* newKeyValuePair(K key, V value);  
    //getters
    const K &Key() const;
    const V &Value() const;
    //setter
    V &Value();

    //The problem
    bool operator<(const KeyValuePair<K,V> &rhs);

    string toString();
    ~KeyValuePair(void);
private:
    K key;
    V value;
    KeyValuePair(K key, V value);
    KeyValuePair(void);
};
Run Code Online (Sandbox Code Playgroud)

这是<function的定义

template <typename K, typename V>
bool KeyValuePair<K,V>::operator<(const KeyValuePair<K,V> &rhs)
{
    return value < rhs.Value();
}
Run Code Online (Sandbox Code Playgroud)

这里是我正在测试类的功能的主要部分.

int _tmain(int argc, _TCHAR* argv[])
{
    KeyValuePair<char,int>* kvp1 = KeyValuePair<char, int>::newKeyValuePair('A',1);
    KeyValuePair<char,int>* kvp2 …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading operator-keyword

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

我一直得到一个"运算符==未定义参数类型boolean,int"并且不知道如何修复它

我在第3行的这段代码中不断得到"运算符==未定义参数类型(bo),int":

    public void loadState(int i)
  {
      if (statesSaved[i] == 0)
      {
        return;
      }
      List list = TMIUtils.getMinecraft().h.at.e;
      for (int j = 0; j < 44; j++)
        {
          sx slot = (sx)list.get(j + 1);
          slot.c(null);
          ul itemstack = TMIUtils.copyStack(states[i][j]);
          if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null))
            continue;
          slot.c(itemstack);
    }
  }
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这种情况,因为我有另一个类文件,其中没有错误.

如果它有帮助,这是我的完整类文件:

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.client.Minecraft;

public class TMIConfig
{
  public static …
Run Code Online (Sandbox Code Playgroud)

java undefined operator-keyword minecraft

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

头中名称空间内的运算符声明?

请原谅,但我不知道在短时间内给这个标题命名.

为什么我需要在标头中声明一个重载的运算符,以使其在此示例中起作用:

HEAD.H

#pragma once

namespace test {
    class A {
    public:
        A() : x(0) {}
        int x;
    };
    A& operator++(A& obj);  //this is my question
}
Run Code Online (Sandbox Code Playgroud)

HEAD.CPP

#include "head.h"

namespace test {
    A& operator++(A& obj) {
        ++obj.x;
        return obj;
    }
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>
#include "head.h"
using namespace std;
using namespace test;

int main() {
    A object;
    ++object;  //this won't work if we delete declaration in a header
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

operator ++是在"head.cpp"中的命名空间中定义和声明的,那么为什么我需要在头文件中再次声明它呢?谢谢.

c++ namespaces operator-keyword

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

复制构造函数和赋值运算符都被调用

我有以下程序片段:

Polynomial Polynomial:: add(const Polynomial b)
{
    Polynomial c;
    c.setRoot(internalAdd(root, c.root));
    c.setRoot(internalAdd(b.root, c.root));
    return c;
}

c = (a.add(b));
Run Code Online (Sandbox Code Playgroud)

据我所知,这段代码假设将a和b加在一起,然后通过调用复制构造函数将结果多项式赋值给c.

但是,当我真正测试它时,

  • c立即调用复制构造函数并尝试复制b,
  • 然后a和b添加
  • 然后c尝试通过赋值运算符得到结果多项式
  • 然后程序崩溃了

我该怎么做才能解决这个问题?

c++ constructor copy variable-assignment operator-keyword

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

试图重载cout <<运算符,但它不起作用

我在这里剪掉了我班上不相关的部分.我不知道我做错了什么,只是想尝试能够cout <<对象.

#include <iostream>

class Snipped
{

    public:
        friend std::ostream& operator<<(std::ostream& os, const Snipped& s);

    protected:
    private:
};

std::ostream& operator<<(std::ostream& os, const Snipped& s)
{
    os << "test";
    return os;
}

int main(int argc, char* argv[])
{
    Snipped* s = new Snipped();

        std::cout << s << std::endl << s;

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

预期产量:

test
test
Run Code Online (Sandbox Code Playgroud)

实际产量:

0x12ae20
0x12ae20  (random memory location?)
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-keyword

1
推荐指数
2
解决办法
4704
查看次数

如何正确地从运算符构造函数中调用复制构造函数?

我的代码也可以在这里找到.

这段代码可行(但代码重复很多):

Employee::Employee(const Employee& x)
{
        name=new char [strlen(x.name)+1];
        strcpy(name, x. name);
        strcpy(EGN, x.EGN);
        salary=x.salary;
}

void Employee::operator=(const Employee& x)
{
        delete[] name;
        name=new char [strlen(x.name)+1];
        strcpy(name, x. name);
        strcpy(EGN, x.EGN);
        salary=x.salary;
}

Employee::Employee(char* n, char* e, double s)
{
        name = new char [strlen(n)+1];
        strcpy(name, n);
        strcpy(EGN, e);
        salary=s;
}
Run Code Online (Sandbox Code Playgroud)

以下是我试图避免三次写同样的事情......但它不起作用.是不是可以缩短代码?

Employee::Employee(char* n, char* e, double s)
{
        name = new char [strlen(n)+1];
        strcpy(name, n);
        strcpy(EGN, e);
        salary=s;
}

Employee::Employee(const Employee& x)
{
        Employee(x.name, x.EGN, x.salary);
} …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor function-call operator-keyword

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

我可以像这样使用条件运算符吗?

!isalpha( str[first] )  ? ( return isPalindrome( str, ++first, last ) ) : return isPalindrome( str, first, --last ) ;
Run Code Online (Sandbox Code Playgroud)

我收到语法错误.

c++ conditional operator-keyword

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

泛型类中的赋值运算符.C++

您好我在我的通用链接队列类中尝试重载赋值运算符时遇到问题.

这是有问题的代码:

template<typename T> 
class Queue : public IQueue<T>
{
(...)
public: 
    Queue();
    Queue(const Queue<T> &origQueue);
    virtual~Queue();
    Queue& operator=(const Queue<T> &origQueue);
    void enqueue(const T& element);
    T dequeue();
    T peek() const;
    int size() const;

};



template<typename T>
Queue& Queue<T>::operator=(const Queue<T> &origQueue)
{
(...)

    return *this;
}
Run Code Online (Sandbox Code Playgroud)

我出错的地方有什么想法吗?

c++ generics templates variable-assignment operator-keyword

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

为什么后递增/递减运算符对循环内的变量没有任何影响?

var j = 0;
for( var i = 0; i < 100; i++){
    j = j++;
    console.log(j);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码块的输出是100个零而不是1到100的数字?

j = j + 1; 
Run Code Online (Sandbox Code Playgroud)

另一方面,上面的代码按预期工作.这背后可能是什么原因?

javascript post-increment operator-keyword

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

ISO C++说这些是模糊的,运算符重载

当我写一些关于基本运算符重载的代码时.我发现了这段代码,

struct MyInt {
  public:
    MyInt()  : data() { };

    MyInt(int val) : data(val) { }

    MyInt& operator++() {
      ++data;

      return (*this);
    }

    MyInt operator++(int) {
      MyInt copy = *this;
      ++data;

      return copy;
    }

    MyInt& operator+=(const MyInt& rhs) {
        data += rhs.data;
        return *this;
    }

    MyInt operator+(const MyInt& rhs) const {
      MyInt copy = *this;
      copy += rhs;

      return copy;
    }

    int data;
};
Run Code Online (Sandbox Code Playgroud)

这些都很好,直到我在课程声明后添加它

MyInt operator+(const MyInt& lhs, const MyInt& rhs)
{
  MyInt copy = lhs;
  copy.data += rhs.data; …
Run Code Online (Sandbox Code Playgroud)

c++ overloading ambiguous operator-keyword

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