I wanted to make a class library so that I can use it across my codes. That's why i splitted my class in 2 files - s_int.h and s_int.cpp But the compilation is giving too many errors related to the class. generally i see some 'anonymous aggregate' related to constructors and destructors errors. Here is my code :
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
compilation errors (in case needed)
$ 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 )
| ^