如何解决头文件中的类给出很多错误
我想创建一个类库,以便可以在我的代码中使用它。这就是为什么我将班级拆分为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
,但是如果不支持#pragma
s 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 举报,一经查实,本站将立刻删除。