As far as I know, it should not compile fine, since there are several issues.
1) Do not ask about not compilable code. The compiler is very capable at spotting errors: do rely on it.
Your program doesn’t compile because it misses a main(). Add a main() and your compiler makes you happy by spotting your errors for you.
2)
https://en.cppreference.com/w/cpp/language/default_arguments
Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions |
They should not be admitted in function definitions, so this line
Elemento::Elemento(int v = 0)
should rise an error.
For example, adding:
1 2 3 4
|
int main()
{
Elemento e;
}
|
we can get:
main.cpp: In function 'int main()':
main.cpp:7:14: error: no matching function for call to 'Elemento::Elemento()'
7 | Elemento e;
| ^
In file included from main.cpp:1:
Elemento.hpp:27:5: note: candidate: 'Elemento::Elemento(int)'
27 | Elemento(int valor_arg);
| ^~~~~~~~
Elemento.hpp:27:5: note: candidate expects 1 argument, 0 provided |
Whereas adding:
1 2 3 4
|
int main()
{
Elemento e(1);
}
|
we get:
ld.exe: main.cpp:(.text+0x1d): undefined reference to `Elemento::Elemento(int)'
|
The solution is:
1 2 3 4 5 6 7
|
class Elemento {
public:
Elemento(int valor_arg = 0);
};
Elemento::Elemento(int valor_arg)
{
|
3)
There’s no way for the compiler to tell these function apart when you call f1() without any argument:
1 2 3 4
|
void f();
void f(int val = 0);
f(); // which one?
|
So you need to choose either
Elemento(); or
Elemento(int valor_arg = 0). You shouldn’t keep both.
4) NULL
Don’t use it any more. Do use
nullptr.
5)
1 2
|
using namespace std;
using std::vector;
|
No point in adding the second line (and I hardly can work out a reason to add the first one).
6) You forgot to include <string>.
7) Take advantage of the initializer lists:
https://en.cppreference.com/w/cpp/language/initializer_list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
Elemento::Elemento(int valor_arg)
: valor { valor_arg }
, dir { nullptr }
, esq { nullptr }
{
}
Elemento::Elemento()
: p1 { nullptr }
, p2 { nullptr }
, p3 { nullptr }
, p4 { nullptr }
, p5 { nullptr }
, p6 { nullptr }
, p7 { nullptr }
, p8 { nullptr }
, p9 { nullptr }
{
}
|
Or you can initialize your variables in class declaration, which is currently the suggested method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Elemento {
public:
int valor {};
Elemento * dir { nullptr }
, * esq { nullptr }
, * p1 { nullptr }
, * p2 { nullptr }
, * p3 { nullptr }
, * p4 { nullptr }
, * p5 { nullptr }
, * p6 { nullptr }
, * p7 { nullptr }
, * p8 { nullptr }
, * p9 { nullptr };
|
8)
C.46: By default, declare single-argument constructors explicit |
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit
Here’s an example of working code — there’re likely issues in this, too :-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
#include <vector>
class Elemento {
public:
int valor {};
Elemento * dir { nullptr }
, * esq { nullptr }
, * p1 { nullptr }
, * p2 { nullptr }
, * p3 { nullptr }
, * p4 { nullptr }
, * p5 { nullptr }
, * p6 { nullptr }
, * p7 { nullptr }
, * p8 { nullptr }
, * p9 { nullptr };
std::vector<std::string> palavras;
explicit Elemento(int valor_arg = 0);
};
Elemento::Elemento(int valor_arg)
: valor { valor_arg }
{
}
int main()
{
Elemento e;
}
|