我在 C++ 中的一些代码有一个小问题它不会编译,我的编程技能现在还不是太高,如果你能帮助我,我将不胜感激。提前泰。
问题是错误在哪里,以及如何修复代码
#include<iostream>
using namespace std;
struct S {
S(int i){ this->x = i;}
~S(){}
int x;
static const int sci = 200;
int y = 999;
}
void main(){
S *ps = new S(300);
ps->x = 400;
S s;
s.x = 500;
}
编译器输出:
8 13 [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
9 1 [Error] expected ';' after struct definition
10 11 [Error] '::main' must return 'int'
In function 'int main()':
14 7 [Error] no matching function for call to 'S::S()'
14 7 [Note] candidates are:
4 7 [Note] S::S(int)
4 7 [Note] candidate expects 1 argument, 0 provided
3 8 [Note] S::S(const S&)
3 8 [Note] candidate expects 1 argument, 0 provided
======== 赛后 ;) ===========
代码:
#include<iostream>
using namespace std;
struct S {
S() : x() {} // default constructor
S(int i) : x(i) {} // non-default constructor
~S(){} // no need to provide destructor for this class
int x;
static const int sci = 200;
int y = 999; // Only valid since C++11
}; // ; after class definition.
int main(){
S *ps = new S(300);
ps->x = 400;
S s;
s.x = 500;
}
也:
#include<iostream>
using namespace std;
struct S {
S(int i){
this->x = i;
}
~S(){}
int x;
static const int sci = 200;
int y = 999;
};
int main(){
S *ps = new S(300);
ps->x = 400;
S *s = new S(20);
s->x = 500;
}
工作!TY to juanchopanza、Paul Renton 和所有抽出时间帮助我的人!