我有这个代码片段:
void addLineRelative(LineNumber number, LineNumber relativeNumber) {
list<shared_ptr<Line> >::iterator i;
findLine(i, number);
if(i == listOfLines.end()){
throw "LineDoesNotExist";
}
line 15 if(dynamic_cast<shared_ptr<FamilyLine> >(*i)){
cout << "Family Line";
} else {
throw "Not A Family Line";
}
}
Run Code Online (Sandbox Code Playgroud)
我有类 Line 并从它派生 FamilyLine 和 RegularLine,所以我想找到 FamilyLine
我的程序在第 15 行失败,我收到一个错误
cannot dynamic_cast target is not pointer or reference
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗,提前致谢
我试过这个:
shared_ptr<FamilyLine> ptr(dynamic_cast<shared_ptr<FamilyLine> >(*i));
if(ptr){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
同样的错误
void addLineRelative(LineNumber number, LineNumber relativeNumber) {
list<shared_ptr<Line> >::iterator i;
findLine(i, number);
if(i == listOfLines.end()){ …Run Code Online (Sandbox Code Playgroud) 我们有:
Base;Derived。宣言:
class Derived : public Base
{
public:
Derived(); // ctor
~Derived(); // dtor
void MakeSomething();
private:
// some private stuff
}
Run Code Online (Sandbox Code Playgroud)
现在我们去我们的主要。这工作正常:
// ...
Derived derived;
derived.MakeSomething();
// ...
Run Code Online (Sandbox Code Playgroud)
相反,这不起作用:
// ...
boost::shared_ptr< Base > derived(new Derived);
derived->MakeSomething();
// ...
Run Code Online (Sandbox Code Playgroud)
编译器说:
错误:类“Base”没有成员“MakeSomething”
我知道它没有,事实上我想调用Derived的方法!我在继承和指针方面缺少什么?
附加问题:
MakeSomething()virtual in Base,因为Base属于我可以继承的第三方库;boost::shared_ptr< Base >,我不能直接使用boost::shared_ptr< Derived >;我有一个名为scratch的类,并使用scratch.h来声明它。
现在我有另一个类称为scratch2下scratch2.h,想从头开始创建一个对象作为共享指针。
这是我在scratch2类声明中使用的语法:
std::shared_ptr<scratch> newObject(new scratch());
Run Code Online (Sandbox Code Playgroud)
但我收到此错误: Error: Expected type specifier
所以我尝试了这个:
std::shared_ptr<scratch> newObject2 = std::make_shared<scratch>();
Run Code Online (Sandbox Code Playgroud)
这工作正常。谁能告诉我为什么第一个不起作用?
我的scratch.h代码:
#ifndef _SCRATCH_
#define _SCRATCH_
#include <iostream>
class scratch {
private:
int _a;
float _b;
std::string _s;
public:
scratch();
scratch(int a, float b, std::string n);
~scratch();
};
#endif
Run Code Online (Sandbox Code Playgroud)
和我的scratch2.h:
#ifndef _SCRATCH_2_
#define _SCRATCH_2_
#include "scratch.h"
#include <memory>
class scratch2 {
std::shared_ptr<scratch> newObject(new scratch()); // Expected a type specifier error occurs here …Run Code Online (Sandbox Code Playgroud) 我正在使用 astd::shared_ptr指向一个节点
template<typename T>
class A
{
class Node
{
T data;
std::shared_ptr<Node> link;
Node(T data, std::shared_ptr<Node> link);
};
void push(T data);
std::shared_ptr<Node> top;
};
template<typename T>
A<T>::Node::Node(T data, std::shared_ptr<typename A<T>::Node> link) :
data(data), link(link)
{
}
template<typename T>
void A<T>::push(T item)
{
if (top == nullptr)
{
top = std::make_shared<typename A<T>::Node>(new typename
A<T>::Node(item, nullptr));
}
else
{
top = std::make_shared<typename A<T>::Node>(new typename A<T>::Node(item, top));
}
}
Run Code Online (Sandbox Code Playgroud)
结果声明和定义导致编译器错误
严重性代码说明项目文件行抑制状态错误 C2664“Stack::Node::Node(const Stack::Node &)”:无法将参数 1 从“Stack::Node *”转换为“const Stack::Node &”内存901
我需要改变什么才能符合 …
我的代码有什么问题:
class Game{
private:
mtm::Dimensions dimensions;
std::vector<std::shared_ptr<Character>> board;
};
std::shared_ptr<Character> Game::makeCharacter(CharacterType type, Team team, units_t health,
units_t ammo, units_t range, units_t power) {
std::shared_ptr<Character> out = nullptr;
if (type ==SNIPER)
out=mtm::Sniper(team,health,power,ammo,range);
return out;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
没有可行的重载 '='
out=mtm::狙击手(团队,健康,力量,弹药,射程);
注意:Sniper继承自抽象类Character。
我该如何解决这个问题?
考虑这个shared_ptr<T>以各种方式构造并返回的示例:
#include <memory>
#include <iostream>
class Base
{
public:
virtual ~Base() {}
Base(int y) : y_(y) {}
int y_;
};
class Derived : public Base
{
public:
Derived(int y, int z) : Base(y), z_(z) {}
int z_;
};
std::shared_ptr<Base> A()
{
return std::shared_ptr<Base>(new Derived(1, 2));
}
std::shared_ptr<Base> B()
{
std::shared_ptr<Derived> result = std::make_shared<Derived>(Derived(1, 2));
return result;
}
std::shared_ptr<Base> C()
{
std::shared_ptr<Base> result = std::make_shared<Base>(Derived(1, 2));
return result;
}
std::shared_ptr<Base> D()
{
return std::make_shared<Base>(Derived(1, 2));
}
int main(int argc, …Run Code Online (Sandbox Code Playgroud) 以以下代码段为基础:
template<typename Type1, typename Type2>
auto Foo() {
std::tuple<std::shared_ptr<Type1>, std::shared_ptr<Type2>> example;
}
Run Code Online (Sandbox Code Playgroud)
使用可变参数模板时,我应该如何创建共享指针元组?
template<typename... Types>
auto Foo() {
// How to create the tuple?
}
Run Code Online (Sandbox Code Playgroud)
这里有大量与元组和可变参数模板相关的问题。有些人似乎解决了这个问题(link1,link2),但仍然觉得太复杂(老实说,答案超出了我的脑海,但很可能是最简单的方法)。
我的尝试使用了递归模板 and std::tuple_cat,但这会在此过程中创建许多“子元组”,这远非理想。
简而言之:是否有“更简单”或“更直接”的方法来解决这个问题?不需要多个函数、递归调用等等的东西,有点像std::tuple<std::shared_ptr<Types...>>?
我想std::set是否shared_ptr's比较指针对象,而不是指针。
我有这个例子:
std::shared_ptr<std::string> s(new std::string("abc"));
std::shared_ptr<std::string> p(new std::string("abc"));
std::set<std::shared_ptr<std::string>> S;
S.insert(s);
S.insert(p);
std::cout << S.size();
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将相同的元素放入set但输出 2.
如何使 set 的插入使用底层字符串的比较标准?如果它不是字符串而是更复杂的对象呢?
int main(){
int* iptr;
{
std::shared_ptr<int> sptr = std::make_shared<int>(12);
iptr = sptr.get();
}
std::cout << *iptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
12
Run Code Online (Sandbox Code Playgroud)
我原以为iptr指向的内容会被删除,位于内花括号的末尾。但输出显示 12. 我错过了什么吗?
我正在尝试将nlohmann::json对象转换为std::shared_ptr<char[]>. 我遇到了一个问题,共享指针中的数据似乎附加了我原始对象中没有的额外垃圾。
下面的函数并不像它可能的那样简洁,因为我想展示我在调试器中看到的内容。您看到的注释是该行变量的值。我不明白这ÍýýýýÝÝÝÝÝ¡\x4\x1d'ýI是从哪里来的。我在这里做错了什么?
// Incoming json: {"data":[100]}
std::shared_ptr<char[]> serializeJson(nlohmann::json& d) {
std::string ds = d.dump(); // '{"data":[100]}'
std::shared_ptr<char[]> rd(new char[ds.size() + 1]); // 'ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýýÝÝÝÝÝ¡\x4\x1d'ýI'
ds.copy(rd.get(), ds.size() + 1); // '{"data":[100]}ÍýýýýÝÝÝÝÝ¡\x4\x1d'ýI'
return rd;
}
Run Code Online (Sandbox Code Playgroud)
我相信指针内的数据已被修改,因为当我尝试使用下面的函数对字符串进行反序列化时,出现异常。我也将值附加到此代码段中...
nlohmann::json deserializeJson(const std::shared_ptr<char[]>& d) {
std::string p = d.get(); // '{"data":[100]}ÍýýýýÝÝÝÝÝû¤¶¯´2'
std::string e = p.substr(p.size()-10); // 'ÝÝÝÝû¤¶¯´2'
nlohmann::json nd = nlohmann::json::parse(p); // kaboom! / Exception
return nd;
}
Run Code Online (Sandbox Code Playgroud)
感谢帮助!
shared-ptr ×10
c++ ×9
inheritance ×2
c++11 ×1
c++17 ×1
c2664 ×1
casting ×1
class ×1
make-shared ×1
methods ×1
pointers ×1
set ×1
sorting ×1
tuples ×1