1

I came across the following error like below during my project. This error is reproduced with some dummy code for better understanding.

main.cpp:15:7: error: ‘class NameB’ has pointer data members [-Werror=effc++]
 class NameB {
       ^
main.cpp:15:7: error:   but does not override ‘NameB(const NameB&)’ [-Werror=effc++]
main.cpp:15:7: error:   or ‘operator=(const NameB&)’ [-Werror=effc++]

I got the error when I build the following code using the command g++ main.cpp -Weffc++ -Werror

The code is

#include <iostream>

using namespace std;

class NameA {
    private:
        int m_item;
        int m_dist;

    public:
        NameA(int a, int b): m_item(a), m_dist(b) {}
        // NameA(const NameA& namea);
};

class NameB {
    private:
        NameA*  m_namea;
        int     m_int;

    public:
        NameB():m_namea(NULL), m_int() {}
        ~NameB();
};

int main(int argc, char *argv[]) {
    NameB *nb = new NameB();
    if(nb) {
        // suppress unused warning
    }
}

If I remove the destructor from the class NameB, the error has vanished. So could anyone please help me to get a clarification on this?

shafeeq
  • 1,499
  • 1
  • 14
  • 30
  • 1
    Your class violates the rule of 3: [https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – drescherjm Dec 14 '21 at 15:34
  • `if(nb) {` -- This test will always be `true`. An error on `new` throws an exception, not set the value to `nullptr`. If you want that behavior, then the `nothrow` attribute needs to be used. – PaulMcKenzie Dec 14 '21 at 15:37
  • @PaulMcKenzie, That is just a dummy. The core part of the question is how to address the error shown. – shafeeq Dec 14 '21 at 15:39
  • 1
    [Cannot duplicate](http://coliru.stacked-crooked.com/a/f11712be35d9d6bb). Don't post dummy code, if this is not the real code being compiled. – PaulMcKenzie Dec 14 '21 at 15:40
  • This code will compile in Linux with the command shown in the question. I got the correct answer from stackoverflow.com/questions/4172722/what-is-the-rule-of-three. Thanks – shafeeq Dec 14 '21 at 15:42

0 Answers0