[Solved] Why compiler gives the message “call of overloaded function is ambiguous” here in C++11?

[ad_1] You have a member function: void func_A(int a, T initvalue = T()) {} that can be called like this: mc.func_A(1); Then you have an overloaded member function: void func_A(int a) {} that can also be called like this: mc.func_A(1); How could the compiler know which function you intend to call by that line? Answer: … Read more

[Solved] Function receiving different value than passed

[ad_1] At least part of the problem is that you are not understanding how operators work in C++. for(i=1;i<=n,completed==0;i++){ The expression i<=n,completed==0 has the effect of evaluating i <= n, discarding the result, then evaluating completed == 0, and giving the result of that. So the end condition of the loop is essentially completed == … Read more

[Solved] Template to determine the size in bytes of various variables

[ad_1] Solution in C++14, assuming I understood what you meant: template <class Container> constexpr auto byte_size(const Container& container) { using std::begin; using std::end; return (end(container) – begin(container)) * sizeof(container[0]); } Note that this will work even if the container is empty, since sizeof does not evaluate its operand. It won’t work for std::vector<bool> though—guess you’ll … Read more

[Solved] C++ vector of strings segfault

[ad_1] As panta-rei correctly pointed out, it looks like you’re trying to contain a string of the form “string” + string form of (i) but you’re actually doing pointer arithmetic which is illogical in this case (you’re just passing a pointer incremented i from some location – who knows what’s in that memory?). In order … Read more

[Solved] how I can compile templates c ++ 11, I can c ++ 14

[ad_1] Remove #include “ar.cpp” from main. You’re defining insert two times. There is no difference between the relevant rules in C++11 and C++14. You did something different between your test runs that has nothing to do with the language version. I also recommend not calling it .cpp. Conventionally, nothing you include should be called .cpp. … Read more

[Solved] file handling I/O c++ error

[ad_1] You have p as some pointer. Who is going to allocate the memory that pointer points to? In C it is almost always the responsibility of the caller to allocate any buffers before the call. If you don’t want to, then use a std::string instead. [ad_2] solved file handling I/O c++ error

[Solved] How to specify type of a constexpr function returning a class (without resorting to auto keyword)

[ad_1] The actual type is hidden as it’s local inside the function, so you can’t explicitly use it. You should however be able to use decltype as in decltype(create<int>()) v = create<int>(); I fail to see a reason to do like this though, when auto works. 1 [ad_2] solved How to specify type of a … Read more

[Solved] How does vector’s growth function work? [closed]

[ad_1] So how is the 10000th element stored here? The element isn’t stored in the vector. It’s ‘stored’ in a piece of memory that is unrelated to the vector. Isn’t the expected behavior here a “Segmentation fault”? No. The behaviour is undefined, so there is no behaviour to expect. But the above runs successfully. That’s … Read more

[Solved] N-Queens Algorithm using c++

[ad_1] The part in is_attaced in which you check the diagonals is not working. It has two basic problems, first you dont check all diagonals all rows – you just check the previous row. And, second, you are doing out of bound accesses, when you are in the first or last column. An implementation that … Read more

[Solved] why is cin/cout slower than scanf/ printf

[ad_1] The speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to std::ios::sync_with_stdio(false); By default standard C++ streams are synchronized to the standard C stream after each input/output operation. Once synchronization is turned off, the C++ standard streams are … Read more

[Solved] Dynamic Memory allocation fault

[ad_1] The issue is that n keeps growing, but your array does not. This code invokes undefined behavior, which thankfully caused a segfault for you: while(ch==’y’) { n++; cout<<“Enter 1 more element: “; cin>>arr[n]; cout<<“Want to enter more? “; cin>>ch; } arr has only been allocated to store n elements. Simply writing past the end … Read more