mercredi 4 mai 2016

Cannot convert objects to pointers C++

I've stayed up all night trying to figure this out (it's now 7am where I'm at...).

I'm having trouble setting the address of an instantiated object to a pointer. Here's the main function:

#include "position_vector.h"
int main(){
    PositionVector res = PositionVector(10);
    PositionVector * ptr;
    ptr = &res;                   // <--- WHERE IT BREAKS
}

A stripped down version of the h file "position_vector.h":

#include<iostream>

typedef uint32_t word_t;
class PositionVector {
    public:
        word_t * vec;
/*some other member variables */
        PositionVector();
        PositionVector(size_t len);
        PositionVector & operator & ();
        PositionVector & operator !();
        ~PositionVector();

/*some other member functions*/
        void resize(size_t len);
};

I have another cpp file that defines all the methods in the class.

This is part of some larger set of code but here's the compile that fails:

g++-4.9 -std=c++11 -Werror -Wall -Wextra -g -Isrc -ggdb  -c -o bin/main.o src/main.cpp

It fails with the error:

g++-4.9 -std=c++11 -Werror -Wall -Wextra -g -Isrc -ggdb  -c -o bin/main.o src/main.cpp
src/main.cpp: In function ‘int main()’:
src/main.cpp:27:9: error: cannot convert ‘PositionVector’ to ‘PositionVector*’ in assignment
     ptr = &res;
         ^

I must be missing something super basic but I've just pulled an all nighter and I have to run to work... so I cant really think full well any more.

Aucun commentaire:

Enregistrer un commentaire