小编Pet*_*ker的帖子

void指向动态分配的类的指针

我正在尝试将void*传递给函数,然后在该函数内部使指针指向动态创建的对象.这是我到目前为止,但它似乎没有工作:

主要:

int main()
{
  void* objPtr;

  setPtr(objPtr);
}
Run Code Online (Sandbox Code Playgroud)

setPtr:

void setPtr(void*& objPtr)
{
  objPtr = new Obj1; 
  (*objPtr).member1 = 10; //error: expression must have pointer-to-class type
}
Run Code Online (Sandbox Code Playgroud)

OBJ1:

struct Obj1
{
  int member1;
};
Run Code Online (Sandbox Code Playgroud)

提前致谢

c++ void-pointers

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

使用std :: map而不是vector <pair <string,string >>>我会看到性能提升吗?

我目前有一些代码,我使用的vectorpair<string,string>.这用于存储来自XML解析的一些数据,因此,该过程在某些地方非常慢.在试图加快整个过程中,我想知道是否有将是从交换任何性能方面的优势vector<pair<string,string> >std::map<string,string>?我可以编写代码并运行一个分析器,但我想我会看到我是否能得到一个答案,表明首先会有一些明显的性能提升.我不需要进行任何排序,我只是将项添加到向量中,然后在稍后阶段迭代内容并进行一些处理 - 我不需要排序或任何这种性质.我猜也许我不会获得任何性能提升,但我从来没有真正使用过std::map,所以我不知道没有要求或编码全部.

c++ stl stdmap stdvector

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

数组初始化列表中的隐式转换

为什么从隐转换const char*std::string不会在后一种情况下工作吗?请尽可能链接对C++标准的引用.

变式1:

struct Foo {
    Foo(const char* a) {}
};

int main() {
   // works well for a "const char*" accepting constructor
   Foo* foo = new Foo[1] { "a" };
}
Run Code Online (Sandbox Code Playgroud)

变式2:

struct Foo {
    Foo(std::string a) {}
};

int main() {
   // could not convert from "const char*" to "Foo"
   Foo* foo = new Foo[1] { "a" };
}
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++11

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

在C++中,为什么转换指针时地址会改变?

以下是代码:

#include <iostream>
using namespace std;

class B1 {
public:

  virtual void f1() {
      cout << "B1\n";
      }
};

class B2 {
public:

  virtual void f1() {
      cout << "B2\n";
      }
};        

class D : public B1, public B2 {
public:
  void f1() {
      cout << "OK\n" ;
  }
};

int main () {

D dd;    
B1 *b1d = &dd;
B2 *b2d = &dd;
D *ddd = &dd;

cout << b1d << endl;
cout << b2d << endl;
cout << …
Run Code Online (Sandbox Code Playgroud)

c++ oop pointers virtual-functions multiple-inheritance

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

无法识别的模板声明/定义

我正在尝试实现一个堆,但我的一个函数上面出现了上述错误.

这是我的头文件:

template <typename E>
class Heap
{
private:
    class Node {
        E data;
        Node * left;
        Node * right;
    };

    Node root;
    int length;

    E * preorder(E * list, int length, Node node);
    E * inorder(E * list, int length, Node node);
    E * postorder(E * list, int length, Node node);
    void clear(Node node);  //Recursively clears all nodes and frees all pointers
public:
    Heap();
    Heap(E * list, int length);
    ~Heap();

    Node * getRoot();
    void buildHeap(E * list, int length); …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

使用constexpr进行错误编译

我正在尝试编译以下代码:

enum class Order : char
{
    Little,
    Big
};

constexpr Order get_order() {
    uint16_t x = 1;
    return *((uint8_t *) &x) == 0 ? Order::Big : Order::Little;
}
Run Code Online (Sandbox Code Playgroud)

我用-std=c++14标志做到这一点,但我得到了这个错误:

在函数'constexpr byteorder :: Order byteorder :: get_order()'中:/home/user/dev/c++/render/include/byteorder.h:19:1:error:constexpr function'constexpr byteorder :: Order byteorder :: get_order()'不是返回语句

看起来像c ++ 11!

如果c ++ 14在constexpr函数中允许局部变量怎么办?

Debian Jessie,gcc 4.9.2

c++ constexpr c++14

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

为什么我不能在列表迭代器上使用+ =运算符?

我有一个来自a的迭代器std::list<std::string>,但是当我尝试使用它时+=,我得到一个编译错误.

代码是:

#include <list>
#include <iostream>
#include <string>
int main() {
    std::list<std::string> x;

    x.push_front("British");
    x.push_back("character");
    x.push_front("Coding is unco");
    x.push_back("Society");
    x.push_back("City Hole");
    auto iter = x.begin();
    iter += 3;
    //std::advance(iter, 3);
    x.erase(iter);

    for (auto &e: x) {
        std::cout << e << "\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我使用这个编译clang++ -std=c++11 -o li li.cpp,我得到:

li.cpp:13:10: error: no viable overloaded '+='
    iter += 3;
    ~~~~ ^  ~
1 error generated.
Run Code Online (Sandbox Code Playgroud)

为什么我不能使用+=这个迭代器?

c++ iterator stdlist c++11

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

使C++自动将int转换为double

当我有一个数字类型,它定义operator<为double,但不是int,与int literals的比较不起作用.这是一个问题,作为标准库的一部分,即std::complex包含int文字.

在使用类型时,我可以使编译器将int文字视为double吗?

简化示例:

// the defined operator
template<typename T>
bool operator<(const Type<T> &lhs, const T &rhs);

complex<Type<T>> complex_number;
1.0 / complex_number; // this fails
Run Code Online (Sandbox Code Playgroud)

失败发生在at 的_Div方法中std::complex

template<class _Other> inline
void _Div(const complex<_Other>& _Right)
    // ...
    else if ((_Rightimag < 0 ? -_Rightimag : +_Rightimag)
Run Code Online (Sandbox Code Playgroud)

这会导致错误:

error C2678: binary '<': no operator found which takes a left-hand operand of type 'Type<T>' (or there is no acceptable conversion)
(...)
complex(665): note: while trying to …
Run Code Online (Sandbox Code Playgroud)

c++ implicit-conversion ceres-solver

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

为什么 const 指针会意外转换为 bool?

我有一个带有两个构造函数的类。一为bool一为A*

struct B
{
    explicit B(bool b)
    {
      std::cout << "B(bool)" << std::endl;
    }
    explicit B(A*)
    {
      std::cout << "B(A*)" << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

当 B 应该用const A*而不是A*-构造时,const A*将转换为bool.

const A a;
B b(&a);
Run Code Online (Sandbox Code Playgroud)

输出: B(bool)


所需的解决方案是

编译器错误:“B(const A*) 没有有效的构造函数”

我已经尝试过使用explicit关键字,但是没有用。

在coliru运行示例

c++ casting compiler-errors boolean-expression

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

Why can I implicitly convert char* to const char* but not unsigned char*

The following code snippet produces a compile error:

char a = 'a';
const char* a_ = &a;
unsigned char b = 'b';
const char* b_ = &b;
Run Code Online (Sandbox Code Playgroud)

The last line produces the error:

error: invalid conversion from 'unsigned char*' to 'const char*'
Run Code Online (Sandbox Code Playgroud)

I can implicitly convert from char* to const char*, but I cannot do the same for unsigned char*? What is the reason behind this?

c++

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