简体   繁体   English

指针,C ++…代码不起作用

[英]Pointers, C++…code not working

This is the question: Write the definition of a function minMax that has five parameters. 这是一个问题:编写具有五个参数的minMax函数的定义。 The first three parameters are integers. 前三个参数是整数。 The last two are set by the function to the largest and smallest of the values of the first three parameters. 该功能将后两个设置为前三个参数的最大值和最小值。 The function does not return a value. 该函数不返回值。

The function can be used as follows: 该功能可以如下使用:

int a = 31, b = 5, c = 19, big, small; 
minMax(a, b, c, &big, &small); /* big is now 31; small is now 5 */ 

This is my code: 这是我的代码:

void minMax(int x, int y, int z, int* big, int* small)
{
  if (x < y && x < z)

    *small = x;

  else if (y < x && y < z)

    *small = y;

  else if (z < x && z < y)

    *small = z;

  if (x > y && x > z)

    *big = x;

    else if (y > x && y > z)

    *big = y;

  else if (z > x && z > y)

    *big = z;
  }

This is the error I'm getting: 这是我得到的错误:

Your function did not change the value of small . 您的函数值未更改为small Make sure you are dereferencing it in your function. 确保在函数中取消引用它。

Not sure what's wrong? 不知道出什么事了吗?

Thanks. 谢谢。

I see one immediate problem. 我看到一个迫在眉睫的问题。

What do you think will happen when you pass the numbers 1 , 1 and 7 ? 你觉得当你通过数字会发生117

Perhaps you may want to consider the use of <= and >= rather than just < and > . 也许您可能想考虑使用<=>=而不是仅仅使用<>

Since that error message looks nothing like any compiler error I've seen before (and the code is valid syntactically), I'd suggest the message is coming from a test harness which probably: 由于该错误消息看起来与我之前见过的任何编译器错误都不一样(并且该代码语法有效),因此我建议该消息来自测试工具,该工具可能是:

  • sets the big/small values to numbers other than those being passed in (eg, -9999 ). big/small值设置为传入的数字以外的数字(例如-9999 )。
  • calls the function with test data (eg, 1,1,7 ). 使用测试数据(例如1,1,7 )调用该函数。
  • checks the output varibales to ensure they've been changed to the correct values. 检查输出变量以确保已将其更改为正确的值。

In addition, it's not the most readable code in the world (no offence intended). 此外,它不是世界上可读性最高的代码(无意冒犯)。 If you can structure your code in such a way that its intent is clear from a glance (including comments where appropriate), you'll have hordes of future programmers singing your praises and worshiping your name :-) 如果您能够以一种使代码意图一目了然的方式(包括适当的注释)来构造代码,则将有成群的未来程序员大声赞扬并崇拜您的名字:-)

Something like this shows the intent a little more clearly (IMNSHO) than lots of those else if constructs: 这样的事情比else if许多else if构造更清楚地表明了它的意图(IMNSHO):

// Populate big/small based on max/min of x, y and z.

void minMax (int x, int y, int z, int *big, int *small) {
    // Set by default to x, only change if others are bigger.

    *big = x;
     if (y > *big)   *big = y;
     if (z > *big)   *big = z;

    // Same for small but with reversed comparisons.

    *small = x;
     if (y < *small) *small = y;
     if (z < *small) *small = z;
}

I'm not sure what isn't working. 我不确定什么是行不通的。 It seems like that would basically work but could be better structured. 看来这基本上可以工作,但结构可以更好。

Maybe something like this: 也许是这样的:

void minMax(int x, int y, int z, int* big, int* small)
{
    *big = *small = x;

    if (y > *big)
        *big = y;
    if (y < *small)
        *small = y;
    if (z > *big)
        *big = z;
    if (z < *small)
        *small = z;
  }

The error message 错误讯息

Your function did not change the value of small . 您的函数值未更改为small Make sure you are dereferencing it in your function. 确保在函数中取消引用它。

… appears to come from a test harness provided to you by your teacher. …似乎来自您的老师提供给您的测试装置。

Anyway, it's correct: there are values that you can choose where your function will not assign anything to *small . 无论如何,这是正确的:您可以选择一些值,在这些值中函数不会*small分配任何内容。

For example, with a , b and c the same value, your function will do nothing at all. 例如,如果abc的值相同 ,则您的函数将什么也不做。


Anyway, 无论如何,

for future questions, please provide a complete example program that demonstrates the issue. 对于以后的问题,请提供一个完整的示例程序来演示该问题。

So that people will not have to guess and use unreliable telepathy. 这样人们就不必猜测和使用不可靠的心灵感应。


Also, the assignment calls for you to implement a function with an ungood signature . 另外,分配要求您实现签名正确的功能。

It teaches a Bad Way™ to design functions. 它教导了Bad Way™设计功能。

Here is a possible ordinary C++ function signature: 这是可能的普通C ++函数签名:

void getMinAndMax( int& smallest, int& largest, int a, int b, int c )

Here is an even better signature with modern C++ technology: 这是现代C ++技术的一个更好的签名:

std::pair<int, int> minAndMax( int a, int b, int c )

The absence of a get prefix for the latter function's name is because it is an expression-oriented function, like sin and cos (you wouldn't write getSin or getCos , would you?), while the presence of that prefix for the first function is merely to make the name imperative, to reflect that it's not an expression-oriented function but instead an action-oriended function. 后一个函数的名称没有get前缀是因为它是一个面向表达式的函数,如sincos (您不会写getSingetCos ,对吗?),而第一个函数的前缀是存在的只是为了使该名称成为必需,以反映它不是面向表达式的函数,而是一个面向动作的函数。

Of course, with C++11 one would let the function accept any number of arguments. 当然,对于C ++ 11,可以让函数接受任意数量的参数。 Except that as I'm writing this, Visual C++ does not yet support that properly. 除了在我撰写本文时,Visual C ++尚未正确支持。 For example, here is the signature of std::min from the C++11 standard library: 例如,这是C ++ 11标准库中std::min的签名:

template<class T, class Compare>
T min(initializer_list<T> t, Compare comp);

With C++03 one can do that to some degree by accepting a single container argument, of templated type. 使用C ++ 03,可以通过接受模板类型的单个容器参数在某种程度上做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM