Search code examples
c++pointersshared-ptr

C++ shared pointer issue


I'm trying to create a set function that will take in a shared pointer and set it equal to another shared pointer.

This is the shared pointer and the set function that i have declared on my header file

class Shape
{
public:
    Shape();
    Gdiplus::Point start;   
    Gdiplus::Point end;

    std::shared_ptr<Gdiplus::Pen> m_pen;

    virtual  LRESULT Draw(Gdiplus::Graphics * m_GraphicsImage) = 0;

    void setPen(std::shared_ptr<Gdiplus::Pen> pen2);

    void setStart(int xPos, int yPos);
    void setEnd(int xCor, int yCor);
};

but when i try to implement it in my cpp i get an error saying that my "declaration is incompatible with void setPen declared on .h". it also says m_pen is unedintified on my cpp file.

#include<memory>
 #include "stdafx.h"
#include "resource.h"

  #include "ShapeMaker.h"
void Shape::setPen(std::shared_ptr<Gdiplus::Pen> pen2)
{
    m_pen = pen2;
}

void Shape::setStart(int xPos, int yPos)
{
    start.X = xPos;
    start.Y = yPos;
}


void Shape::setEnd(int xCor, int yCor)
{
    end.X= xCor;
    end.Y = yCor;
}

That's literally all i have. the stdax.h includes

  #include <atlwin.h>

  #include <atlframe.h>
  #include <atlctrls.h>
  #include <atldlgs.h>
  #include <atlctrlw.h>
  #include <atlscrl.h>

  #include <GdiPlus.h>

Errors that I get:

shapemaker.h(11): error C2039: 'shared_ptr' : is not a member of 'std'

shapemaker.h(11): error C2143: syntax error : missing ';' before '<'

shapemaker.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

shapemaker.h(11): error C2238: unexpected token(s) preceding ';'
.h(16): error C2039: 'shared_ptr' : is not a member of 'std'

shapemaker.h(16): error C2061: syntax error : identifier 'shared_ptr'
shapemaker.cpp(9): error C2511: 'void Shape::setPen(std::tr1::shared_ptr<_Ty>)' : overloaded member function not found in 'Shape'

Solution

  • I'm posting my answer from the comments for any visitors.

    The problem is at the beginning of the cpp file.

    #include<memory>
     #include "stdafx.h"
    #include "resource.h"
    
      #include "ShapeMaker.h"
    

    MSVC++ demands that the precompiled header "stdafx.h" precede any other code in your source files.

    The #include<memory> must be removed, and instead placed in "ShapeMaker.h" where it is first needed.