C++ 共享ptr和错误C4430:缺少类型说明符-假定为int-C++;
除其他错误外,编译器还指出了字符串(强调)。我是否使用了共享\u ptrs'错误?编译器是否能够通过这种非显式的方式进入private,以设置值 stack_class.hC++ 共享ptr和错误C4430:缺少类型说明符-假定为int-C++;,c++,shared-ptr,C++,Shared Ptr,除其他错误外,编译器还指出了字符串(强调)。我是否使用了共享\u ptrs'错误?编译器是否能够通过这种非显式的方式进入private,以设置值 stack_class.h #pragma once//(我也尝试过没有这个) #包括 类堆栈 { 私人: 共享\u ptr n;//此字符串 公众: 堆栈(); }; stack_class.cpp /\include's 使用名称空间std; Stack::Stack() { 共享用户(新整数); cin>>*n_用户; 共享ptr n(新整数)
#pragma once//(我也尝试过没有这个)
#包括
类堆栈
{
私人:
共享\u ptr n;//此字符串
公众:
堆栈();
};
stack_class.cpp
/\include's
使用名称空间std;
Stack::Stack()
{
共享用户(新整数);
cin>>*n_用户;
共享ptr n(新整数);
此->n=n\u用户;
}
您的头文件需要如下所示:
#pragma once
#include <memory> // ADDED
class Stack
{
private:
std::shared_ptr<int> n; // ADDED std::
int *s_array;
std::shared_ptr<int> amount; // ADDED std::
public:
Stack();
void Push(int value);
int Get(int receiver);
~Stack();
};
#pragma一次
#包括//已添加
类堆栈
{
私人:
std::shared_ptr n;//添加的std::
int*s_数组;
std::shared_ptr amount;//添加的std::
公众:
堆栈();
无效推送(int值);
int-Get(int-receiver);
~Stack();
};
#包含
和std::shared_ptr
。请发布完整的错误消息,同时确保您的代码是@user2952487,您在cpp文件中使用“using namespace std”,但不在标题中(在包含标题之前两者都不包括)@user2952487:您不能在头文件的顶层使用名称空间说。我写的方式是正确的。胡安科潘扎,你的闲话对我没有帮助。皮特和约翰·兹温克帮了忙。
//#include's
using namespace std;
Stack::Stack()
{
shared_ptr<int> n_user(new int);
cin >> *n_user;
shared_ptr<int> n(new int);
this->n = n_user;
}
#pragma once
#include <memory> // ADDED
class Stack
{
private:
std::shared_ptr<int> n; // ADDED std::
int *s_array;
std::shared_ptr<int> amount; // ADDED std::
public:
Stack();
void Push(int value);
int Get(int receiver);
~Stack();
};