c++初学者,有个问题困扰小弟很久,求能人解答一下

weixin_41865657 2018-03-19 05:54:02
源代码如下:
#include <iostream>

using namespace std;
int MAX(int x);
int main(int argc, char *argv[]) {
int n = 0; int a;int sum = 0; int i = 0;
" how many numbers do you want to input? please input: ";
cin>>n;
if(n>0&&n<=100)
{
cout<<"please input";
while(i<=n)
{
cin>>a;
cout<<" ";
sum += a;
max = MAX(a)
i += 1;

}
cout<<sum<<endl<<max;



}
else
cout<<"your input is wrong";

}

int MAX(int x)
{
int max = 0;

{
if(x>max)
max = x;
else
max = max;

}
return max;

}

提示的error:
error: reference to overloaded function could not be resolved; did you mean to call it?
max = MAX(a)
^~~
error: reference to overloaded function could not be resolved; did you mean to call it?
cout<<sum<<endl<<max;
^~~

真多不知道哪错了,能为小弟解答的话将十分感谢
...全文
991 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-03-30
  • 打赏
  • 举报
回复
引用 7 楼 theo_a_sommeil 的回复:
如三楼所说,你的main函数中没有对max的定义。 其次你的逻辑是有问题的,最后是没有办法得到你想的最大的数,因为: max每执行完一次,它内部的值(例如你定义在里面的max)都会被释放,因此你的每次比较都是把你的参数和0比,你输出的max就是你最后输入的值。 你可以考虑定义一个全局变量储存目前最大的值,传入比较函数里,而不要试图在比较函数里储存你目前最大的值!
加static修饰,可以在比较函数里储存目前最大的值。
ifndef冬瓜 2018-03-30
  • 打赏
  • 举报
回复
如三楼所说,你的main函数中没有对max的定义。 其次你的逻辑是有问题的,最后是没有办法得到你想的最大的数,因为: max每执行完一次,它内部的值(例如你定义在里面的max)都会被释放,因此你的每次比较都是把你的参数和0比,你输出的max就是你最后输入的值。 你可以考虑定义一个全局变量储存目前最大的值,传入比较函数里,而不要试图在比较函数里储存你目前最大的值!
ifndef冬瓜 2018-03-30
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
[quote=引用 7 楼 theo_a_sommeil 的回复:] 如三楼所说,你的main函数中没有对max的定义。 其次你的逻辑是有问题的,最后是没有办法得到你想的最大的数,因为: max每执行完一次,它内部的值(例如你定义在里面的max)都会被释放,因此你的每次比较都是把你的参数和0比,你输出的max就是你最后输入的值。 你可以考虑定义一个全局变量储存目前最大的值,传入比较函数里,而不要试图在比较函数里储存你目前最大的值!
加static修饰,可以在比较函数里储存目前最大的值。[/quote] 确实可以,静态局部变量并不会随着函数的结束而被释放掉,但是在这里我觉得并没有什么意义
crospo 2018-03-29
  • 打赏
  • 举报
回复
max = MAX(a) 没有分号 max 变量 你现在打印的是 N+1 输出最大的数 和 累加和 改成: #include "StdAfx.h" #include <iostream> #include<stdlib.h> using namespace std; int MAX(int x); int max_j =0; int main(int argc, char *argv[]) { int n = 0; int a;int sum = 0; int i= 0; " how many numbers do you want to input? please input: "; cin>>n; if(n>0&&n<=100) { cout<<"please input"; while(i<n) { cin>>a; cout<<" "; sum += a; max_j = MAX(a); i+= 1; } cout<<sum<<endl<<max_j; system("pause"); } else cout<<"your input is wrong"; } int MAX(int x) { //int max = 0; { if(x>max_j) max_j = x; else max_j= max_j; } return max_j; }
qq_34644914 2018-03-21
  • 打赏
  • 举报
回复
int MAX(int x); int max = 0; int MAX(int x) { { if (x>max) max = x; else max = max; } return max; } int main(int argc, char *argv[]) { int n = 0; int a; int sum = 0; int i = 0; " how many numbers do you want to input? please input: "; cin >> n; if (n>0 && n <= 100) { cout << "please input"; while (i <= n) { cin >> a; cout << " "; sum += a; max = MAX(a); i += 1; } cout << sum << endl << max; } else cout << "your input is wrong"; } 改好了 可以运行,错误是因为max在你的主函数里没有定义,把他声明成全局的,让主函数可以找到他就行了
weixin_41865657 2018-03-21
  • 打赏
  • 举报
回复
楼上的大神能具体说一下怎么改吗?因为我的程序输入一个数还要按回车,有点不符合实际,希望能帮忙修改一下
lylvv777 2018-03-20
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int MAX(int x);
int main() 
{
	int n = 0;
	int a;
	int sum = 0;
	int i = 0;
	int max = 0;
	" how many numbers do you want to input? please input: ";
	cin >> n;
	if (n>0 && n <= 100)
	{
		cout << "please input";
		while (i <= n)
		{
			cin >> a;
			cout << " ";
			sum += a;
			max = MAX(a);	//max没定义 结尾没有分号
			i += 1;

		}
		cout << sum << endl << max;
	}
	else
		cout << "your input is wrong";
	return 0;
}

int MAX(int x)
{
	int max = 0;
	{
		if (x>max)
			max = x;
		else
			max = max;
	}
	return max;
}
另外你这个函数有问题的哦 实际上输入的个数并不是n 自己改吧
chujiu_ 2018-03-19
  • 打赏
  • 举报
回复
max 定义在MAX函数体内,离开MAX它的生命周期就结束了。把它定义为全局变量试试。
自信男孩 2018-03-19
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

int MAX(int x, int y);

int main(int argc, char *argv[])
{
    int n = 0;
    int a, sum = 0, i = 0, max;

    cout<<"how many numbers do you want to input? please input: ";
    cin>>n;

    if (n > 100 || n <= 0) {
        cout<<"your input is wrong";
        return 0;
    }

    cout<<"please input: ";
    while(i < n)
    {
        cin>>a;
        sum += a;
        max = a;
        max = MAX(max, a);
        i += 1;
    }

    cout<<"sum = "<<sum<<", "<<"max = "<<max<<endl;

    return 0;
}

int MAX(int x, int y)
{
    return x > y ? x : y;
}
参考一下吧

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