注册 登录
编程论坛 C++教室

函数定义题目的问题

洪夜馨 发布于 2009-07-16 08:49, 1107 次点击
我弄的是:
#include "iostream.h"
int max(int a,int b);
int main()
{
    do
    {
    int h,f,t;
    char q;
    cout<<"请输入你想要比对的整数"<<endl;
    cin>>h>>f;
    cout<<"你输入的整数为"<<h<<"和"<<f<<"吗?"<<endl;
    cout<<"是的话请按Y,反之请按N"<<endl;
    cin<<q;
    if(q!='n'&&'N'&&'y'&&'Y')
    {
        cout<<"输入错误"<<endl;
    }
    }while(q!='y'&&'Y')
        t=max(h,f);
    cout<<"这2个整数中最大的数为"<<t<<endl;
    return 0;
}
int max(int a,int b)
{
    if(a>=b) return a;
    return b;
}
而下面编译的时候显示:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
F:\教育教学\练习\1.cpp(13) : error C2676: binary '<<' : 'class istream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator
F:\教育教学\练习\1.cpp(18) : error C2065: 'q' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2146: syntax error : missing ';' before identifier 't'
F:\教育教学\练习\1.cpp(19) : error C2065: 't' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2065: 'h' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2065: 'f' : undeclared identifier
执行 cl.exe 时出错.

1.obj - 1 error(s), 0 warning(s)
谁能告诉我哪里错了?

另外再问一下,谁能教教我下面显示的东西怎么看?如果会看了我就能自己解决了.
7 回复
#2
wuyun85362009-07-16 15:06
#include <iostream>
using namespace std;
int max(int a,int b);
int main()
{
    int h,f,t;
    char q;
    do
    {
    cout<<"请输入你想要比对的整数"<<endl;
    cin>>h>>f;
    cout<<"你输入的整数为"<<h<<"和"<<f<<"吗?"<<endl;
    cout<<"是的话请按Y,反之请按N"<<endl;
    cin>>q;
   if(q!='n'&&q!='N'&&q!='y'&&q!='Y')
    {
        cout<<"输入错误"<<endl;
    }
    }
    while(q!='y'&&q!='Y');
        
        t=max(h,f);
        cout<<"这2个整数中最大的数为"<<t<<endl;
        return 0;
}
int max(int a,int b)
{
    if(a>=b) return a;
    return b;
}
#3
洪夜馨2009-07-16 15:55
using namespace std;
请问一下,是什么意思?
#4
洪夜馨2009-07-16 16:16
哦,我已经发新帖了。谢谢你,wuyun8536。我还是结帖吧。
#5
jackie19182009-07-17 17:05
using namespace std;
是使用命名空间~~~算是现在的标准写法~~但友元函数和命名空间不要一起用~~
#6
pangding2009-07-18 12:22
以下是引用洪夜馨在2009-7-16 08:49的发言:
Compiling...
1.cpp
F:\教育教学\练习\1.cpp(13) : error C2676: binary '<<' : 'class istream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator
F:\教育教学\练习\1.cpp(18) : error C2065: 'q' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2146: syntax error : missing ';' before identifier 't'
F:\教育教学\练习\1.cpp(19) : error C2065: 't' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2065: 'h' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2065: 'f' : undeclared identifier
执行 cl.exe 时出错.

1.obj - 1 error(s), 0 warning(s)

像这些错误提示确实应该练练自己看,不然老因为一些小语法错误上来问是很耽误时间的~

binary '<<' : 'class istream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator
意义其实就是说既找不着合适的重载版,而且也无法通过自动类型转换匹配到预定义的版本上。
像你这个错的就很明显,cin是一个istream的对象,当然没有重载<<这个运算符了~

'q' : undeclared identifier
这是说,q是个没声明的标识符

syntax error : missing ';' before identifier 't'
这是,语法错误:在标识符t前缺少‘;’

其实大部分错误提示还是挺明显的,一般就算看不懂,它指出了有错的行。去仔细查一查自己也应该能查出错误。这是练基本功的好方法,要加油呀~
#7
shaozihaozi2009-07-20 15:06
这样可以
#include "iostream.h"
int max(int ,int );
void main()
{
   
    int a,b,t;
    char q;
    cout<<"请输入你想要比对的整数"<<endl;
    cin>>a>>b;
    cout<<"你输入的整数为"<<a<<"和"<<b<<"吗?"<<endl;
    cout<<"是的话请按Y,反之请按N"<<endl;
    cin>>q;
    if(q=='y'&&'Y')
        
        {
        t=max(a,b);
        cout<<"这2个整数中最大的数为"<<t<<endl;
        }

    else

    {
        cout<<"输入错误"<<endl;
    }
    
    
}
int max(int a,int b)
{
    if(a>=b)
        return a;
    else
        return b;
}
#8
iamchris19922011-07-05 10:11
看到这个帖子有个新问题:楼主的编译结果最后显示:
执行 cl.exe 时出错.

1.obj - 1 error(s), 0 warning(s)
我也遇到了这种问题,编译器的计数器显示的永远是这个值,问一下这算是个Bug么?是否有办法解决呢?
1