blank

blank
Last edited on
what does parsesymbol return? guessing its the automaton object... which can't be converted to bool unless you provide a bool() convert operator (you can do this if it makes sense).
Sorry, forgot to implement that function to the Original Post, its been updated. Reading your reply, I'm still puzzled on what to do next and I still have serious gaps in my C++ knowledge to understand what should be applied to which, so apologizes if I'm not accurate in my description or what to clarify later this is my first post here and I do need help.
Last edited on
Your problem is exhibited by this simpler, complete program:
1
2
3
4
5
int main()
{
  struct foo {} f; 
  if (auto x = f) /* do nothing */; 
}

Do you understand what is wrong with this program?
Last edited on
my int main () goes by this
1
2
3
4
5
int main() 
{
    // all chars would have their own independent panthesis removed, note it dosen't accept integers...
  Enumerate(Parse("(((a).(b)).((a)*)).((b)*)*").value());
}


You can worry about the commented part later, that's for me figure out, or perhaps someone here will probably figure out why this is the case. As for your question mobzzi, I've still yet to decipher if the program could even run with the errors still showing, especially in c++11
Last edited on
Searching up through the web, I've come across the following function from StackOverflow showcasing what the meaning of "operator bool() const" https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const:
1
2
3
4
operator bool() const 
  {
    return col != 0;
  }


and had put it inside my struct Automaton

so now it looks like:
1
2
3
4
5
6
7
8
9
10
struct Automaton 
{
  State *initial_state;
  State *final_state;
  operator bool() const 
  {
    return col != 0;
  }
};


This significantly reduced the amount errors now, but its still not over (as I still can't compile it yet, errors still exists), the pointers (*) are still showing the message:

"no operator "*" matches these operands"

and the operator bool() const function is a mystery to me as well as the variable col which is supposedly an int?
Last edited on
that is exactly what I said: that function casts your class to bool on demand, which makes the original problem clear up. The problem with bool cast function is what it means; does the function's answer do what you want and make sense? Only you can answer that, so you don't need to tell me, just be sure it fits your design. I don't like these cast operators for classes but on occasion they are a clean answer to a need.

I do not know what your col variable is supposed to mean. Its from someone else's program where that made sense to do: your bool operator needs to use your object's fields in some way that makes sense to your problem. Specifically: wtf does automaton == true or automaton == false really MEAN in this context? Take that knowledge and code the bool cast operation to give the true or false result that matches what you said to answer my question.

We need the latest code to see why operator * is borked, and where.

I still have serious gaps in my C++ knowledge

And we can help. But there are a great many issue in play here, and we need to weed them out and reduce the problem so you can understand what we tell you to do.
- I see issues where you seem to not know what you WANT to do. You can't code it if you don't know what you want.
- I see issues where you are copying and pasting without knowing what it means. You can do that to an extent, but eventually it burns you. If you don't understand, stop and research the question, the answer, and what the C++ is doing. Its ok to ask here too.

You look like you are trying to do some advanced things without knowing what you want from them or why you are doing them. That is ok, pushing is how we learn, but you have to expect to fail and have some confusion for a while as you work through it.
Last edited on
Yeah I'm trying to understand your point, but after messing it around, it seems the problem is seriously bigger than I thought that I would have to dig around, I've made another post on the General C++ Programming form describing my intent on posting this in the first place. I've been banging my head against this kind of roadblock for 3 days now that is part of a bigger project that I'm working on, so apologizes.

If you're still reading this, I'll be back online tomorrow morning at 6:30 AM
Last edited on
You keep mentioning pointers but your parse functions don't return pointers to Automaton objects. They simply return Automaton objects. No pointers.

This
1
2
3
4
if (auto maybe_a = ParsePrimitive(input))
{
    ...
}
is the same as
1
2
3
4
if (Automaton maybe_a = ParsePrimitive(input))
{
    ...
} 
which is basically the same as
1
2
3
4
5
Automaton maybe_a = ParsePrimitive(input);
if (maybe_a)
{
    ...
}

For this to work maybe_a needs to be convertible to bool. Pointers and integers (including chars) are convertible to bool but struct types are not convertible to bool by default. By defining an operator bool() you can make it convertible to bool but is this what you want?

If you want the parse functions to return pointers you should change the return type of your parse functions and instead return pointers (preferably smart pointers). Then you could return nullptr if the parsing failed which would be treated as false by the if statement.

If you want to define an operator bool() you should think about what it means for an Automaton to be "false". The example code that you found used col but Automaton doesn't contain a member with that name so instead you would probably want to use initial_state and/or final_state to decide whether the object is "false" or not.

Another option is to return a std::optional<Automaton>.


I noticed that many of your parse functions call syntax_error() without returning anything afterwards. This could be fine if syntax_error() throws an exception (or terminates the program immediately), but if it returns normally and the function ends without returning anything you're in trouble because reaching the end of a non-void function leads to undefined behaviour.

If the parse functions throw exceptions then you would check that with a try-catch statement. In that case you probably don't need to check the result of the parse functions with an if statement.
Last edited on
> I've come across the following function from StackOverflow
> and had put it inside my struct Automaton
ah, the old voodoo code


return *maybe_a; // no operator "*" matches these operands
no matter how much you believe it, maybe_a is not a pointer.
If you really don't understand what is wrong with the program in this post
https://cplusplus.com/forum/general/282478/#msg1222593

You'll need to take a step back and study some C++. Since you're obviously not a beginner to programming in general, it should only take a week or so.
Yes, I clearly should, thanks for the recommendation anyways
Let's look at the offending line:

if (auto maybe_a = Parse(input))

What this does is:

1. call the function Parse() with input as the sole argument
2. copy the object returned that function call, into an object of the same type called maybe_a
3. test whether maybe_a is true or false.

With me so far? If any of that isn't clear to you, then you really do need to go back to the very basics of C++ and start learning them.

Assuming that's clear, the problem is with that third part. maybe_a is an Automaton object, because that's what Parse() returns, according to line 50 of your code. But how can the code determine whether an Automaton object is true or false? What does it even mean for an Automaton to be "true" or "false"?

The compiler can't possibly know this. The only way it's possible to test an expression like this, is if the compiler knows how to convert the expression to a bool. And here, it doesn't know that. Because you haven't told it.

So, how do you tell the compiler? By implementing a conversion operator - i.e., by implementing a special function called operator bool() const that does the work of telling calling code whether the Automaton object is true or false.

What goes into the function? That's up to you. You have to decide what it means for an Automaton to be "true" or "false", and then you can write the code to implement that logic, and return the correct value (true or false) to the calling code.

Is that clearer now?
If you were a true beginning C++ coder, someone who had never done any coding, I'd suggest Learn C++ as a free resource to start the journey.
https://www.learncpp.com/

If'n you are someone who has done programming in another language and need some help and a leg up with C++ I'd recommend this:
https://hackingcpp.com/cpp/beginners_guide.html#intro
Topic archived. No new replies allowed.