3

Here is the code:

#include <iostream>
using namespace std;

template<class OwnerType>
class Move {
public:
    Move() {}
    Move(OwnerType &_owner) {
        owner = &_owner;
    }
    void GetPosition() {
        cout << owner->x << endl;
    }
    OwnerType *owner;
};

class Entity {
public:
    int x = 50;
    Move<Entity> *move;
};


int main() {        
    Entity en; 
    en.x = 77;
    en.move = new Move<Entity>(en);   // sign '=' is underlined by VS
    en.move->GetPosition();
    return 0;
}

Error it gives :

a value of type "Move<Entity> *" cannot be assigned to an entity of type "Move<Entity> *"

The program is compiling, working as expected and gives expected values, however error is still here. It's probably something to do with templates and compiling time and stuff but I don't have enough knowledge to know what this error actually represents.

Also don't worry about leaks since this was just me testing, error is what I don't understand.

Thanks in advance.

buld0zzr
  • 962
  • 5
  • 10
Drunk Bearzz
  • 55
  • 1
  • 6
  • 3
    Don't trust the intellisense. Actually compile. – πάντα ῥεῖ Jul 15 '16 at 10:00
  • 1
    [OT]: Your program leaks. – Jarod42 Jul 15 '16 at 10:02
  • `int main { ` is that in your actual code? Misses the `()`. – Baum mit Augen Jul 15 '16 at 10:03
  • This was made just so I can test if something can be done so I didn't care much about deleting pointers, also I forgot () when copy-pasting somehow I'll correct it, but problem is error itself. – Drunk Bearzz Jul 15 '16 at 10:09
  • Possible duplicate of [Error 'a value of type "X \*" cannot be assigned to an entity of type "X \*"' when using typedef struct](http://stackoverflow.com/questions/15595449/error-a-value-of-type-x-cannot-be-assigned-to-an-entity-of-type-x-when) –  Jul 15 '16 at 10:22

2 Answers2

1

so it is not Error.

it is Intellisense : see:
Error 'a value of type "X *" cannot be assigned to an entity of type "X *"' when using typedef struct

Visual Studio 2015: Intellisense errors but solution compiles


old:

your main needs ():

this works for me:

#include<iostream>
using namespace std;

template<class T> class Move {
public:
    Move() {}
    Move(T &_owner) {
        owner = &_owner;
    }
    void GetPosition() {
        cout << owner->x << endl;
    }
    T *owner;
};

class Entity {
public:
    int x = 50;
    Move<Entity> *move;
};


int main(){
    Entity en;
    en.x = 77;
    en.move = new Move<Entity>(en);   // sign '=' is underlined by VS
    en.move->GetPosition();

    return 0;
}

output:

77
Community
  • 1
  • 1
1

Intellisense is known for displaying invalid errors (see for example Visual Studio 2015: Intellisense errors but solution compiles), trust the compiler and linker, as suggested in comments.

However, this error is quite annoying, try closing the solution, delete the .suo file (it's hidden), and open in again. More info on what a .suo file is given here Solution User Options (.Suo) File

Side note, in your code example main is missing ().

Community
  • 1
  • 1
buld0zzr
  • 962
  • 5
  • 10