小编Joh*_*nas的帖子

为什么在将模板参数用作另一个模板的模板参数时,不能推导出模板参数?

这段代码有什么问题?

#include <map>

template<typename T>
struct TMap
{
    typedef std::map<T, T> Type;
};

template<typename T>
T test(typename TMap <T>::Type &tmap_) { return 0.0; }

int _tmain(int argc, _TCHAR* argv[])
{
    TMap<double>::Type tmap;
    tmap[1.1] = 5.2;
    double d = test(tmap); //Error: could not deduce template argument for T
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ parameters templates

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

C++,三元运算符,std :: cout

如何使用C++使用三元运算符编写以下条件

int condition1, condition2, condition3;
int / double result; //int or double
....
std::cout << ( condition1: result1 : "Error" ) 
          << ( condition2: result2 : "Error" )
          << ( condition3: result3 : "Error")...;
Run Code Online (Sandbox Code Playgroud)

c++ printing conditional ternary-operator

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

比较迭代器,C++

是否可以比较两个迭代器?使用std :: min的比较

void change ( typename TList <Item *>::Type ::iterator it_begin, typename TList <Item*>::Type ::iterator it_end )
{
   ....
this->items.resize ( index );
   std::sort ( it_begin, std::min (it_end, it_begin += index - 1); //Compare two iterators, exception
....
}
Run Code Online (Sandbox Code Playgroud)

抛出以下异常:

Assertion failed: Vector iterators  incompatible... 
Run Code Online (Sandbox Code Playgroud)

比较还有其他方法吗?

c++ iterator compare

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

为什么分配给std :: ostream的引用无法编译?

我正在使用抽象类std :: ostream.有以下参考:

std::ostream &o = std::cout; 
Run Code Online (Sandbox Code Playgroud)

如果满足任何条件,我需要初始化o,以便输出重定向到std :: cout.如果没有,输出将被重定向到文件

if (!condition)
    o = file; //Not possible 
Run Code Online (Sandbox Code Playgroud)

如何正确编写代码?

c++ ostream

9
推荐指数
2
解决办法
926
查看次数

C++,vs 2010中的模糊继承错误

在这个例子中我对多态性的应用有些麻烦.这个问题类似于我的上一个问题

C++,虚拟继承,奇怪的抽象类+克隆问题

有3个抽象类:

class A
{
public:
    virtual A  * copy () const = 0;
    virtual ~A() = 0;
};

A::~A(){}

class B
{
public:
    virtual B  * copy () const = 0;
    virtual ~B() = 0;
};

B::~B(){}

class C: virtual public A , public B 
{
public:
    virtual C  * copy () const = 0;
    virtual ~C() = 0;
};

C::~C(){}
Run Code Online (Sandbox Code Playgroud)

和两个使用虚拟继承的继承类

class D: virtual public A
{
public:
    virtual D  * copy () const {return new D …
Run Code Online (Sandbox Code Playgroud)

c++ covariance virtual-inheritance visual-c++ visual-c++-2010

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

C++,std :: map的迭代器

如何声明迭代器

std::map <T, Point <T> *> ,
Run Code Online (Sandbox Code Playgroud)

哪里:

template <typename T>
struct TList
{
    typedef std::vector < std::map <T, Point <T> *> >  Type;
};
Run Code Online (Sandbox Code Playgroud)

在以下代码中

int main ()
{
    ....
    std::map <T, Point <T> *> ::iterator i_map;  //Error
    ...
}
Run Code Online (Sandbox Code Playgroud)

g ++显示此错误:

error: dependent-name `  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' is parsed as a non-type, but instantiation yields a type
note: say `typename  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' if a type is meant
Run Code Online (Sandbox Code Playgroud)

c++ iterator stdmap

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

C++,std :: copy和templates

我正在使用模板和STL进行实验.这是我的测试代码......

template <typename Item>
struct TList
{
    typedef std::vector <Item> Type;
};

template <typename Item>
class Cont
{
private:
    typename TList <Item>::Type elements;

public:
    void push_back ( const Item & p ) {elements.push_back ( p );}

    typename TList <Item>::Type ::iterator
        copy (typename TList <Item>::Type ::iterator first,
                typename TList <Item>::Type ::iterator last,
                typename TList <Item>::Type ::iterator result) 
        {
            elements.resize(elements.size() + last - first);  //Exception
            return copy ( first, last, result ); 
        }

        typename TList <Item>::Type ::iterator begin() { return elements.begin(); } …
Run Code Online (Sandbox Code Playgroud)

c++ templates copy vector

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

Multiset to vector,C++

如何以最快的顺序将std :: multiset中的最后k个项目以相反的顺序复制到std :: vector?

stl copy vector multiset

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