C++奇怪的第三方函数构造函数

C++ weird third-party function constructor

我有一个第三方库,我想使用提供的构造函数之一。

ex.h:

/** Construct example from string and a list of symbols. The input grammar is
 *  similar to the GiNaC output format. All symbols and indices to be used
 *  in the expression must be specified in a lst in the second argument.
 *  Undefined symbols and other parser errors will throw an exception.        */
ex(const std::string &s, const ex &l);

我尝试了以下方法:

symbol x("x");

ex e("x^2",x);

很遗憾,此构造函数的用法不正确。我收到以下错误消息:

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: find_or_insert_symbol: symbol "x" not found

提供的所有文档都是声明上方的注释。我是一个C++新手,所以我不知道哪里出了问题。

我尝试了第一个答案中的建议,如下所示:

symbol x("x");

ex expression;
ex e("x^2",expression);

std::cout << diff(e,x) << std::end

这会导致以下错误消息:

libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: find_or_insert_symbol: symbol "x" not found (lldb)

注意:我尝试在diff中使用eexpression ().

您需要提供 ex 参考,而不是 symbol 参考; 试试这个:

ex MyEx1; //This will call to the ex default constructor for MyEx1, if it exist.
ex e("x^2",MyEx1); //This will call to the ex constructor that you want to use for e.

第二个参数应该是字符串中出现的符号列表(更准确地说,GiNaC::ex 处理 GiNaC::lst)。这有效:

    symbol x("x");
    ex e("x^2", lst{x});

这个想法是它应该与不止一个符号一起工作:

    symbol x("x"), y("y");
    ex e("x^2-2*x*y+y^2", lst{x,y});
    cout << diff(e, x) << endl;  // prints "2*x-2*y" or similar