错误C2668:对重载函数的模糊调用 [英] error C2668:ambiguous call to overloaded function

查看:105
本文介绍了错误C2668:对重载函数的模糊调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到错误的地方写了一个示例代码:

I have written a sample code where I am getting the error:

vc_study.cpp(44) : error C2668: 'xyz::xyz' : ambiguous call to overloaded function
    vc_study.cpp(18): could be 'xyz::xyz(int,int)'
    vc_study.cpp(14): or 'xyz::xyz(void)'
1>        while trying to match the argument list '(void)'

\vc_study.cpp(54) : error C2668: 'xyz::xyz' : ambiguous call to overloaded function
    vc_study.cpp(18): could be 'xyz::xyz(int,int)'
    vc_study.cpp(14): or 'xyz::xyz(void)'
1>        while trying to match the argument list '(void)'




代码如下:




The code is as follows:

// VC_CONSOLE.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include <stddef.h>
#include <stdio.h>
#include <windows.h>

class xyz
{
    int x, y;
public:
    xyz(void)                                      //// LINE 14
    {
        printf("xyz constructor");
    }
    xyz(int c = 0, int d = 0);                     //// LINE 18
    void set_values (int,int);
    void print();
};

xyz::xyz(int c, int d):x(44),y(77)
{
    c = c;
    d = d;
}

void xyz::set_values(int c, int d)
{
    x = c;
    y = d;
}

void xyz::print()
{
    printf("x=%d    y = %d\n",x,y);
}

class CED: public xyz
{
    int X, Y;
public:
    CED(void)    //// LINE 44
{
        printf("CED constructor");
    }
    CED(int c = 0, int d = 0);
    void CED_set_values (int,int);
    void CED_print();
};

CED::CED(int c, int d)          //// LINE 54
{
    c = c;
    d = d;
}

void CED::CED_set_values(int c, int d)
{
    X = c;
    Y = d;
}

void CED::CED_print()
{
    printf("X=%d    Y = %d\n",X,Y);
}

int _tmain(int argc, _TCHAR* argv[])
    {
    CED xyzobj(3,4);
    xyzobj.print();

    return 0;
}





Whats wrong in this code and how to eliminate the error?

推荐答案

您为第二个构造函数提供了默认值,因此编译器无法知道是否要使用默认构造函数(不带参数)或第二个构造函数(带默认值).

要解决此问题,只需删除第二个构造函数声明(或至少第一个构造函数声明)上的默认值.
You provided default values for your second constructor so the compiler can''t know if you want to use the default constructor (without arguments) or the second one (with default values).

To fix the problem just remove the default values on the second constructor declaration (or at least the first one).


这篇关于错误C2668:对重载函数的模糊调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