Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot initialize a variable of type 'test_nested *' with an rvalue of type 'test_nested *' (aka 'test::test_nested *') using nested class, pointers

I’m trying to gain access to array’s in the *some_nested_array witch is a properties of private nested class, i would also like to delete a pointer of array of array’s.

error indation

#include <iostream>

class test_nested;
class test{
private:
    //public:
    class test_nested{
    public:
        unsigned char array_index;
        unsigned char *some_nested_array;
        test_nested():array_index(10), some_nested_array(new unsigned char[array_index]){
            std::cout << "test_nested created" << std::endl;
            // for(unsigned char i = 0; i != array_index; i++){
            //     //std::cout << "some_array[" << array_index << "]!" << &some_nested_array[i] << std::endl;
            //     std::cout << "some_array: " << &some_nested_array[i] << std::endl;
            // }
        };
        ~test_nested(){};
    };
    test_nested* test_nested_array;
public:
    test(): test_nested_array(new test_nested[10]){
        std::cout << "test created" << std::endl;
    }
    void remove_Item(unsigned char index){

        //free(&this->test_nested_array[index]);
        //delete &this->test_nested_array[index];
        //this->test_nested_array[index] = NULL;
    }
    ~test(){
        delete[] test_nested_array;
    }

    //test_nested(* connect(unsigned char index)){
    test_nested* connect(unsigned char index){
        return &this->test_nested_array[index];
    }
};

int main(int argc, char *argv[])
{
    test A;
    test_nested *temp = A.connect(6); // error: Cannot initialize a variable of type 'test_nested *' with an rvalue of type 'test_nested *'

    // for(unsigned char i = 0; i != array_index; i++){
    //     //std::cout << "some_array[" << array_index << "]!" << &some_nested_array[i] << std::endl;
    //     std::cout << "some_array: " << &some_nested_array[i] << std::endl;
    // }
    A.remove_Item(9);
    //delete A;
    QCoreApplication a(argc, argv);

    return -1;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have two different declarations of the test_nested structure.

The first is the one you declare in the global scope, which is really named ::test_nested. Then there’s the one you declare inside the test class, which has the name ::test::test_nested (or just test::test_nested for short).

In the main function you use ::test_nested, not test::test_nested, which leads top your error.

Use the correct structure and the error should disappear.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading