头文件中的 class 给出了许多错误

A class in header file giving many errors

我想制作一个 class 库,以便我可以在我的代码中使用它。这就是为什么我将我的 class 分成 2 个文件 - s_int.h 和 s_int.cpp 但是编译给出了太多与 class 相关的错误。通常我会看到一些 'anonymous aggregate' 与构造函数和析构函数相关的错误。这是我的代码:


main.cpp :

#include "s_int.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
    s_int integer;
    integer = "11111111111111111111111111111111111";
    s_int integer1(integer);
    cout << integer.integer << "\n";
    cout << integer1.integer << "\n";
}

s_int.cpp

#include <bits/stdc++.h>
#include "s_int.h"
using namespace std;
 
    s_int::s_int(std::string inp) 
    {
        this-> integer = inp;
    }
    s_int::s_int(const s_int &inp) 
    {
        this-> integer = inp.integer;
    }
    void s_int::operator = (const string &inp )
    {
        integer = inp;
    }
    void s_int::operator = (const s_int &inp )
    {
        integer = inp.integer;
    }

s_int.h

#include <bits/stdc++.h>
#ifndef s_int
#define s_int
using namespace std;
class s_int
{
    public:
        string integer = "";
        s_int(std::string inp = "") ;
        s_int(const s_int &inp) ;
        void operator = (const string &inp );
        void operator = (const s_int &inp );
};
#endif

编译错误(以备不时之需)

$ g++ main.cpp s_int.h s_int.cpp
main.cpp: In function ‘int main()’:
main.cpp:7:11: error: ‘integer’ was not declared in this scope
    7 |     s_int integer;
      |           ^~~~~~~
main.cpp:9:11: error: ‘integer1’ was not declared in this scope
    9 |     s_int integer1(integer);
      |           ^~~~~~~~
s_int.h:9:26: error: expected ‘)’ before ‘inp’
    9 |         s_int(std::string inp = "") ;
      |              ~           ^~~~
      |                          )
s_int.h:10:15: error: expected unqualified-id before ‘const’
   10 |         s_int(const s_int &inp) ;
      |               ^~~~~
s_int.h:10:15: error: expected ‘)’ before ‘const’
   10 |         s_int(const s_int &inp) ;
      |              ~^~~~~
      |               )
s_int.h:13:33: error: ‘inp’ has not been declared
   13 |         s_int operator + (s_int inp );
      |                                 ^~~
s_int.h:8:16: error: member ‘std::string <unnamed class>::integer’ with constructor not allowed in anonymous aggregate
    8 |         string integer = "";
      |                ^~~~~~~
s_int.h:8:16: error: member ‘std::string <unnamed class>::integer’ with destructor not allowed in anonymous aggregate
s_int.h:8:16: error: member ‘std::string <unnamed class>::integer’ with copy assignment operator not allowed in anonymous aggregate
s_int.h:14:2: error: abstract declarator ‘<unnamed class>’ used as declaration
   14 | };
      |  ^
In file included from s_int.cpp:2:
s_int.h:9:26: error: expected ‘)’ before ‘inp’
    9 |         s_int(std::string inp = "") ;
      |              ~           ^~~~
      |                          )
s_int.h:10:15: error: expected unqualified-id before ‘const’
   10 |         s_int(const s_int &inp) ;
      |               ^~~~~
s_int.h:10:15: error: expected ‘)’ before ‘const’
   10 |         s_int(const s_int &inp) ;
      |              ~^~~~~
      |               )
s_int.h:13:33: error: ‘inp’ has not been declared
   13 |         s_int operator + (s_int inp );
      |                                 ^~~
s_int.h:8:16: error: member ‘std::string <unnamed class>::integer’ with constructor not allowed in anonymous aggregate
    8 |         string integer = "";
      |                ^~~~~~~
