Buggy code

What is wrong in below code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class NonZeroSample{
protected:
          virtual intnxt = 0;
public:
         
       int nxtNonZero() {
       int value;
       do {
            value = this->intnxt();
       }while(value ==0);
       return value;
    }
};


            
Nigh um-possible to say what is wrong with that snippet, it is without any context of application. It would need a crap load of framework code to be written to figure out what, if anything, is wrong.

1. A Short, Self Contained, Correct (Compilable), Example sure would help.
http://www.sscce.org/

2. "what is wrong" is not very helpful at all. You want help you need to do more than that. Lots more.
http://www.gregcons.com/KateBlog/HowToAskForCCodingHelp.aspx
c++ does not have virtual variables, does it? I am not up on the new 20 deep details, but last I heard, that was not a thing. It also lacks a type, assuming you wanted an int on it?

you can fake a virtual variable by providing getter/setter that are virtual and overriding what they do up the inheritance chain.

it looks like you are trying to do some sort of linked list weirdness. Details are required...
Last edited on
Lack of coffee? It cannot work because of the infinite loop and some errors which disallow compilation ++

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
#include <iostream>

class NonZeroSample {

protected:
    static const int intnxt = 0;

public:
    int nxtNonZero() {
        int value;
        // infinite loop
        do {
            value = this->intnxt;
            std::cout << "loop" << std::endl;
        } while (value == 0);
        // never reached
        return value;
    }
};

int main()
{
    NonZeroSample *sample = new NonZeroSample();
    std::cout << "output " << sample->nxtNonZero() << std::endl;
    delete sample;
}


Change the value of the integer intnxt (line 6) in order to see the output. In your code, while the integer is equal to zero, it runs without breaking. So you create an infinite loop :/
Last edited on
Thanks to clarify this. This thread can be closed.
Topic archived. No new replies allowed.