我有一些非常简单的代码:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream is("1.0 2.0 1e-500 1e500 12.0");
double d = {17.0, 17.0, 17.0, 17.0, 17.0};
for (int i=0; i < 5; ++i)
{
if (is >> d[i])
{
cout<<"Conversion succeeded"<<endl;
}
else
{
cout<<"Conversion failed"<<endl;
is.clear();
}
}
for (int i=0; i < 5; ++i) cout<<d[i]<<endl;
}
Run Code Online (Sandbox Code Playgroud)
当我使用g ++ 4.1.2编译此代码并在Redhat 5.10(相同的编译器)上运行时,我得到输出:
Conversion succeeded
Conversion succeeded
Conversion failed
Conversion failed
Conversion succeeded
1
2
0
17
17
12
Run Code Online (Sandbox Code Playgroud)
当我在Redhat Linux 6.5(编译器4.4.7)上执行相同的二进制文件时,我得到了 …
好的,我正在使用g ++ 4.8.2并且有以下(有点长)代码,它获取有关不完整类型的错误消息.我已将代码缩减为较小的块以包含在此处,并且可以直接编译:
#include <cstdlib>
struct S
{
void method(){}
};
template<size_t sz, size_t tot>
class genpool
{
};
template <class T>
class mempool
{
private:
genpool<sizeof(T), 10*sizeof(T)> p;
};
template <class obj, class mthd>
class functor
{
private:
static mempool<functor<obj, mthd> > pool;
};
template <class obj, class mthd>
mempool<functor<obj, mthd> > functor<obj, mthd>::pool;
int main()
{
typedef void (S::*m)();
typedef functor<S, m> fctr;
fctr f;
}
Run Code Online (Sandbox Code Playgroud)
编译器错误消息是:
g++ jj.C
jj.C: In instantiation of ‘class mempool<functor<S, void (S::*)()> >’: …Run Code Online (Sandbox Code Playgroud) 我希望有一个看起来像这样的课程:
template <typename T>
class foo
{
public:
template <typename S>
foo()
{
//...
}
};
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何调用构造函数.显然,我可以通过给foo()一个S类型的参数来完成这项工作,但它可以在没有任何参数的情况下完成吗?
--Ron