c++共享指针问题

C++ shared pointer issue

本文关键字:问题 指针 共享 c++      更新时间:2023-10-16

我试图创建一个集合函数,将在一个共享指针,并设置它等于另一个共享指针。

这是我在头文件中声明的共享指针和set函数

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);
};

但是当我试图在我的cpp中实现它时,我得到一个错误,说我的"声明与。h上声明的void setPen不兼容"。它还说m_pen在我的CPP文件中未被指定。

#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;
}

这就是我所有的了。stdax.h包含

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

我得到的错误:

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'

我把我的答案从评论中贴出来给任何访问者。

问题在cpp文件的开头。

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

msvc++要求预编译头文件"stdafx.h"在源文件中的任何其他代码之前。

必须移除#include<memory>,并将其放置在"ShapeMaker.h"中,这是首先需要的。