C++ 代码中的可视错误以及如何修复它们
visual Errors in c++ code and how to fix them
我对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 感谢 juanchopanza、Paul Renton 和所有其他花时间帮助我的人!
由于您已经声明了一个非默认构造函数,编译器不再为您生成一个构造函数。因此,您需要提供一个默认的。这条线需要它。
S s;
您还应该使用构造函数初始化列表,如本带批注的示例所示
struct S {
S() : x() {} // default constrictor
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.
代码使用一个 C++11 功能,即在声明点初始化非静态数据成员。为此,您需要传递-std+c++11
标志。
此外,void main()
C++无效。 main()
必须返回int
.
编译器
错误也指出的几件事
必须使用分号完成结构声明,就像关闭类定义一样。
所以
struct S {
S(int i){ this->x = i;}
~S(){}
int x;
static const int sci = 200;
int y = 999;
}; // add semi colon
此外,您提供了一个构造函数,因此编译器不再为您生成默认构造函数。
所以 S s;无效,您需要提供默认构造函数,如果未给出默认值,则需要为 x 提供默认值。
struct S {
S(int i = 3){ this->x = i;} // Add a default value
~S(){}
int x;
static const int sci = 200;
int y = 999;
};
编辑:由 millsj 建议
也如果要在结构中设置默认值,则需要使用 -std=c++11 标志
int y = 999; // Only valid in C++11
最后
您希望返回带有 main 的 int,而不是 void。
int main() // not void main()
希望这一切都有所帮助!我们一度都是初学者。
#include<iostream>
using namespace std;
struct S {
S(int i){ this->x = i;}
~S(){}
int x;
static const int sci = 200;
int y = 999; //<-- c++11, use the -std=c++11 flag on the compiler command line.
}; //<-- need a semicolon
int main(){ //<-- main must return int
S *ps = new S(300);
ps->x = 400;
S s(500); //<-- must supply parameter since there is no default constructor.
s.x = 500;
delete ps; //<-- need to delete memory allocated with `new`
return 0; //<-- main must return int
}
这看起来像列出的错误。如果您修复这些问题并重新编译,可能会有更多。
相关文章:
- 如何组织 git 工作流以修复错误并同时引入新功能
- 如何修复错误 2298 和 2563
- 如何修复错误:使用向量查找最小值和最大值进行合并排序
- 修复错误的 JSON 数据
- 在Visual Code 2019中使用graphics.h绘制线条时,如何修复错误E0167和C2019?
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 视觉如何修复错误E0026 C
- 如何修复错误,迭代器未在此范围内声明,并且迭代器未命名类型'
- 如何在c ++中调整数组大小时修复错误?
- 如何修复错误分配C++运行时错误
- 如何修复错误C2259:无法实例化抽象类
- 视觉"如何修复错误 C4996:"_strupr":C++ 中的错误"
- 如何在两个设备之间与TCP通信时修复错误"连接被拒绝"
- 如何修复错误 LNK 2022?
- Microsoft Visual C++, Visual Studio, 如何修复错误: 标识符"GetAsyncKeyState"未定义
- 如何修复错误消息,如"argument list for class template is missing"?
- 如何修复错误"在c ++ UE4中为类似函数的宏调用'TEXT的参数过多
- 如何修复错误:g++.exe:无法使用-c或-S和多个编译指定-o
- 如何修复错误:请求从"void"转换为非标量类型"std::vector<int>"