c++-faq

Why should I not #include <bits/stdc++.h>?

主宰稳场 提交于 2021-01-29 08:42:33
问题 I posted a question with my code whose only #include directive was the following: #include <bits/stdc++.h> My teacher told me to do this, but in the comments section I was informed that I shouldn't. Why? 回答1: Including <bits/stdc++.h> appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year. I imagine the advantages are vaguely given thus: You only need write one #include line You do not need to

Why can templates only be implemented in the header file?

陌路散爱 提交于 2021-01-29 07:52:58
问题 Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.) 回答1: Caveat: It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer. Anyway, the reason your code is failing is that,

What is an undefined reference/unresolved external symbol error and how do I fix it?

给你一囗甜甜゛ 提交于 2021-01-29 05:26:03
问题 What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? Feel free to edit/add your own. 回答1: Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote] . Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set

Can I add a deduction guide to `std` namespace?

十年热恋 提交于 2020-05-29 03:45:26
问题 Suppose I want to make a new deduction guide making the following possible ? std::string str; std::basic_string_view sv = str; Would that be an Ok customization ? 回答1: [namespace.std]/2.4: The behavior of a C++ program is undefined if it declares [...] a deduction guide for any standard library class template. 来源: https://stackoverflow.com/questions/58992794/can-i-add-a-deduction-guide-to-std-namespace

What is an undefined reference/unresolved external symbol error and how do I fix it?

心已入冬 提交于 2020-05-17 06:06:23
问题 What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? Feel free to edit/add your own. 回答1: Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote] . Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set

Why can templates only be implemented in the header file?

孤者浪人 提交于 2020-03-21 07:01:45
问题 Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.) 回答1: Caveat: It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer. Anyway, the reason your code is failing is that,

Why can templates only be implemented in the header file?

若如初见. 提交于 2020-03-21 07:01:11
问题 Quote from The C++ standard library: a tutorial and handbook: The only portable way of using templates at the moment is to implement them in header files by using inline functions. Why is this? (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.) 回答1: Caveat: It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer. Anyway, the reason your code is failing is that,

C++ delete - It deletes my objects but I can still access the data?

微笑、不失礼 提交于 2020-03-05 09:31:32
问题 I have written a simple, working tetris game with each block as an instance of a class singleblock. class SingleBlock { public: SingleBlock(int, int); ~SingleBlock(); int x; int y; SingleBlock *next; }; class MultiBlock { public: MultiBlock(int, int); SingleBlock *c, *d, *e, *f; }; SingleBlock::SingleBlock(int a, int b) { x = a; y = b; } SingleBlock::~SingleBlock() { x = 222; } MultiBlock::MultiBlock(int a, int b) { c = new SingleBlock (a,b); d = c->next = new SingleBlock (a+10,b); e = d-

Where and why do I have to put the “template” and “typename” keywords?

徘徊边缘 提交于 2020-03-03 06:57:46
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

Why does std::getline() skip input after a formatted extraction?

落花浮王杯 提交于 2020-01-28 10:16:40
问题 I have the following piece of code that prompts the user for their name and state: #include <iostream> #include <string> int main() { std::string name; std::string state; if (std::cin >> name && std::getline(std::cin, state)) { std::cout << "Your name is " << name << " and you live in " << state; } } What I find is that the name has been successfully extracted, but not the state. Here is the input and resulting output: Input: "John" "New Hampshire" Output: "Your name is John and you live in "