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 years ago.
Improve this question
i find this construction:
std::map<T, T> a = boost::assign::map_list_of(arg11, arg12)
(arg21, arg22)
;
Please, tell what happens in this and how to realize it on c++?
I didn't read the implementation, bu I think you can reproduce that with a "Accumulator" object :
template<class T>
class Acc
{
public:
Acc &operator()(T a, T b)
{
map.insert(std::make_pair(a, b));
}
operator std::map<T, T>()
{
return map;
}
private:
std::map<T, T> map;
}
Each time you do something like (a,b), you call the operator(), which just add the std::pair(a, b) inside the map. The cast operator allow to get a std::map instead of the Acc object.
The use is :
std::map<int, int> = Acc<int>()(1,2)(3,4)(6,7);
Nb : Code not tested.
Related
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)) {
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 3 years ago.
Improve this question
I do know how to access member variables given a vector of objects but suppose
if I have a class called "layer" that is
class layer{
public:
layer(.... that initializes "val" .... );
vector<vector<double>> getval(){return val;}
private:
vector<vector<double>> val;
}
and then suppose there is another class that is
class Net{
public:
Net( ..... that initializes "nn" ..... );
vector<layer> getnn(){ return nn; }
private:
vector<layer> nn;
}
So in the main function, I could create an object like
Net n( ....... )
and in the main function I could get vector of objects via
n.getnn();
but the question is how could I get the specific, given i index,
vector<vector<double>> val
at nn[i]
float value = n.getnn()[i].getval()[j][k];
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 5 years ago.
Improve this question
This is my code.(I simplified this,Normally there were a lot of member functions,but error still same,So I simplified it.)
template <class K,class V>
class MyMap:public MySet<pair<K, V> >{};
int main(void){
MyMap<int,int> map1;
MyMap<int,int>::MyIterator it;
it=map1.begin();
cout<<it->first<<endl;
return 0;
}
You need to provide the -> operator to MyIterator class, something as this:
T *operator->() {
return data;
}
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
#include <iostream>
using namespace std;
Class ITEM{
private:
int cnt;
public:
ITEM(){}
void func(ITEM a){
a.cnt = 10;
}
};
int main(){
return 0;
}
I assume that red line will make the error.
because 'a.cnt' value is private value.
I learned that private value must be modified inner of class.
void func(A a){
a.cnt = 10; //valid
}
notice that function is inside the class, so it becomes it member and class member functions have access to private members.
Note that it is being modified inner of the class ITEM using a member function,which is perfectly valid
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