头文件中的类给出很多错误

如何解决头文件中的类给出很多错误

我想创建一个类库,以便可以在我的代码中使用它。这就是为什么我将班级拆分为2个文件-s_int.h和s_int.cpp,但是编译产生了太多与该班级相关的错误。通常我会看到一些与构造函数和析构函数错误有关的“匿名聚合”。这是我的代码:


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

将使编译器将类名s_int替换为空字符串,并会引起麻烦。

使用其他名称来定义包含保护。

使用

#pragma once

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

同样,您必须按照@NathanOliver的说法,将#include "s_int.h"添加到main.cpp

,

Macros are very simple text substitution。无论在何处都可以找到宏的标识符,无论该标识符是否有意义,标识符都将替换为该标识符之后的所有内容。所以当你

#ifndef s_int 

无论您在代码中有s_int的哪个地方,它都不会被替换。这意味着

s_int integer;

变成

integer; 

,编译器不知道该怎么办。

class s_int

成为

class

创建一个未命名的类定义。如果您尝试使用现在无法命名的类,则会发生混乱。

您需要确保仔细使用宏。因此,许多编码约定要求宏必须全部大写才能突出显示。

对于Include Guard,防护罩必须完全唯一。如果在代码中重复使用标识符,则会得到在此代码中看到的某种意外的文本替换。如果您有多个标头由相同的标识符保护,则第一次使用防护将阻止其他所有使用此标头的标头。这是一个非常糟糕的场面。

可以用

缓解
#pragma once

如果您的编译器支持once,并且大多数主流编译器都支持once,但是如果不支持#pragmas are allowed to be silently discarded,那么您甚至可能不知道标头不受保护。此外,sufficiently complicated project structures can fool once

,

您是否考虑过将#include“ s_int.h”放在主类中?

,

我对您的代码进行了很少的更改。
我删除了不必要的引用,这些引用不是必需的,例如对 = 运算符进行重载,我们不需要它,因为我们不打算这样做更改给定的参数。
我们无需初始化 string整数; ,因为我们为其实现了构造函数。
您还应该考虑如何在头文件中定义宏 #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编写类。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-