C++共享指针问题

3

我将尝试创建一个设置函数,该函数将接受一个 shared pointer 并将其设置为另一个 shared pointer。

这是我在头文件中声明的 shared pointer 和设置函数:

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不兼容”。它还说我的cpp文件中的m_pen未定义。

#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'

2
你能展示一下这个实现吗? - Drew Dormann
2
那么,m_pen和setPen是Shape类的成员吗? - Kijewski
2
你有包含 <memory><gdiplus.h> 吗? - chris
4
现在我们得到了答案。实际错误很重要。在那个头文件中你漏掉了 #include <memory> - Drew Dormann
2
@user1665569 这是个好消息!如果你再次遇到编译问题,你应该把第一个、完整、准确的错误信息放在首位。 - Drew Dormann
显示剩余14条评论
1个回答

3

我将我的回答从评论中发布出来,供任何访客参考。

问题出在cpp文件的开头。

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

  #include "ShapeMaker.h"

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

#include<memory>必须被移除,并放置在"ShapeMaker.h"中第一次需要它的地方。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接