Error No Match For Ternary
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C++ increasing, decreasing numbers with “?:” up vote -1 down vote favorite I though it would be nice to learn how to make a code with "?:" in C++ that first increases number, when it reaches 100, it decreases, when it reaches 0, it increases again. Ok so to clarify: Start number: 1, should write 1 - 100, then at 100, 100, 99, 98, 97 to 0, and so on. #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Conditional operator issue up vote 8 down vote favorite 1 I'm having some trouble with using the conditional operator to get a reference to an object. http://stackoverflow.com/questions/9725868/c-increasing-decreasing-numbers-with I have the a setup similar to this: class D { virtual void bla() = 0; }; class D1 : public D { void bla() {}; }; class D2 : public D { void bla() {}; }; class C { public: C() { this->d1 = new D1(); this->d2 = new D2(); } D1& getD1() {return *d1;}; D2& getD2() {return *d2;} private: D1 *d1; D2 *d2; }; int main() { C c; D& d = (rand() %2 http://stackoverflow.com/questions/1807516/conditional-operator-issue == 0 ? c.getD1() : c.getD2()); return 0; } When compiling, this gives me the following error: WOpenTest.cpp: In function 'int main()': WOpenTest.cpp:91: error: no match for conditional 'operator?:' in '((((unsigned int)rand()) & 1u) == 0u) ? c.C::getD1() : c.C::getD2()' I understand this is illegal according to the C++ standard (as seen in this blog post), but I don't know how to get my reference to D without using the conditional operator. Any ideas? c++ conditional-operator share|improve this question edited Jan 30 '10 at 18:49 Ether 39.8k1065140 asked Nov 27 '09 at 8:52 laura 5,28512338 add a comment| 3 Answers 3 active oldest votes up vote 14 down vote accepted Cast to D& within both branches: D& d = (rand() %2 == 0 ? static_cast
big_bad_al Log in Or connect using: Facebook Twitter VK Google+ Mail.ru OpenID Error Username: Error Password: Forgot password? Remember me Log in Forgot password? Create an Account Your OpenID URL: Log in Alan (big_bad_al) wrote, http://big-bad-al.livejournal.com/95735.html 2008-04-11 02:44:00 Alan big_bad_al 2008-04-11 02:44:00 Previous Share Next C++ is bad: problems with https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52288 the ternary operator, addendum I've found another interesting wrinkle in the ternary operator. So, here's a bonus question for that quiz from last time. You start with the following code:class Abstract;class DerivedOne; // Inherits from Abstractclass DerivedTwo; // Inherits from AbstractAbstract x;DerivedOne one;DerivedTwo two;bool test;Again, you can assume that all of these are error no defined/initialized (and you can assume the comments are correct—both Derived* classes inherit from Abstract). Now, consider these snippets:QuestionNumberCode SnippetACode SnippetBBonusif (test) x = one;else x = two;x = (test ? one : two);If you read the previous installment, you've probably guessed by now that these are not equivalent, and you'd be right. In what ways do they differ? Do you have an answer? Better double check that.Alright, time's error no match up. It's a little bit of a trick question. Snippet B does not compile! Instead, it fails with an error: no match for ternary 'operator?:' in 'test ? one : two' (at least, that's what it says in GCC; your error message may be different). Section 5.16 of the C++ Standard states that the ternary operator will only work if, of the two values that can be returned, one is a subclass of the other (or one is an exception that gets thrown, or a couple other things like that). In other words, snippet B is not valid because DerivedOne is not a subclass of DerivedTwo or vice versa, regardless of whether they share a superclass. As before, I feel that this goes against the Principle of Least Surprise, and it bugs me. Tags: code, computer science, cpp, cpp is bad, software, ternary, ternary operator Post a new comment Error Comments allowed for friends only Anonymous comments are disabled in this journal We will log you in after post We will log you in after post We will log you in after post We will log you in after post We will log you in after post Anonymously switch LiveJour
filtered due to spam. Bug52288 - Trouble with operator?: and lambdas Summary: Trouble with operator?: and lambdas Status: UNCONFIRMED Alias: None Product: gcc Classification: Unclassified Component: c++ (show other bugs) Version: 4.6.1 Importance: P3 enhancement Target Milestone: --- Assignee: Not yet assigned to anyone URL: Keywords: diagnostic Depends on: Blocks: Reported: 2012-02-16 23:06 UTC by Ivan Godard Modified: 2014-05-09 18:29 UTC (History) CC List: 2 users (show) jason manu See Also: Host: Target: Build: Known to work: Known to fail: Last reconfirmed: Attachments Add an attachment (proposed patch, testcase, etc.) Note You need to log in before you can comment on or make changes to this bug. Description Ivan Godard 2012-02-16 23:06:11 UTC This code: int main(int argc, char** argv) { bool b; void* p = b ? [argc](int i){ return i; } : [argc](int i){ return i; }; return 0; } gets you this: s3:~/ootbc/personal/ivan$ g++ --std=c++0x foo.cc foo.cc: In function âint main(int, char**)â: foo.cc:5:28: error: no match for ternary âoperator?:â in âb ? {argc} : {argc}â which is a poor. Meanwhile this code: int main(int argc, char** argv) { bool b; void* p = b ? &[argc](int i){ return i; } : &[argc](int i){ return i; }; return 0; } gets you this: s3:~/ootbc/personal/ivan$ g++ --std=c++0x foo.cc foo.cc: In function âint main(int, char**)â: foo.cc:4:42: error: taking address of temporary [-fpermissive] foo.cc:5:29: error: taking address of temporary [-fpermissive] foo.cc:5:29: error: conditional expression between distinct pointer types âmain(int, char**)::