s_int.h:8:16: error: member ‘std::string <unnamed class>::integer’ with destructor not allowed in anonymous aggregate
s_int.h:8:16: error: member ‘std::string <unnamed class>::integer’ with copy assignment operator not allowed in anonymous aggregate
s_int.h:14:2: error: abstract declarator ‘<unnamed class>’ used as declaration
   14 | };
      |  ^
s_int.cpp:5:17: error: expected id-expression before ‘(’ token
    5 |     s_int::s_int(std::string inp)
      |                 ^
s_int.cpp:9:17: error: expected id-expression before ‘(’ token
    9 |     s_int::s_int(const s_int &inp)
      |                 ^
s_int.cpp:13:47: error: ‘void operator=(const string&)’ should have been declared inside ‘::’
   13 |     void s_int::operator = (const string &inp )
      |                                               ^
s_int.cpp:13:15: error: ‘void operator=(const string&)’ must be a nonstatic member function
   13 |     void s_int::operator = (const string &inp )
      |               ^~
s_int.cpp:17:46: error: ‘void operator=(const int&)’ should have been declared inside ‘::’
   17 |     void s_int::operator = (const s_int &inp )
      |                                              ^
s_int.cpp:17:15: error: ‘void operator=(const int&)’ must be a nonstatic member function
   17 |     void s_int::operator = (const s_int &inp )
      |               ^~
s_int.cpp:21:29: error: expected constructor, destructor, or type conversion before ‘(’ token
   21 |     s_int s_int::operator + (s_int inp )
      |                             ^
#define s_int

会让编译器将 class 名称 s_int 替换为空字符串,这会导致麻烦。

使用另一个名称来定义包含防护。

正在使用

#pragma once

如果您的环境支持此功能,也很好。

您还必须将 #include "s_int.h" 添加到 main.cpp,正如@NathanOliver 所说。

你有没有想过把#include "s_int.h" 放在主 class 中?

Macros are very simple text substitution。凡是找到宏标识符的地方,无论这样做是否有意义,标识符都会被标识符后面的任何内容替换。所以当你

#ifndef s_int 

代码中任何有 s_int 的地方,它都被替换为空。也就是说

s_int integer;

变成

integer; 

编译器不知道如何处理它。

class s_int

变成

class

进行未命名的 class 定义。如果您尝试使用 now-nameless class.

就会出现混乱

您需要确保谨慎使用宏。因此,许多编码约定要求宏全部为大写,以使其脱颖而出。

Include Guard 的情况下,守卫必须是完全唯一的。如果您在代码中重复使用该标识符,就会出现您在此代码中看到的那种意想不到的文本替换。如果你有多个 headers 由相同的标识符保护,第一次使用保护将阻止所有其他 headers 也使用它。这是一个非常糟糕的场景。

可以通过

缓解
#pragma once

如果您的编译器支持 once,大多数主要编译器都支持 once,但如果不支持 #pragmas are allowed to be silently discarded, so you may not even know your headers aren't protected. In addition, sufficiently complicated project structures can fool once

我对你的代码做了一些改动。
我删除了不需要的不必要的引用,例如 = 运算符的重载,我们不需要因为我们不打算更改给定的参数。
我们不需要初始化 string integer;当我们为它实现构造函数时。
您还应该考虑,我们如何定义宏 #ifndef#define 在头文件中。
结合你的头文件和它的方法实现。 s_int.h

#include <bits/stdc++.h>
#ifndef S_INT_H
#define S_INT_H
using namespace std;
class s_int
{
    public:
        string integer;
        s_int(std::string inp = "")
        {
            this->integer = inp;
        }
        s_int(const s_int *inp)
        {
            integer = inp->integer;
        }
        void operator=(const string inp)
        {
            this->integer = inp;
        }
        void operator= (const s_int inp)
        {
            this->integer = inp.integer;
        }
};
#endif

main.cpp

#include "s_int.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
    s_int integer;
    integer = "11111111111111111111111111111111111";
    s_int integer1(integer);
    cout << integer.integer << "\n";
    cout << integer1.integer << "\n";
}

希望对你有所帮助,你想用正确的方式在cpp中写类。