标签: overloading

重载==编译错误c ++

我做了一个简单的struct命名coord来保持,坐标很好,我想检查两个坐标是否相等,所以我看了如何在另一个线程中做一个正确的运算符重载并想出了这个:

#include <iostream>
using namespace std;

struct coord{
    int x;
    int y;

    inline bool operator==(const coord& lhs, const coord& rhs){
        if(lhs.x == rhs.x && lhs.y == rhs.y){
            return true;
        }
        else{
            return false;
        }
    }
};

int main(){
    coord a,b;
    a.x=5;
    a.y=5;
    b.x=a.x;
    b.y=a.y;
    if(a==b){
        cout<<"Working"<<endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但在编译时我得到了一个巨大的错误,看起来像:

g++ -c -o obj/main.o main.cpp -I../include
main.cpp:8:62: error: ‘bool coord::operator==(const coord&, const coord&)’ must take exactly one argument
     inline bool operator==(const coord& lhs, const coord& rhs){
                                                          ^ …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading

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

C++继承的方法调用基类的方法而不是重载方法

鉴于代码:

class A{
public:
    void callFirst()
    {
        callSecond();
    }
    void callSecond()
    {
        cout << "This an object of class A." << endl;
    }
};

class B : public A{
public:
    void callSecond()
    {
        cout << "This is an object of class B." << endl;
    }
};

int main()
{
    B b;
    b.callFirst();

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

我得到输出:

This an object of class A.
Run Code Online (Sandbox Code Playgroud)

我可以这样做,当我调用派生类的继承方法时,它不会反过来调用基类的方法而不是重载方法,除了重载第一个方法?

c++ inheritance overloading

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

为什么编译器不能解析std :: function参数的重载?

请注意以下示例:

#include <iostream>
#include <functional>
#include <cstdlib>

void Print_Wrapper(std::function<void(int)> function);
void Print(int param);

int main(){

  Print_Wrapper(Print);

  return EXIT_SUCCESS;
}

void Print_Wrapper(std::function<void(int)> function){
  int i = 5;
  function(i);
}
void Print(int param){
  std::cout << param << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,并打印5.


现在看一下添加了重载函数的相同示例:

#include <iostream>
#include <functional>
#include <cstdlib>

void Print_Wrapper(std::function<void(int)> function);
void Print(int param);
void Print(float param);

int main(){

  Print_Wrapper(Print);

  return EXIT_SUCCESS;
}

void Print_Wrapper(std::function<void(int)> function){
  int i = 5;
  function(i);
}
void Print(int param){
  std::cout << param << std::endl;
} …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading std c++11

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

超载功能的简短方法?

为什么要以更短的方式做到这一点?我想到了类似的东西void DoSth(int i) : DoSth(i, this);.

class Foo
{

   void DoSth(int i)
   {
      DoSth(i, this);
   }

   static void DoSth(int i, Foo foo)
   {
      // do sth
   }

}
Run Code Online (Sandbox Code Playgroud)

编辑:忘static了第二个功能

c# overloading

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

最佳重载方法匹配对类构造函数(C#)有一些无效参数

我收到此错误并不完全确定原因.在一个类中,我使用以下代码行创建一个对象:

StoreSale sale = new StoreSale(1, 13.99);
Run Code Online (Sandbox Code Playgroud)

StoreSale类中的构造函数如下所示:

public StoreSale(int quantity, float value)
{
   this.quantity = quantity;
   this.value = value;
}
Run Code Online (Sandbox Code Playgroud)

...而且我收到错误''applicationname'的最佳重载方法匹配.StoreSale.StoreSale(int,float)有一些无效的参数.

谁能告诉我我做错了什么?

c# constructor overloading class

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

可以将operator*重载为多个int和一个char*?

我想获得功能,所以我可以这样做:

std::cout << "here's a message" << 5*"\n";
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

std::string operator* (int lhs, const char* rhs) {
  std::string r = "";
  for(int i = 0; i < lhs; i++) {
    r += rhs;
  }
  return r;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

error: ‘std::string operator*(int, const char*)’ must have an argument of class or enumerated type
Run Code Online (Sandbox Code Playgroud)

根据这篇SO帖子中的答案,"必须有一个类或枚举类型的参数"实际上意味着它几乎看起来我不能做这个时期.那是真的吗?如果没有,我该如何解决这个问题或安排解决方法?

我知道我可以做的是有rhs一个std::string,但随后的演习整点半放弃,因为5*std::string("\n")很笨重.

c++ overloading

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

C++:重载=运算符以创建uint8_t兼容数据类型

我认为这应该是微不足道的:

我写了一个类来处理二进制编码的十进制(BCD)8位值.
该类有像set(),get(),add(),sub()等的方法.完美的工作.
get()的示例:

class bcd8_t {
  public:
    uint8_t get() { return value_u8; }
  private: 
    uint8_t value_u8;
}; 
Run Code Online (Sandbox Code Playgroud)

现在我想将此类转换为新的数据类型.我基本上想要替换类似的东西

bcd8_t  a;  
uint8_t b = a.get();  
Run Code Online (Sandbox Code Playgroud)

通过类似的东西

bcd8_t  a;  
uint8_t b = (uint8_t)a;
Run Code Online (Sandbox Code Playgroud)

所以我希望我能写一个重载的"="运算符,它返回一个uint8_t,如:

class bcd8_t {
  public:
    uint8_t operator=() { return value_u8; }
  private: 
    uint8_t value_u8;
}; 
Run Code Online (Sandbox Code Playgroud)

然而,我曾经尝试过的编译器告诉我

cannot convert 'bcd8_t' to 'uint8_t'  
Run Code Online (Sandbox Code Playgroud)

要么

invalid cast from type 'bcd8_t' to type 'uint8_t'
Run Code Online (Sandbox Code Playgroud)

这该怎么做?

c++ types casting overloading

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

在Haskell中重载全局&&运算符无法编译

我有一个hs文件,试图重载&&运算符

(&&)::Bool->Bool->Bool
True && x = x
False && _ = False

and' :: (Bool)->Bool
and' xs=foldr (&&) True xs
Run Code Online (Sandbox Code Playgroud)

在Prelude中导入时,出现错误:

Ambiguous occurrence ‘&&’
It could refer to either ‘Main.&&’, defined at D:\baby.hs:2:6
                      or ‘Prelude.&&’,
                         imported from ‘Prelude’ at D:\baby.hs:1:1
                         (and originally defined in ‘GHC.Classes’)
Run Code Online (Sandbox Code Playgroud)

所以我改变了最后一行

and' xs=foldr (Main.&&) True xs
Run Code Online (Sandbox Code Playgroud)

现在它打印新的错误消息:

Couldn't match expected type ‘t0 Bool’ with actual type ‘Bool’
In the third argument of ‘foldr’, namely ‘xs’
In the expression: foldr (Main.&&) True xs
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?谢谢.

parsing haskell overloading operator-keyword

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

没有<</>>运算符的ostream和istream重载

每当我使用coutcin时,我必须使用3键(shift,2按<<).

我试着用,(逗号运算符)重载ostream和istream .

而现在一切都运作良好,除了CINint,float,double,char但它的工作原理与char[].我也测试tie()了将ostream绑定到istream的方法,但是cin流不会与cout流绑定.

事实上,cin得到了价值,但这个价值与cout无关.

非常感谢,如果你有一个想法.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;


template < class AT> // AT : All Type 
std::ostream& operator,(std::ostream& out,AT t)
{
    out<<t;
    return out;
}
template < class AT> // AT : All Type 
std::istream& operator,(std::istream& in,AT t)
{
    in>>t;
    return in;
}

int main(){
    cout,"stack over flow\n";
    const char* sof ( "stack …
Run Code Online (Sandbox Code Playgroud)

c++ overloading comma

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

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

我目前正在尝试学习如何使用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
查看次数