小编son*_*yao的帖子

模板类专业化:模板 ID 不匹配任何模板声明

我正在尝试使用模板,但无法理解以下代码有什么问题。

解决.h

#include "nlp.h"
#include "Ipopt_solve.h"

enum algo_type {IPOPT =1, SQP};

template<int ALG>
class solve
{
public:
    solve()
    {
    }
};

template<>
class solve<IPOPT>
{
public:
    solve(nlp*);

private:
    Ipopt_solve m_ipopt;

};
Run Code Online (Sandbox Code Playgroud)

解决.cpp

template<>
solve<IPOPT>::solve(nlp* problem): m_ipopt(problem)
{
}
Run Code Online (Sandbox Code Playgroud)

Ipopt_solve是抽象类的子类TNLPIpopt_solve使用对nlp类的引用进行初始化。

来自 main.cpp

nlp problem(&model);
solve<IPOPT> solution(&problem);
Run Code Online (Sandbox Code Playgroud)

我收到如下所示的错误。

错误:模板 id 'solve<>' for 'solve<1>::solve(nlp*)' 不匹配任何模板声明 solve::solve(nlp* problem): m_ipopt(problem)

c++ templates template-specialization

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

Return both string and integer from a template function in C++

I am trying to write a generic function, using template, that is able to return bool, integer or char* or string.

template<typename entryType>
entryType getEntry(xml_node node, const char* entryKey)
{
    ...

    if (is_same<entryType, bool>::value) {
         return boolvalue; //returning a boolean
    }
    else if(is_same<entryType, int>::value) {
         return a; //this is an integer
    }
    else if (is_same<entryType, char*>::value) {
         return result; //this is a char*
    }
}
Run Code Online (Sandbox Code Playgroud)

and I would like to be able to call it like:

