一个重载函数,但程序并不这么看

发布于 2024-10-17 16:08:42 字数 2209 浏览 2 评论 0原文

以下代码重载了函数CandyBarFunc。第一个原型定义了函数,以便它修改结构的值。第二个原型定义了该函数,以便它只显示传递的结构的内容。问题是,当我运行控制台程序时,除了按任意键...之外,屏幕上什么也没有出现。 我尝试调试它,发现第一个原型工作正常(我将第二个原型的显示功能添加到第一个原型),因为它修改并显示了结构的内容。因此,重载似乎不起作用,因为第二个函数原型在执行期间不会被调用,因为控制台屏幕上没有显示任何内容。我不确定签名是否不好,因为编译器不会抱怨不明确的函数调用。我错过了代码中明显的东西吗?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    char name[40];
    double weight;
    int calories;
};

void CandyBarFunc(CandyBar & astruct, const char * aname = "Millennium Munch", double aweight = 2.85, int acalories = 350);
void CandyBarFunc(const CandyBar & astruct);

int main(void)
{
    CandyBar MyCandyBar =
    {
        "Hi",
        1.5,
        456
    };
    cout << "1" << endl; 'little debug'
    CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar'
    CandyBarFunc(MyCandyBar, "Hello World Candy Bar", 1.25, 200); 'suppose to modify MyCandyBar
    CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar again'
    cout << "2"; 'little debug'
    return 0;
}

void CandyBarFunc(CandyBar & astruct, const char * aname, double aweight, int acalories)
{
    strncpy_s(astruct.name,aname,40);
    astruct.weight = aweight;
    astruct.calories = acalories;
    cout << "Name: " << astruct.name << endl; 'not suppose to be here, just for debug'
    cout << "Weight: " << astruct.weight << endl; 'not suppose to be here, just for _ debug'
    cout << "Calories: " << astruct.calories; 'not suppose to be here, just for debug'
}

void CandyBarFunc(const CandyBar & astruct)
{
    cout << "Name: " << astruct.name << endl;
    cout << "Weight: " << astruct.weight << endl;
    cout << "Calories: " << astruct.calories;
}

锻炼:

CandyBar 结构包含三个成员。第一个会员持有品牌 糖果棒的名称。第二个成员持有重量(可能有分数 部分)的糖果,第三个成员保存卡路里数(整数 值)在直板条中。编写一个程序,使用一个以 a 作为参数的函数 对 CandyBar、指向 char 的指针、double 和 int 的引用,并使用最后三个 值来设置结构体的相应成员。最后三个参数 应具有默认值“Millennium Munch”、2.85 和 350。此外,该程序 应该使用一个函数,该函数将对 CandyBar 的引用作为参数并显示 结构的内容。在适当的地方使用 const。

The following code has overloaded function CandyBarFunc. First prototype defines the function so that it modifies the value of a structure. Second prototype defines the function so that it just displays the content of a passed structure. The problem is that when I run the console program nothing appears on the screen except the Press Any Key...
I tried to debug it and found out that first prototype works properly(I added the display functionality from the second prototype to the first one) becuase it modified and displayed the contents of the structure. So therefore it seems that overloading didn't work because the second function prototype doesn't get called during execution because nothing is displayed on the console screen. I'm not sure if the signaure is bad because the compiler does't complain about the ambigious function call. Did I miss something obvious in the code?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    char name[40];
    double weight;
    int calories;
};

void CandyBarFunc(CandyBar & astruct, const char * aname = "Millennium Munch", double aweight = 2.85, int acalories = 350);
void CandyBarFunc(const CandyBar & astruct);

int main(void)
{
    CandyBar MyCandyBar =
    {
        "Hi",
        1.5,
        456
    };
    cout << "1" << endl; 'little debug'
    CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar'
    CandyBarFunc(MyCandyBar, "Hello World Candy Bar", 1.25, 200); 'suppose to modify MyCandyBar
    CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar again'
    cout << "2"; 'little debug'
    return 0;
}

void CandyBarFunc(CandyBar & astruct, const char * aname, double aweight, int acalories)
{
    strncpy_s(astruct.name,aname,40);
    astruct.weight = aweight;
    astruct.calories = acalories;
    cout << "Name: " << astruct.name << endl; 'not suppose to be here, just for debug'
    cout << "Weight: " << astruct.weight << endl; 'not suppose to be here, just for _ debug'
    cout << "Calories: " << astruct.calories; 'not suppose to be here, just for debug'
}

void CandyBarFunc(const CandyBar & astruct)
{
    cout << "Name: " << astruct.name << endl;
    cout << "Weight: " << astruct.weight << endl;
    cout << "Calories: " << astruct.calories;
}

Exercise:

The CandyBar structure contains three members. The first member holds the brand
name of a candy bar. The second member holds the weight (which may have a fractional
part) of the candy bar, and the third member holds the number of calories (an integer
value) in the candy bar. Write a program that uses a function that takes as arguments a
reference to CandyBar, a pointer-to-char, a double, and an int and uses the last three
values to set the corresponding members of the structure. The last three arguments
should have default values of “Millennium Munch,” 2.85, and 350. Also, the program
should use a function that takes a reference to a CandyBar as an argument and displays
the contents of the structure. Use const where appropriate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2024-10-24 16:08:42

由于 MyCandyBar 不是 const,编译器选择第一个(对非 const 的引用)重载。

但说真的,如果您希望一个函数设置属性,而另一个函数将它们打印出来,请不要通过给它们提供相同的名称来滥用重载。只需以不同的名称命名即可,不再有问题。

另外,在 C++ 中,我们更喜欢 std::string 而不是固定大小的字符数组和字符指针。

Since MyCandyBar isn't const, the compiler choses the first (reference to non-const) overload.

But seriously, if you want one function to set properties and another function to print them out, please don't abuse overloading by giving them the same name. Just name them differently, no more problems.

Also, in C++ we prefer std::string to fixed-size character arrays and character pointers.

忘东忘西忘不掉你 2024-10-24 16:08:42

由于 MyCandyBar 不是 const,因此它将始终尝试使用接受非 const CandyBar 的函数。您可以通过将其强制转换为 const 来强制它调用另一个函数:

CandyBarFunc((const CandyBar &)MyCandyBar);

Since MyCandyBar is not const, it will always try to use the function which accepts the non const CandyBar. You can force it to call the other function by casting it to const:

CandyBarFunc((const CandyBar &)MyCandyBar);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文