小编lar*_*pco的帖子

修复C++多重继承模糊调用

我有三个类这样的结构:

#include <iostream>
using namespace std;

class Keyword
{
    public:
        virtual float GetValue() = 0;
};

class CharacterKeyword : public Keyword
{
    public:
        virtual float GetValue(){return _value;}
    private:
        float _value;
};

class MeasurementKeyword : public Keyword
{
    public:
        virtual float GetValue(){return _value;}
    private:
        float _value;
};

class AddressType : public CharacterKeyword, public MeasurementKeyword
{

    private:
        float address;
        float addresExt;
};

int main()
{
    AddressType *a = new AddressType();
    a->GetValue();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下内容:

In function ‘int main()’:
error: request for …

c++ virtual-inheritance diamond-problem

6
推荐指数
3
解决办法
2万
查看次数

将构造函数传递给函数

我正在使用STL向量,它是参数的向量.

std::vector<Parameter> foo;
Run Code Online (Sandbox Code Playgroud)

我试图找到一种方法将Parameter对象添加到向量而不执行此操作:

Parameter a;
foo.push_back(a);
Run Code Online (Sandbox Code Playgroud)

我遇到了一个执行此操作的实现:

foo.push_back(Parameter()); //Using the Parameter constructor
Run Code Online (Sandbox Code Playgroud)

我认为当我创建一个对象时,构造函数被调用,反之亦然.为什么我可以将构造函数传递给函数?

c++ constructor temporary-objects

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

使用CPPUnit的Printf样式断言

CPPUnit是否有任何功能可以让我做出printf风格的断言?例如:

CPPUNIT_ASSERT("Actual size: %d", p->GetSize(), p->GetSize() == 0);
Run Code Online (Sandbox Code Playgroud)

我知道这不是一个有效的CPPUNIT_ASSERT - 我只是以此为例.

我发现CPPUNIT_ASSERT_MESSAGE(message,condition)哪个接受一个字符串,然后条件来评估,但没有运气将值输入断言.

c++ cppunit

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

如何将十六进制字符串转换为Uint8

我想知道为什么在将十六进制字符串(0x1)转换为uint8时得到0的结果.

我尝试使用boost::lexical_cast但是我得到了一个bad_lexical_cast例外.因此,我决定使用a stringstream而不是我得到的值不正确.

...
uint8_t temp;
std::string address_extension = "0x1";
std::cout << "Before: " << address_extension << std::endl;
StringToNumeric(address_extension, temp);
std::cout << "After: " << temp << std::endl;
...

template <typename T>
void StringToNumeric(const std::string& source, T& target)
{
    //Check if source is hex
    if(IsHexNotation(source))
    {
       std::stringstream ss;
       //Put value in the stream
       ss << std::hex << source;
       //Stream the hex value into a target type
       ss >> target;
     }

 }
Run Code Online (Sandbox Code Playgroud)

您可以放心,它IsHexNotation()可以正常工作,并且不会在声明时更改源:

bool …
Run Code Online (Sandbox Code Playgroud)

c++ casting

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