std

Strange C++ compile error with valarrays

半城伤御伤魂 提交于 2019-12-13 14:47:22
问题 I have a strange compile error using valarrays in C++. This is a stripped down version of my code: #include <iostream> #include <valarray> using namespace std; bool test(const int &x,const valarray<int> &a,const valarray<int> &b) { return a*x==b; } int main() { int a1[3]= {1,2,3}; int b1[3]= {2,4,6}; valarray<int> a(a1,3); valarray<int> b(b1,3); int x=2; cout<<test(x,a,b); return 0; } Expected behavior: outputs some variant of true or 1 The compile error (using g++): main.cpp: In function

assigning true/false to std::string: what's going on?

最后都变了- 提交于 2019-12-13 12:24:26
问题 I was testing a c++11 compiler on my source code and it caught an error in one of my functions that I would have expected my non c++11 compiler to catch as well. I was returning false from a function that has a return type of std::string... Here's the code that demonstrates the problem #include <iostream> int main ( ) { std::string str = false; std::cerr << "'" << str << "'" << std::endl; return 0; } $ g++ test.cpp -W -Wall -Wextra $ ./a.out terminate called after throwing an instance of 'std

Overload operator>> for std::pair<int, int>

不打扰是莪最后的温柔 提交于 2019-12-13 11:42:43
问题 I am trying to use boost::lexical_cast on a std::pair<int, int> . #include <iostream> #include <utility> #include <boost/lexical_cast.hpp> namespace my { // When my_pair is a user defined type, this program compiles // and runs without any problems. // When declaring my_pair as an alias of std::pair<int, int>, // it fails to compile /* struct my_pair { int first; int second; }; */ using my_pair = std::pair<int, int>; std::istream& operator>>(std::istream& stream, my_pair& pair) { stream >>

What would 'std:;' do in c++?

断了今生、忘了曾经 提交于 2019-12-13 11:33:38
问题 I was recently modifying some code, and found a pre-existing bug on one line within a function: std:;string x = y; This code still compiles and has been working as expected. The string definition works because this file is using namespace std; , so the std:: was unnecessary in the first place. The question is, why is std:; compiling and what, if anything, is it doing? 回答1: std: its a label, usable as a target for goto . As pointed by @Adam Rosenfield in a comment, it is a legal label name. C+

c++ char* + std::vector memory leak [closed]

﹥>﹥吖頭↗ 提交于 2019-12-13 11:26:57
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . The following code is reading a big object collection (95G of compressed objects that are uncompressed via the WriteObject streamer) from disk and prints their content as strings. object.cxx: std::vector<char> ObjectHandler::GetObject(const std::string& path) { TFile *file = new TFile(path.c_str()); // If file

Creating std::map with a structure as its key and value [duplicate]

做~自己de王妃 提交于 2019-12-13 11:04:37
问题 This question already has answers here : Error using custom operator< with std::less (2 answers) Closed last year . I want to define a static map inside a class, which will have a structure as both it's key and value. I've read that a '<' operator is needed to be defined, therefore I've included that definition inside the struct itself(I've randomly defined it as I don't need any comparison anywhere in my program). The below sample code doesn't compile.The compiler produces a lot of warnings

placement new on shared_ptr make seg fault when delete [duplicate]

社会主义新天地 提交于 2019-12-13 08:04:00
问题 This question already has answers here : Closed 6 years ago . Edit Solution:: In fact, i juste forget the placment new in the copy constructor ><" Question: I have a weird problem. After having tried for a long momnet origin I found masi does not understand. If someone can explain to me why. My class: class B; //on other file class A { public: A(int type) : type(type) { switch(type) { case TOKEN: { for(int i=0;i<4;++i) new(&token.h[i].link) shared_ptr<B>; //< init the ptr on the addr (because

Understanding the space occupied by an vector instance in C++

偶尔善良 提交于 2019-12-13 05:47:50
问题 I'm trying to understand the amount of bytes occupied by an instance of std::vector. The following code: vector <int>uno; uno.push_back(1); uno.push_back(1); cout <<"1: "<< sizeof(uno)<<" bytes"<<endl; cout << endl; vector <bool>unos; unos.push_back(true); cout <<"2: "<< sizeof(unos)<<" bytes"<<endl; cout << endl; gives me this output: 1: 12 bytes 2: 20 bytes Can someone explain me why the size of the vector<bool> has more bytes than the vector<int> ?. And what is the correct way to measure

Link error using std::vector

天大地大妈咪最大 提交于 2019-12-13 03:37:59
问题 I'm having a problem with a vector declaration. Here's the code: .h #ifndef ANIMATEDSPRITE_H_ #define ANIMATEDSPRITE_H_ #include "Sprite.h" #include <vector> //using namespace std; class AnimatedSprite //abstract class to point sprites { public: AnimatedSprite(); ~AnimatedSprite(); //gets and sets Sprite GetMySprite(int _index); void SetSpriteToList(Sprite _sprite); int GetState() const; void SetState(int _state); //other private: std::vector<Sprite> spriteList; int state; //estado que esse

Why std::string does not have (explicit) const char* cast

青春壹個敷衍的年華 提交于 2019-12-13 02:55:30
问题 I like to know pro's and con's for having and not-having such cast. At several places including here on Stack Overflow I can see that the const char* cast is considered bad idea but I am not sure why? Lack of the (const char*) and forcing to always use c_str() cast creates some problems when writing generic routines and templates. void CheckStr(const char* s) { } int main() { std::string s = "Hello World!"; // all below will not compile with // Error: No suitable conversion function from "std