c ++错误C2662无法将'this'指针从'const Type'转换为'Type&'

时间:2012-08-22 07:33:47

标签: c++ operator-overloading syntax-error friend-function

我正在尝试重载c ++运算符==但是我遇到了一些错误......

错误C2662:'CombatEvent :: getType':无法将'this'指针从'const CombatEvent'转换为'CombatEvent&'

此错误在此行

if (lhs.getType() == rhs.getType())

见下面的代码:

class CombatEvent {

public:
    CombatEvent(void);
    ~CombatEvent(void);

    enum CombatEventType {
        AttackingType,
        ...
        LowResourcesType
    };

    CombatEventType getType();
    BaseAgent* getAgent();

    friend bool operator<(const CombatEvent& lhs, const CombatEvent& rhs) {

        if (lhs.getType() == rhs.getType())
            return true;

        return false;
    }

    friend bool operator==(const CombatEvent& lhs, const CombatEvent& rhs) {

        if (lhs.getType() == rhs.getType())
            return true;

        return false;
    }

private: 
    UnitType unitType;
}

任何人都可以帮忙吗?

4 个答案:

答案 0 :(得分:52)

CombatEventType getType();

需要

CombatEventType getType() const;

您的编译器正在抱怨,因为函数被赋予了const对象,您尝试将其调用为非const函数。当一个函数获得一个const对象时,在整个函数中对它的所有调用都必须是const(否则编译器无法确定它是否未被修改)。

答案 1 :(得分:6)

将声明更改为:

CombatEventType getType() const;

你只能通过引用const来调用'const'成员。

答案 2 :(得分:5)

这是一个const问题,你的getType方法没有被定义为const,但你的重载运算符参数是。因为getType方法不保证它不会更改类数据,所以编译器会抛出错误,因为您无法更改const参数;

最简单的更改是将getType方法更改为

CombatEventType getType() const;

除非该方法实际上是在改变对象。

答案 3 :(得分:0)

我见过这样的错误代码

<!DOCTYPE html>
<html lang="en">

<head>
     <!-- Required meta tags always come first -->
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
     <meta http-equiv="x-ua-compatible" content="ie=edge">
 
     <!-- Bootstrap CSS -->
     <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
     <link rel="stylesheet" href="css/styles.css">
    <title>Ristorante Con Fusion</title>
</head>

<body>
    <nav class="navbar navbar-dark navbar-expand-sm fixed-top">
        <div class="container">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#Navbar">
                <span class="navbar-toggler-icon"></span>
            </button>
            <a class="navbar-brand mr-auto" href="#">Ristorante Con Fusion</a>
            <div class="collapse navbar-collapse" id="Navbar">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active"><a class="nav-link" href="#">Home</a></li>
                    <li class="nav-item"><a class="nav-link" href="./aboutus.html">About</a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Menu</a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
                </ul> 
            </div>

        </div>
    </nav>
    <header class="jumbotron">
        <div class="container">
            <div class="row row-header">
                <div class="col-12 col-sm-6">
                    <h1>Ristorante con Fusion</h1>
                    <p>We take inspiration from the World's best cuisines, and create a unique fusion experience. Our lipsmacking creations will tickle your culinary senses!</p>
                </div>
                <div class="col-12 col-sm">
                </div>
            </div>
        </div>
    </header>

    <div class="container">
        <div class="row row-content align-items-center">
            <div class="col-12 col-sm-4 order-sm-last col-md-3">
                <h3>Our Lipsmacking Culinary Creations</h3>
            </div>
            <div class="col col-sm-6 order-sm-last col-md">
                <h2>Uthappizza</h2>
                <p>A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.</p>
            </div>
        </div>


        <div class="row row-content align-items-center">
            <div class="col-12 col-sm-4 col-md-3">
                <h3>This Month's Promotions</h3>
            </div>
            <div class="col col-sm-6 col-md">
                <h2>Weekend Grand Buffet</h2>
                <p>Featuring mouthwatering combinations with a choice of five different salads, six enticing appetizers, six main entrees and five choicest desserts. Free flowing bubbly and soft drinks. All for just $19.99 per person </p>
            </div>
        </div>

        <div class="row row-content align-items-center">
            <div class="col-12 col-sm-4 order-sm-last col-md-3">
                <h3>Meet our Culinary Specialists</h3>
            </div>
            <div class="col col-sm-6 order-sm-first col-md">
                <h2>Alberto Somayya</h2>
                <h4>Executive Chef</h4>
                <p>Award winning three-star Michelin chef with wide International experience having worked closely with whos-who in the culinary world, he specializes in creating mouthwatering Indo-Italian fusion experiences. </p>
            </div>
        </div>
    </div>

    <footer class="footer">
        <div class="container">
            <div class="row">             
                <div class="col-4 offset-1 col-sm-2">
                    <h5>Links</h5>
                    <ul class="list-unstyled">
                        <li><a href="./index.html">Home</a></li>
                        <li><a href="./aboutus.html">About</a></li>
                        <li><a href="#">Menu</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
                <div class="col-7 col-sm-5">
                    <h5>Our Address</h5>
                    <address>
                      121, Clear Water Bay Road<br>
                      Clear Water Bay, Kowloon<br>
                      HONG KONG<br>
                      Tel.: +852 1234 5678<br>
                      Fax: +852 8765 4321<br>
                      Email: <a href="mailto:confusion@food.net">confusion@food.net</a>
                   </address>
                </div>
                <div class="col-12 col-sm-4 align-self-center">
                    <div class="text-center">
                        <a href="http://google.com/+">Google+</a>
                        <a href="http://www.facebook.com/profile.php?id=">Facebook</a>
                        <a href="http://www.linkedin.com/in/">LinkedIn</a>
                        <a href="http://twitter.com/">Twitter</a>
                        <a href="http://youtube.com/">YouTube</a>
                        <a href="mailto:">Mail</a>
                    </div>
                </div>
           </div>
           <div class="row justify-content-center">             
                <div class="col-auto">
                    <p>© Copyright 2018 Ristorante Con Fusion</p>
                </div>
           </div>
        </div>
    </footer>
    <!-- jQuery first, then Popper.js, then Bootstrap JS. -->
    <script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
    <script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>

</body>

</html>

当我更改为模板时,它起作用了。

    get_color(const std::unsigned_integral auto &x,
              const std::unsigned_integral auto &y,
              const BPPT &                       depth,
              const std::unsigned_integral auto &palette    = 0U,
              const std::unsigned_integral auto &texture_id = 0U) const