bool bPublisher = getEntry<bool>(node, …
Run Code Online (Sandbox Code Playgroud)

c++ templates if-statement

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

std::vector emplace 和 std::vector emplace 返回成对

我有这个代码:

std::vector<std::pair<const std::string, int>> vec;

vec.emplace_back("a", 1); //success
vec.emplace(vec.end(), "b", 2); //compile error

vec.emplace_back(std::make_pair<const std::string, int>("c", 3));  //success
vec.emplace(vec.end(),
     std::make_pair<const std::string, int>("d", 4)); //compile error
Run Code Online (Sandbox Code Playgroud)

你能解释一下为什么吗?

c++ stl vector c++11 emplace

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

C ++模板类继承

如何定义从模板类继承的模板类?

我想包装std::queuestd::priority_queue转到基类。就我而言LooperQueue。我用StdQueue这种方式auto queue = new StdQueue<LooperMessage *>()

我的课定义编译器抱怨

错误日志:

  In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
  /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:14:5: error: unknown type name 'size_type'; did you mean 'size_t'?
      size_type size() override;
      ^~~~~~~~~
      size_t
  /Users/rqg/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/5.0.300080/include/stddef.h:62:23: note: 'size_t' declared here
  typedef __SIZE_TYPE__ size_t;
                        ^
  In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
  /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:16:5: error: unknown type name 'reference'
      reference front() override;
      ^
  /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:20:21: error: unknown type name 'value_type'; did you mean 'ARect::value_type'?
      void push(const value_type &x) override;
                      ^~~~~~~~~~
                      ARect::value_type …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates class name-lookup

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

通过string :: insert插入char时出错

我的问题与类似,但那里没有示例代码,我觉得它没有用.

我的错误是

调用类'std :: __ 1 :: __ wrap_iter'的私有构造函数

我的问题的一个最小例子可以在下面找到:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string g = "this_word";
    cout << g << endl;
    char temp = g[0];
    g.erase(0,1);
    cout << g << endl;
    g.insert(0,temp); // Compiler seems to dislike this.  Why?
    cout << g << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我通过两个编译器尝试了这个,并且出现了同样的错误.从标准文档中尽可能多地阅读,但不理解我的错误.

c++ string

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

为什么我可以将std :: map的键传递给期望非const的函数?

根据std::map 文档,它存储键值对std::pair<const Key, Value>,因此映射中的键是const.

现在假设我有一个std::map键是指向某些对象的指针.

struct S {};
struct Data {};
using MyMap = std::map<S*, Data>;
Run Code Online (Sandbox Code Playgroud)

我们还假设有一个foo接受S*参数的函数.

void foo(S* ptr) { /* modify the object (*ptr) */ }
Run Code Online (Sandbox Code Playgroud)

现在,问题是:当我MyMap使用基于范围的for循环迭代时,我能够将map元素键传递给foo:

MyMap m = getMyMapSomehow();
for (auto elem : m)
{
    static_assert(std::is_const<decltype(elem.first)>::value, "Supposed to be `const S*`");
    foo(elem.first); // why does it compile?
}
Run Code Online (Sandbox Code Playgroud)

所以,即使我的static_assert成功(所以我假设它的类型elem.firstconst S*),foo编译的调用很好,因此看起来好像我能够修改指针到const后面的对象.

为什么我能做到这一点?

PS这是Coliru …

c++ pointers stl const stdmap

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

使用模板重载运算符,但阻止重新定义

我想定义operator<<使用模板的特化,但我不希望它破坏了这个运算符的行为,如果它已经为某些数据类型定义的话.

enum State {Open, Locked};
enum Input {Coin, Push};

std::string ToString(State c){
  switch (c) {
    case Locked : return "Locked";
    case Open : return "Open";
  }
}

std::string ToString(Input c){
  switch (c) {
    case Coin : return "Coin";
    case Push : return "Push";
  }
}

template<typename T>   //obviously, a bad idea
std::ostream& operator<<(std::ostream& s, T c) {
  return s<<ToString(c);
}
Run Code Online (Sandbox Code Playgroud)

稍后在代码中我想使用:

int main() {
  std::cout<<Coin<<std::endl;
  std::cout<<Open<<std::endl;
  std::cout<<std::string("normal string")<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

不出所料,上面给出了编译错误:

error: ambiguous overload for ‘operator<<’ (operand types are …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading operator-overloading

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

在结构外部调用指向函数的指针

我有一个结构,里面是一个指向同一结构的函数的指针.现在我需要调用一个指向结构外部函数的指针.我举一个下面的代码示例:

#include <iostream>

struct test {
    void (test::*tp)(); // I need to call this pointer-to-function
    void t() {
        std::cout << "test\n";
    }
    void init() {
        tp = &test::t;
    }
    void print() {
        (this->*tp)();
    }
};
void (test::*tp)();

int main() {
    test t;
    t.init();
    t.print();
    (t.*tp)(); // segfault, I need to call it
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers

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

如何在结构上使用std :: unique_ptr?

标题说明了大部分内容,我该怎么做?我已经在Google上搜索了一下,没有任何东西告诉我它无法完成,但是也没有任何说明如何做到这一点。

在此处获取以下代码段:

#include <cstdio>
#include <memory>

int main(void)
{
    struct a_struct
    {
        char first;
        int second;
        float third;
    };

    std::unique_ptr<a_struct> my_ptr(new a_struct);

    my_ptr.first = "A";
    my_ptr.second = 2;
    my_ptr.third = 3.00;

    printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);

    return(0);
}
Run Code Online (Sandbox Code Playgroud)

正如能够回答这个问题的人已经知道的那样,这行不通,甚至无法编译。

我的问题是我该如何做类似的工作?

编译错误(使用g ++-7)如下所示

baduniqueptr6.cpp: In function ‘int main()’:
baduniqueptr6.cpp:15:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘first’
     my_ptr.first = "A";
            ^~~~~
baduniqueptr6.cpp:16:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘second’
     my_ptr.second = 2;
            ^~~~~~
baduniqueptr6.cpp:17:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘third’ …
Run Code Online (Sandbox Code Playgroud)

c++ struct unique-ptr c++11

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

如何将大括号括起来的初始化列表传递给函数?

我想编写一个可以与参数一起使用的函数,否则该函数可能直接出现在基于范围的循环中:

template <typename Iterable>
void sayIt(const Iterable& stuff) {
    for (const auto& e : stuff) {
        cout << e << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于 stl 容器和其他类型,但不适用于大括号封闭的初始化程序:

std::vector<std::string> baz(2, "sorry");
sayIt(baz);              // okay
sayIt({"foo", "bar"});   // not okay
Run Code Online (Sandbox Code Playgroud)

有没有办法让函数同时使用两者?

c++ templates stl list-initialization range-based-loop

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