cp420 - 1 year ago 77

C++ Question

I have a number class here that works properly:

number.hpp

`#ifndef NUMBER_HPP`

#define NUMBER_HPP

#include <memory>

class Number

{

private:

std::unique_ptr<int[]> mDigits;

public:

// CONSTRUCTORS \\

Number();

};

#endif

number.cpp

`#include "number.hpp"`

#define PRECISION 2048

Number::Number()

:mDigits( new int[PRECISION]() )

{

}

When I add the following operators

number.hpp

`#ifndef NUMBER_HPP`

#define NUMBER_HPP

#include <memory>

class Number

{

private:

std::unique_ptr<int[]> mDigits;

public:

// CONSTRUCTORS \\

Number();

// CONST OPERATORS \\

bool operator==( Number const& rhs ) const;

bool operator!=( Number const& rhs ) const;

};

#endif

number.cpp

`#include "number.hpp"`

#define PRECISION 2048

Number::Number()

:mDigits( new int[PRECISION]() )

{

}

bool Number::operator==( Number const& rhs ) const

{

for( int i = 0; i < PRECISION; ++i )

if( mDigits[i] != rhs.mDigits[i] )

return false;

return true;

}

bool Number::operator!=( Number const& rhs ) const

{

return !( *this == rhs );

}

I get the following error from GCC 5.4, GCC 6.2, and CLANG idk

`number.cpp:5:16: error: definition of implicitly declared constexpr Number::Number()`

Number::Number()

error: number.cpp:12 no bool Number::operator==( const Number& rhs ) const member function declared in class Number

And so on for every method in the class. What is happening here?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
public:
// CONSTRUCTORS \\
Number();
// CONST OPERATORS \\
bool operator==( Number const& rhs ) const;
bool operator!=( Number const& rhs ) const;
```

The preprocessor removes all occurrences of backslash-newline (i.e. `\`

at the end of a line) very early in processing. What you end up with is:

```
public:
// CONSTRUCTORS \ Number();
// CONST OPERATORS \ bool operator==( Number const& rhs ) const;
bool operator!=( Number const& rhs ) const;
```

Which is then parsed as two comments and one declaration,

```
bool operator!=( Number const& rhs ) const;
```

Solution: Don't use `\`

as the last character in a line. Just write `// CONSTRUCTORS`

or `// CONST OPERATORS`

.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**