2013-03-27 137 views
1

我试图创建一个设置函数,它将采用共享指针并将其设置为等于另一个共享指针。C++共享指针问题

这是共享指针和我有我的头文件中声明所设定的功能

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' 
+2

你能展示这个实现吗? – 2013-03-27 01:50:25

+1

实际上看到它正在抱怨的行是很方便的,以便我们可以将它与声明进行比较。 – chris 2013-03-27 01:50:26

+0

那里它去的家伙。对不起,那 – user1665569 2013-03-27 01:52:38

回答

2

我从任何游客评论张贴我的答案。

问题出在cpp文件的开头。

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

    #include "ShapeMaker.h" 

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

必须删除#include<memory>,而是将其放置在首先需要的"ShapeMaker.h"