Ambiguous FunctionOverloading in C++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a class A and it has two method foo which are actually overloaded. The class somewhat look like this
class A
{
public:
bool foo(int& a);
bool foo(size_t& a);
};
bool A::foo(int& a)
{
return true;
}
bool A::foo(size_t& a)
{
int new_a = a;
return foo(new_a); // here Cl throws me warning C4717: 'hweudm::UDMAbstractBaseEntity::SetAttribute' : recursive on all control paths, function will cause runtime stack overflow
}
int main()
{
A aObj;
size_t val = 12;
aObj.foo(val);
return 0;
}
From the code it does not look ambiguous. But I don't want to this warning during compilation. So can Anyone tell me
Why I am getting this warning even though I have type casted size_t to int ?
whether this will be be an error not a warning on GCC.

The code isn't correct. The result of the conversion-cast (int)a is an rvalue, and it cannot bind to either lvalue reference.
The only thing that would come close, if it weren't horribly undefined behaviour, would be something like foo(reinterpret_cast<int &>(a)), but don't do this, since it is not correct.

isn't size_t:
typedef unsigned int size_t;
? then A::foo(int& a) and A::foo(size_t& a) are exactly the same thing

Related

why is the const in the signature allowing the change in address of variable 'a' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
why is the const in the void change(int newValue,int *&what) const allowing change in address of 'a' when pointer to it is passed as ref. but this function is supposed to keep data of 'this' const. please help me where i am lacking my concept.
Note : please see comments in private change function..
#include <iostream>
using namespace std;
class A{
private:
int *a;
void change(int newValue,int *&what) const{
/*
//when what received as 'int * what'
a=new int(25);//not ok
what=new int(25);//ok as it does not change 'a'
*(this->a)=newValue;//ok
*what=newValue;//ok
*/
//when what received as 'int *&what'
//a=new int(25);//not ok
delete a;
what=new int(25);//ok but how ?
//*(this->a)=newValue;//ok
//*what=newValue;//ok
}
public:
A(){
a=new int(10);
}
void change(int newValue){
change(newValue,a);
}
void show(){
cout<<*a<<endl;
}
};
int main(){
A a;
a.change(15);
a.show();
return 0;
}
In this method:
void change(int newValue){
change(newValue,a);
}
... passing a into change(int, int*&) is allowed, because change(int) is not tagged as a const method, and therefore it is acceptable to pass a reference to a's natural type (int *) to the method it calls.
In this method:
void change(int newValue,int *&what) const{
[...]
what=new int(25); //ok but how ?
}
.... changing the value of what is allowed, because what is a non-const reference. The compiler has no way of knowing that what is actually pointing to a member-variable of this object. (Well, in this particular example it could know, but in the general case this change() method could be called from a different translation-unit/file, and in that scenario the compiler would not know anything about what what is aliasing)
So, rather than try to add a rule that would require the compiler to do full-program analysis to enforce consistently, the C++ committee decided to make the const-correctness rules apply on a per-method basis; that way, even if it allows some "const-violations in spirit" like this to slip through the cracks, the rules are more practical to implement and easier to understand.

How avoid error on not evaluated code C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Apologies for the most ambiguous and bizarre title.
Suppose we have 2 classes A and B.
class B has interface hasSmth but class A has not.
How to make this code evaluate without compile errors?
class A {
//..implementation
int id() { return 1; }
};
class B {
//..implementation
int id() { return 2; }
bool hasSmth() { return true; }
};
int main()
{
auto obj = someFunction();//returns A or B
if (obj.id() == 1 || (obj.id() == 2 && obj.hasSmth())) {
...
}
}
If the function returns obj of type B, then we are good.
But if it returns obj of type A, compiler will complain about A not having hasSmth, regardless of that part of if never been evaluated.
Can someone give a workaround please?
Can someone give a workaround please?
Read the declaration of someFunction to see what it returns. In the case it doesn't return B, then don't write obj.hasSmth(). Problem solved.
Now, let's change the question a bit. Let's say that you want to make this work without knowing the return type. Perhaps because rather than main you may be actually writing a template that works with different types. There are several approaches, but function overloads are a simple one:
bool check([[maybe_unused]] const A&) {
return true;
}
bool check(const B& b) {
return b.hasSmth();
}
template<bool returnsA>
void foo() {
auto obj = someTemplate<returnsA>(); // returns A or B
if (check(obj)) {

C++: struct accessing instance of itself [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have come across a rather unusual issue in my code. A struct needs to be able to access instances of itself.
Relavent portion of code:
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
vector<crtr> creatures[10];
Of course, this is nowhere close to working - crtr.foo() requires creatures, while creatures requires crtr. Is there some way to initialize creatures before crtr, perhaps changing the vectors' data type? (preferably with minimal pointers, if possible)
I must be missing something, what's wrong with this?
struct crtr {
char f;
void foo();
};
vector<crtr> creatures[10];
void crtr::foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
Also crtr::foo can be inline if that's required.
Use forward declaration of the struct:
struct crtr;
vector<crtr> creatures[10];
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};

unqualified id before a friend function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am getting the error below:
..\src\test.cpp:17:20: error: expected unqualified-id before 'try'
friend int try(Complex);
Please help and tell me why this code is producing errors:
#include <iostream>
using namespace std;
class Complex
{
private:
int a, b;
public:
showData()
{
cout<<"\na= "<<a<<" b= "<<b;
}
Complex(int x, int y)
{ //constructer
a = x;
b = y;
}
friend int try(Complex);
//friend function
};
int try(Complex c)
{
cout<<"You areworking in try now";
cout<<"\noutput from friend fun : "<<c.a<<" "<<c.b;
}
int main()
{
Complex c1(3, 4);
try(c1);
return 0;
}
First let's simplify the problem to a Minimal Complete and Verifiable example that duplicates the problem:
int try;
Not much code is required because try is a reserved word. You cannot use try in a program without the compiler expecting a try/catch` block.
Solution: Do not use try as an identifier. Instead use try_func or something that describes what is being tried.
Additional note: showData() needs a return type. Most likely it should be void showData()

Why we cannot declare a function argument static? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have been curious about why c++ does not allow declaring a static function argument as shown below:
int test(static int a )
{
return a;
}
int main()
{
test(5);
return 0;
}
output console shows:
error: storage class specifiers invalid in parameter declarations
error: storage class specified for parameter 'a'
Update #1:
I can achieve my requirements like follows:
int test(int a )
{
static int count = 0;// <-- I want to eliminate this line due to some project constraints.
count += a;
return count;
}
I cannot use passing arguments by reference if you suggest, I have already tried considering that option.
If there is any other way to accomplish above behavior, you're welcome.
Thanks.
To declare a function static you would do it as such
static int test(int a )
{
return a;
}
You are trying to pass in a "static int a" into a function but there's no reason to do that. You would instead declare
static int a;
somewhere in the class and then simply pass in a to the static method created above as so
test(a);