“调用'三角'是模棱两可的”错误

问题描述 投票:0回答:3
#include "rec_fun.h"
using namespace mry2270;

void triangle(ostream& outs, unsigned int m, unsigned int n){
    if (m <= n){
        for (int i = 0; i < m; i++){
            cout<<"*";
        }
        cout<< endl;
        triangle(outs, ++m, n);
    }
    else if (m > n && n >= 0) {

        for (int i = 0; i < n; i++){
            cout<<"*";
        }
        cout<< endl;
        triangle(outs, m, --n);
    }
}

void numbers(ostream& outs, const string& prefix, unsigned int levels){

}

bool bears(int n){
    return 0;
}


int main(int argc, const char * argv[]) {



    return 0;
}

#ifndef rec_fun_h
#define rec_fun_h
#include <iostream>
#include <string>
using namespace std;

//  void triangle(ostream& outs, unsigned int m, unsigned int n)
// Precondition: m <= n
// Postcondition: The function has printed a pattern of 2*(n-m+1) lines
// to the output stream outs. The first line contains m asterisks, the next
// line contains m+1 asterisks, and so on up to a line with n asterisks.
// Then the pattern is repeated backwards, going n back down to m.
/* Example output:
 triangle(cout, 3, 5) will print this to cout:
 ***
 ****
 *****
 *****
 ****
 ***
 */
//
//  bool bears(int n)
// Postcondition: A true return value means that it is possible to win
// the bear game by starting with n bears. A false return value means that
// it is not possible to win the bear game by starting with n bears.
// Examples:
//   bear(250) is true (as shown above)
//   bear(42) is true
//   bear(84) is true
//   bear(53) is false
//   bear(41) is false
//
//  bool bears(int n)
// Postcondition: A true return value means that it is possible to win
// the bear game by starting with n bears. A false return value means that
// it is not possible to win the bear game by starting with n bears.
// Examples:
//   bear(250) is true (as shown above)
//   bear(42) is true
//   bear(84) is true
//   bear(53) is false
//   bear(41) is false

namespace mry2270 {

    void triangle(ostream& outs, unsigned int m, unsigned int n);

    void numbers(ostream& outs, const string& prefix, unsigned int levels);

    bool bears(int n);

}

我正在处理一个递归类并不断收到此错误。知道怎么解决吗?

编辑:这是main.cpp文件

我认为潜在的问题可能在.h文件下,但我不确定。

编辑:我已经添加了.h文件,并在递归调用中更新了我的参数。我仍然得到“调用'三角形'是模棱两可的”错误,我现在得到一个“无符号表达式的比较> = 0总是为真”错误。

c++ xcode recursion
3个回答
0
投票

在标题中,有一个前向声明

namespace mry2270 {
    void triangle(ostream& outs, unsigned int m, unsigned int n);
    ...
}

承诺一个函数mry2270::triangle存在于某处。

cpp文件定义了一个名为triangle的函数。

void triangle(ostream& outs, int m, int n){

请注意名称的不同之处。 triangle不是mry2270::triangle,打破了头球的承诺。通常情况下,链接器会在寻找mry2270::triangle的实现时捕获它。

事情变得奇怪的是using namespace mry2270;允许mry2270::triangle被称为triangle。已经存在triangle,导致编译器报告歧义。

可以通过将命名空间添加到triangle的实现来解决。更换

void triangle(ostream& outs, unsigned int m, unsigned int n) {

void mry2270::triangle(ostream& outs, unsigned int m, unsigned int n) {

通过整理,您可以继续整理其他答案指出的其余错误。


0
投票

为了使此代码触发您提到的错误,必须有一个单独的triangle声明与您在代码中提供的定义冲突 - 例如,具有不同参数类型的前向声明。

您的代码的真正问题是它会导致堆栈溢出。仔细看看这个调用:

triangle(outs, m++, n);

你知道为什么呼叫通过trianglemn相同的确切值?如果你说“后增量”,你得到一个金星你的答案:电话应该使用m+1来解决这个问题;同样,其他调用中的n--应更改为n-1

此外,您的代码中没有路径退出递归,因为两个分支最终无条件地调用triangle。您应该将n的检查添加为零,并在减法后添加一个返回值(当然,它不能为负,因为它是unsigned,但是一旦n溢出,您的程序会打印大量不需要的星号) 。

Demo.


0
投票

调用'三角'是一个模棱两可的错误

不,你的triangle不含糊,至少从你的例子来看。

然而,你的triangle有它自己的(两个)问题 - 没有终止来阻止递归,并且错误地使用operator++operator--如下:

void triangle(ostream& outs, int m, int n){
    if (m <= n){
        for (int i = 0; i < m; i++){
            cout<<"*";
        }
        cout<< endl;
        triangle(outs, ++m, n); // instead of triangle(outs, m++, n);
    } 
    else if (m > n && n >= 0) { // if `n` is negative, exits.
        for (int i = 0; i < n; i++){
            cout<<"*";
        }
        cout<< endl;
        triangle(outs, m, --n); // instead of triangle(outs, m, n--)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.