C++ 代码中的可视错误以及如何修复它们

visual Errors in c++ code and how to fix them

本文关键字:何修复 错误 代码 可视 C++      更新时间:2023-10-16

我对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
}

这看起来像列出的错误。如果您修复这些问题并重新编译,可能会有更多。