3

我正在尝试创建一个 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'
4

1 回答 1

3

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.

于 2013-03-27T03:35:34.530 回答