I have a third-party library, and I want to use one of the supplied constructors.
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);
I tried the following:
symbol x("x");
ex e("x^2",x);
Unfortunately the usage of this constructor is incorrect. I get the following error message:
libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: find_or_insert_symbol: symbol "x" not found
All documentation that is provided is the comment above the declaration. I am a C++ novice, so I have no idea what is wrong.
I tried the suggestion in the first answer like the following:
symbol x("x");
ex expression;
ex e("x^2",expression);
std::cout << diff(e,x) << std::end
This results in the following error message:
libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: find_or_insert_symbol: symbol "x" not found
(lldb)
Note: I tried using e and expression in diff().
You need to provide an ex reference, not a symbol reference;
Try this:
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.
The second argument should be a list of symbols occuring in the string (more precisely, a GiNaC::ex handling a GiNaC::lst). This works:
symbol x("x");
ex e("x^2", lst{x});
The idea is that it should work with more than just one symbol:
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
Related
As you see i have a check method defined in the class definition. The problem is that i cant compare the two characters since it doesnt let me to define char e in the class defintion. How do i fix this task? a==e is written by me for reference of what i have been doing, but i get the error message:
Syntax Error(s)
tester.java:7: error: cannot find symbol
if(a==e){
^
symbol: variable e
location: class tester
1 error
Thankful for any help!
public static void check (char a){`
`if(a==e){`
`System.out.println("if");`
`}else{`
`System.out.println("else");
}
}
I am trying to implement the code sample from a text book. It is trying to create an inherited Exception class from exception with code below.
#include <stdexcept>
#include <string>
using namespace std;
class TargetNotFoundException: public exception {
public :
TargetNotFoundException(const string& message = "")
: exception("Target not found: " + message.c_str()){ } // end constructor
}; // end TargetNotFoundException
The constructor provides a way for a throw statement to identify the condition that caused the exception. For example, the statement
throw TargetNotFoundException (target + " not found in a box!");
invokes the constructor of TargetNotFoundException. The message given to the constructor is returned by the method what that is inherited from the class exception. Thus, a catch block, such as the following one, can access the message:
catch(TargetNotFoundException except) {
cout << except.what() << endl;
}
If target has the value "glasses" when this block executes, the output is Target not found: glasses not found in a box!
However, there is a compilation error stating that:
error: invalid operands to binary expression ('const char [19]'
and 'const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type *'
(aka 'const char *'))
: exception("Target not found: " + message.c_str()){ } // end constructor
~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
There's no + operator that takes two C strings as parameters. Simply remove .c_str() from your code
exception("Target not found: " + message){ }
Presumably just a typo from your text book.
In gdb, if you have a pointer to something, you can cast it before printing it.
For example, this works:
print *(int*) 0xDEADBEEF
However, how do I print a std::vector<T>? Specifically a std::vector<std::string>?
If it's std::string, I can do it with std::__cxx11::string, which whatis std::string outputs, but I can't convince gdb to like std::vector<int> (as an example). Quoting it doesn't help, as it says, No symbol "std::vector<int>" in current context.
One way you can do this is by using the mangled name of the type. For example, the mangled name of std::vector<int> on current gcc and libstdc++ is _ZSt6vectorIiSaIiEE, which I found by compiling the following code on the Compiler Explorer:
#include <vector>
void foo(std::vector<int>) {}
// Mangled symbol name: _Z3fooSt6vectorIiSaIiEE
// _Z means "this is C++".
// 3foo means "identifier 3 chars long, which is `foo`"
// Strip those off and you're left with: St6vectorIiSaIiEE
// Add the _Z back: _ZSt6vectorIiSaIiEE
The mangled name of std::vector<std::string> is: _ZSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE, which can be verified with whatis.
Actually performing the cast:
print *(_Z3fooSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE*) 0xDEADBEEF
I'm debugging a C++ program in Xcode 5 using lldb, and I would like to evaluate arbitrary expressions in the debugger, particularly those that use overloaded operators.
For example, I created a very simple Xcode 5 C++ project with the following main.cpp and all compiler/linker/etc options set to the default:
#include <iostream>
#include <vector>
int main(int argc, const char * argv[])
{
std::vector<int> vec;
vec.push_back(42);
std::cout << "vec[0] = " << vec[0] << std::endl;
return 0;
}
I set a breakpoint on the return 0; line and ran the program.
Then, at the lldb prompt, printing the vector as a whole works fine:
(lldb) expr vec
(std::__1::vector<int, std::__1::allocator<int> >) $0 = size=1 {
[0] = 42
}
However, I can't access its members using the overloaded operator[]:
(lldb) expr vec[0]
error: call to a function 'std::__1::vector<int, std::__1::allocator<int> >::operator[](unsigned long)' ('_ZNSt3__16vectorIiNS_9allocatorIiEEEixEm') that is not present in the target
error: The expression could not be prepared to run in the target
Similarly, I can't get the iterator (though I have less experience here, so my syntax may be wrong):
(lldb) expr vector<int>::iterator it = vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
and
(lldb) expr (vector<int>::iterator) vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
Analogously, printing a simple string works fine:
(lldb) expr string("a")
(std::__1::string) $0 = "a"
However, a simple string concatenation fails:
(lldb) expr string("a") + string("b")
error: invalid operands to binary expression ('string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') and 'string')
error: 1 errors parsing expression
What am I doing wrong? Does lldb support evaluation with overloaded operators?
Thank you in advance!
I just ran into the same issue and apparently found a simple work-around.
You can access i-th element of a vector vec like this:
(lldb) p vec.__begin_[i]
(int) $1 = 100
Note that the C++ standard libraries are set up so that they inline all the templated functions that they can sensibly inline, and no real function copies exist. So for instance, when you go to call std::vector<int>::begin(), there is no such function. All its uses have been inlined.
That is why you are getting errors about "call to function... not present in target." There may be inlined copies of the function, but none we can actually call. As an example, if I build a little C++ program that makes a std::vector, and pushes some elements onto it and then iterates over them, and then do:
(lldb) image lookup -r -n begin
2 matches found in /private/tmp/vector:
Address: vector[0x0000000100000eaf] (vector.__TEXT.__text + 1071)
Summary: vector`main + 1071 [inlined] std::__1::vector<int, std::__1::allocator<int> >::begin() at vector.cpp:12
vector`main + 1071 at vector.cpp:12 Address: vector[0x0000000100000eaf] (vector.__TEXT.__text + 1071)
Summary: vector`main + 1071 [inlined] std::__1::vector<int, std::__1::allocator<int> >::begin() at vector.cpp:12
vector`main + 1071 at vector.cpp:12
So all the instances of the begin & end accessors for std::vector<int> are inlined. And further down in the part that comes from the std c library itself:
12 matches found in /usr/lib/libc++.1.dylib:
Address: libc++.1.dylib[0x000000000003e4ec] (libc++.1.dylib.__TEXT.__text + 252188)
Summary: libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::begin() Address: libc++.1.dylib[0x000000000003e51c] (libc++.1.dylib.__TEXT.__text + 252236)
Summary: libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::begin() const Address: libc++.1.dylib[0x000000000003e574] (libc++.1.dylib.__TEXT.__text + 252324)
and a few more for basic_string, and that's all. So there aren't any real implementations that we can call. Then once we've only got a little bit of the real world of these std objects available to us, the world falls apart in other odd ways as you start to push on it.
lldb isn't currently smart enough to figure out how to reconstitute a templated function/method from the C++ standard library's header files. We don't have enough of the environment in which your code was originally compiled to do that task.
Note that this isn't really a problem with overloaded operators, it is more a problem with the way the std libraries are used by the compiler. Things should work better for your own classes, where at -O0 there isn't so much inlining.
I have the following decleration in file Order.h (Holdng class Order) :
void removeFromAlbum(int barcode);
and the following Implementation line:
void Order::removeFromAlbum(int barcode)
But, when im trying to call the function with a different file, Store.cpp (Order.h was included) with the following line :
order.removeFromAlbum(barcode);
I get the following error from eclipse :
Invalid arguments '
Candidates are:
void removeFromAlbum(int)
'
Eclipse is well defined.
Any suggestions ?
Edit :
This is the function when all the magic happens :
void Store::removeFromOrder(int ordNum, int barcode)
barcode is an int.
EDIT: Before the OP's edit, it was impossible to tell if barcode was an integer. This answer may, therefore, be invalid:
It looks like you're trying to call the function removeFromAlbum() with a type that isn't an int. Ensure that barcode is actually cast as an int.