gpt4 book ai didi

c++ - 收到 C2628 错误

转载 作者:行者123 更新时间:2023-11-27 23:27:05 25 4
gpt4 key购买 nike

<分区>

考虑以下代码:

#include<iostream>
using namespace std;
class sample
{
int a;
int b;
public:
void setValue()
{
a=25;
b=40;
}
friend float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
}
int main()
{
sample x;

x.setValue();
cout<< "mean value:"<< mean(x)<<endl;

cin.ignore();
getchar();
return (0);
}

我希望在尝试编译和运行它时输出:

mean value: 32.5

但是,我得到了这些编译器错误:

1>  frndF.cpp
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(18): error C2628: 'sample' followed by 'int' is illegal (did you forget a ';'?)
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(19): error C3874: return type of 'main' should be 'int' instead of 'sample'
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(27): error C2664: 'sample::sample(const sample &)' : cannot convert parameter 1 from 'int' to 'const sample &'
1> Reason: cannot convert from 'int' to 'const sample'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(15): warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data

我做错了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com