【问题标题】:emscripten complex function set to zeroemscripten 复函数设置为零
【发布时间】:2014-07-10 12:28:55
【问题描述】:

我正在尝试交叉编译一些代码,但我有这行给出错误,我不知道它应该做什么,所以我可以修复它。

typedef std::complex COMPLEX;

复杂* _matrix[MATRIX_NODES+OFFSET]; // 对于 tran,real 现在是,imag 被保存。

_matrix[ii][jj].real() = 0;

我不确定 =0 应该做什么。它用 clang 和 g++ 编译,但不是 emscripten。 emscripten 有一个不同的复数库。 有谁知道如何解决这个问题?

这里是emscripten中的定义,与常规库不同。

template<>
class _LIBCPP_TYPE_VIS_ONLY complex<double>
{
double __re_;
double __im_;
public:
typedef double value_type;

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
    : __re_(__re), __im_(__im) {}
_LIBCPP_CONSTEXPR complex(const complex<float>& __c);
explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}

_LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
_LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}

_LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
    {__re_ = __re; __im_ = value_type(); return *this;}
_LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
_LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
_LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
_LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}

template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
    {
        __re_ = __c.real();
        __im_ = __c.imag();
        return *this;
    }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
    {
        __re_ += __c.real();
        __im_ += __c.imag();
        return *this;
    }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
    {
        __re_ -= __c.real();
        __im_ -= __c.imag();
        return *this;
    }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
    {
        *this = *this * complex(__c.real(), __c.imag());
        return *this;
    }
template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
    {
        *this = *this / complex(__c.real(), __c.imag());
        return *this;
    }
};

我写了这个简短的例子

#include <iostream>
#include <complex>

using namespace std;


int main(int argc, const char *argv[]){
complex<double> x;
x.real(1);
cout<<x.real()<<'\n';  

x.real()=0;
cout<<x.real()<<'\n';


x.real(2);
cout<<x.real()<<'\n';


double* y=&x.real();
cout<<*y<<'\n';
}

它使用 -std=c++98 而不是 -std=c++11 编译。 我在 emscripten 上尝试了 -std=c++98 但它似乎没有做任何事情。

有什么想法吗?

【问题讨论】:

  • = 0 正是它的样子——赋值为 0。我假设 real() 方法返回对复数实部的引用;赋值将其设置为 0。你说它不能用 emscripten 编译,但你甚至没有说你得到什么错误消息......
  • 抱歉,错误:表达式不可赋值
  • 可能是编译器设置?

标签: c++ emscripten


【解决方案1】:

std::complex::real() 返回一个值而不是引用。这意味着您不能为其分配值,它只能出现在表达式的右侧。在 C 标准术语中,使其成为右值(与可以出现在左侧或右侧的左值相反)。

但是,看起来代码最初试图将实部分配为零。

所以,如果这是您的意图,那么请使用以下方法:

_matrix[ii][jj].real(0.0);

较旧的库可能不支持这个*,但它可以与我在此更改的最新 clang++ 一起正常编译(编辑:同时使用 std::complex 和您提供的模板类)。

*http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1589.html

【讨论】:

    猜你喜欢
    • 2011-03-21
    • 1970-01-01
    • 2015-05-04
    • 2016-11-23
    • 2016-06-27
    • 2023-03-27
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多