小编Awe*_*e36的帖子

在C++中继承异常

我刚刚创建了一个异常层次结构,并希望我的catch-block显示派生异常的消息.我有5个例外,比如这个:

class ImagetypeException : public TGAException {  
public:  
const char* what() const throw();  
};  


const char* ImagetypeException::what() const throw() {
    return "Der Bildtyp ist nicht \"RGB unkomprimiert\".";
}
Run Code Online (Sandbox Code Playgroud)

所有这些都是从TGAException派生的,它是从std :: exception派生的.

class TGAException : public std::exception {
public:
    virtual const char* what() const throw();
};

const char* TGAException::what() const throw() {
    return "Beim Einlesen oder Verarbeiten der TGA-Datei ist ein unerwarteter Fehler aufgetreten!";
}
Run Code Online (Sandbox Code Playgroud)

因此,我显然希望在我的代码中的某些时候抛出这些内容,并认为最小化我需要的catch块数量可能是个好主意.

catch (TGAException e) {
        cout << e.what() << endl;
    }
Run Code Online (Sandbox Code Playgroud)

如果我这样做,将打印的消息是来自TGAException的消息,但我希望它显示更具体的派生消息.那么我需要做什么才能让我按照我想要的方式工作呢?

c++ exception

6
推荐指数
1
解决办法
297
查看次数

Unique_Ptr:尝试引用已删除的函数

所以我现在正在用 C++ 做一个学校项目,虽然我对这门语言还不太熟悉。整个项目分为几个里程碑。1:读取具有不同类型生物的列表并将它们存储在向量中 2:读取 TGA 文件并将其存储在类中。... 5:为每个读取的 Creature-Type 读取 TGA-Picture 并将其存储以供进一步使用。(在 GUI 上打印,删除/添加)

所以我认为将每种类型的图片存储在类本身中是个好主意,因为它应该只加载一次。我的 TGAPicture 类中的 load() 函数将返回 std::unique_ptr 所以我在 CreatureType 类中添加了类型作为参数。这样做之后,我得到了几个这样的错误:

Error   C2280   'biosim::CreatureType::CreatureType(const biosim::CreatureType &)': attempting to reference a deleted function  bio-sim-qt  E:\Development\C++\bio-sim-qt\bio-sim-qt\qtmain.cpp 58  1   

Error (active)      function "biosim::CreatureType::CreatureType(const biosim::CreatureType &)" (declared implicitly) cannot be referenced -- it is a deleted function  bio-sim-qt  e:\Development\C++\bio-sim-qt\bio-sim-qt\Model.cpp  15  26  
Run Code Online (Sandbox Code Playgroud)

因此,我阅读了大约 10 个与我的标题类似的问题,并且每个问题都指出,您不能复制 unique_ptr 和建议的解决方案,例如使用 std::move() 或返回引用。尽管我尝试使用这些来解决我的问题,但我一点也做不到,可能是因为我对 C++ 还很陌生,从未使用过唯一指针。

这是代码,似乎与我有关:

/**
 * @class CreatureType
 * Object of the various CreatureTypes ingame
 */ …
Run Code Online (Sandbox Code Playgroud)

c++ reference move unique-ptr

1
推荐指数
1
解决办法
991
查看次数

包含std :: string常量的类

因此,我目前正在使用C ++进行学校项目,而我对此并不十分熟悉。我想创建一个类,其中包含我尝试的所有常量(string,int,double,own类),它在Java中一直对我有用:

class Reference {


    //Picture-Paths
    public:
    static const std::string deepSeaPath = "E:\\Development\\C++\\Material\\terrain\\deep_sea.tga";
    static const std::string shallowWaterPath = "E:\\Development\\C++\\Material\\terrain\\deep_sea.tga";
    static const std::string sandPath = "E:\\Development\\C++\\Material\\terrain\\deep_sea.tga";
    static const std::string earthPath = "E:\\Development\\C++\\Material\\terrain\\deep_sea.tga";
    static const std::string rocksPath = "E:\\Development\\C++\\Material\\terrain\\deep_sea.tga";
    static const std::string snowPath = "E:\\Development\\C++\\Material\\terrain\\deep_sea.tga";

};
Run Code Online (Sandbox Code Playgroud)

但是,在C ++中,出现以下错误:

Error   C2864   'Reference::Reference::earthPath': a static data member with an in-class initializer must have non-volatile const integral type bio-sim-qt  e:\development\c++\bio-sim-qt\bio-sim-qt\Reference.hpp  16  1   
Run Code Online (Sandbox Code Playgroud)

所以我有什么办法可以存储例如这样的String-Constants吗?如果可以,还有更好的方法吗?如果否,还有其他方法(#define?)吗?


c++ string static const constants

0
推荐指数
1
解决办法
183
查看次数

标签 统计

c++ ×3

const ×1

constants ×1

exception ×1

move ×1

reference ×1

static ×1

string ×1

unique-ptr ×1