c++共享指针问题
C++ shared pointer issue
我试图创建一个集合函数,将在一个共享指针,并设置它等于另一个共享指针。
这是我在头文件中声明的共享指针和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"
中,这是首先需要的。
相关文章:
- 关于 c++ 函数中指针赋值的简单问题
- 链表指针问题
- C++ 关于指针取消引用的技术问题
- C++中的指针和常量问题不大
- 包含矢量指针的结构的内存释放问题
- 指针问题:从不兼容的类型"int"分配给"int *"
- 将字节数组转换为带有字节序问题的指针
- 关于如何使用指向主窗口的指针的 QT 问题
- 当成员值从指针更改为非指针时,C++常量问题
- 为什么循环会导致指针出现问题?
- 使用指针计算堆栈问题的大 O 表示法
- 构造函数 (C++) 中的 char 指针参数存在问题
- 指向包含对齐 C 结构C++类的 C 指针的对齐问题
- 涉及指针和手动实现的矩阵类的问题
- 从基指针到派生的强制转换问题
- 迭代器的指针操作问题
- 64 位迁移问题:指针更改
- C++模板使用问题指针
- 跳到C++第13章练习问题4-指针
- 用std::pair数组初始化std::map问题(指针错误?)