错误:在"void"之前预期初始化器(如何修复?



我在为竞争性编程课程编写代码时遇到了这个问题,但我遇到了关于void的问题:

代码1:在第一个代码中,它可以正确执行。void不需要初始化,一切都很好。

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#define ll long long
#define ar array
using namespace std;
int n; int b;int a[100000];
void solve(){
cin >> n >> b;
for(int i =0;i <n;++i){
cin >> a[i];
}
sort(a,a+n);
int ans=0;
for(int i=0;i < n;++i){
if(b>=a[i]){
b-=a[i];
++ans;
} 
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;int e=1;
cin >> t;
while(t--){
cout << "Case #" << e <<":";
solve();
}
}
Output:
Success

但在另一个代码上,void需要初始化,当我在main中调用它时,它没有在scope上声明。代码有问题吗?或者我需要重新下载gcc?代码2:

#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#define ll long long
#define ar array
int a; int b;int c[10000]
void solution(){
cin >> a >> b;
string f;
cin >> f;
while(a--){
string l;int x; int y; int z;
cin >> l >> x >> y >> z;
cout << l << x << y << z endl;
}
}
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
solution();
}
}

输出:

seleksi_olimpiade.cpp:7:1: error: expected initializer before 'void'
7 | void solution(){
| ^~~~
seleksi_olimpiade.cpp: In function 'int main()':
seleksi_olimpiade.cpp:23:5: error: 'solution' was not declared in this scope
23 |     solution();
|     ^~~~~~~~

您在int a; int b;int c[10000]行中缺少一个分号

最新更新