小编Dav*_*vid的帖子

在c ++中使用重载的歧义错误

我试图使用浮动和整数重载.当我只使用整数时,代码运行正常,但是当我包含浮动时它给了我错误.代码如下:

#include <iostream>
using namespace std;

int calculate(int x,int y);
float calculate(float x,float y);
const int MAININT=4;

int main()
{
    int result=calculate(5,5);
    float fresult=calculate(7.5,7.5);                 LINE X
    cout << (result + MAININT + fresult);             LINE Y
    return 0;
}

int calculate(int x,int y)
{
    int result=x*y;
    return result;
}

float calculate(float x,float y)
{
    int result=x*y;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

通过从LINE Y删除LINE X和fresult,代码没有给我任何错误.所以我假设LINE X中肯定有问题,但我不明白为什么会出错.

我收到的错误消息是:

[Error] call of overloaded 'calculate(double, double)' is ambiguous
[Note] candidates are:
[Note] int calculate(int, int)
[Note] float …
Run Code Online (Sandbox Code Playgroud)

c++ overloading overload-resolution

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

C++的初学者.在代码中获取错误

我对c ++完全不熟悉,但我试图从我正在阅读的关于这个主题的书中运行代码.对于初学者来说,这是一本非常基础的书,但它包含了我试图写入c ++的som代码,以了解它是如何工作的.我尝试了以下代码,但我收到两个错误:

在函数'int main()'中:

[错误]预期';' 'emp'之前

#include <iostream>
#include <ctime>

class Employee
{
    private:
        int m_id;
    public:
        Employee(){}
        Employee(int id)
        {
            m_id=id;
        }
        int id;
        std::string firstname;
        std::string lastname;
        int birthyear;

        void ClockTime()
        {
            //clocktime code goes here
        }

        ~Employee()
        {
             m_id=0;
        }
};

int main()
{
    Employee emp(2);
    std::cout<<"The employee time clocked is"
    emp.ClockTime()<<std::endl;     
    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

为了使这段代码有效,我还有很多需要改变的地方吗?

c++

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

标签 统计

c++ ×2

overload-resolution ×1

overloading ×1