c++ - 头文件中的类给出许多错误
问题描述
我想制作一个类库,以便我可以在我的代码中使用它。这就是为什么我将我的课程分成 2 个文件 - s_int.h 和 s_int.cpp 但是编译给出了太多与课程相关的错误。通常我会看到一些与构造函数和析构函数错误相关的“匿名聚合”。这是我的代码:
主.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
相反,如果您的环境支持此功能也很好。
你也必须#include "s_int.h"
像main.cpp
@NathanOliver 所说的那样添加。
推荐阅读
- jquery - JQuery - 将 AJAX 请求的响应复制到剪贴板,无需用户再次按下按钮
- javascript - 电子打字稿:捆绑一切
- c# - error System.FormatException: '输入字符串的格式不正确。'
- c# - RichTextBox字体更改c#
- vhdl - 组件声明的用处是什么?
- python - ImportError:无法导入名称“imshow”
- javascript - PrimeNG是在你使用的时候全部进口的,还是摇摇晃晃的?我怎样才能自己确定呢?
- apache - Q: 找不到 Ngrok 配置文件
- angular - Angular:如何从自定义装饰器中获取 ElementRef
- javascript - Hide a ',' comma that repeats multiple times on a page