most-vexing-parse

Visual Studio C++ compiler weird behaviour

不想你离开。 提交于 2019-12-02 21:47:25
I'm just curious to know why this small piece of code compiles correctly (and without warnings) in Visual Studio . Maybe the result is the same with GCC and Clang , but unfortunately I can't test them now. struct T { int t; T() : t(0) {} }; int main() { T(i_do_not_exist); return 0; } T(i_do_not_exist); is an object declaration with the same meaning as T i_do_not_exist; . N4567 § 6.8[stmt.ambig]p1 There is an ambiguity in the grammar involving expression-statement s and declaration s: An expression-statement with a function-style explicit type conversion (5.2.3) as its leftmost subexpression

Visual C++: No default constructor

两盒软妹~` 提交于 2019-12-02 03:39:40
问题 I've looked at a couple other questions asking this, but mine seems to be a lot simpler of a case then the ones I've been through, so I'll ask my case for this. Learn.h: #ifndef LEARN_H #define LEARN_H class Learn { public: Learn(int x); ~Learn(); private: const int favourite; }; #endif Learn.cpp: #include "Learn.h" #include <iostream> using namespace std; Learn::Learn(int x=0): favourite(x) { cout << "Constructor" << endl; } Learn::~Learn() { cout << "Destructor" << endl; } Source.cpp:

Why do anonymous objects sometimes require a default constructor?

三世轮回 提交于 2019-12-02 00:58:46
问题 If I write the following program, it works as I expect: struct Foo { Foo (std::string x) { std::cout << x << std::endl; } }; int main () { Foo("hello, world"); } However, if I write a slightly different program, I get a compilation error: struct Foo { Foo (std::string x) { std::cout << x << std::endl; } }; std::string x("hello, world"); int main () { Foo(x); } The error is: prog.cc: In function 'int main()': prog.cc:10:20: error: no matching function for call to 'Foo::Foo()' The complete

Visual C++: No default constructor

▼魔方 西西 提交于 2019-12-02 00:44:18
I've looked at a couple other questions asking this, but mine seems to be a lot simpler of a case then the ones I've been through, so I'll ask my case for this. Learn.h: #ifndef LEARN_H #define LEARN_H class Learn { public: Learn(int x); ~Learn(); private: const int favourite; }; #endif Learn.cpp: #include "Learn.h" #include <iostream> using namespace std; Learn::Learn(int x=0): favourite(x) { cout << "Constructor" << endl; } Learn::~Learn() { cout << "Destructor" << endl; } Source.cpp: #include <iostream> #include "Learn.h" using namespace std; int main() { cout << "What's your favourite

Why this statement does not call the constructors - C++

早过忘川 提交于 2019-12-01 23:40:57
A template class and a normal class: template <typename Type> class Holder { public: Holder(const Type& value) : held_(value) { cout << "Holder(const Type& value)" << endl; } Type& Ref() { return held_; } private: Type held_; }; class Animal { public: Animal(const Animal& rhs) { cout << "Animal(const Animal& rhs)" << endl; } Animal() { cout << "Animal()" << endl; } ~Animal() { cout << "~Animal" << endl; } void Print() const { cout << "Animal::Print()" << endl; } }; Then I want to instantiate a Holder<Animal> with this statement Holder<Animal> a(Animal()); , however, it fails. I mean Animal()

How is this a most vexing parse?

岁酱吖の 提交于 2019-12-01 18:49:48
I was going through this article and there is a statement in item 3 saying // C++98 rectangle w( origin(), extents() ); // oops, vexing parse how is the above a most vexing parse. If I did something like this struct origin { }; struct Rectangle { Rectangle(const origin& s) { } }; The statement Rectangle s(origin()); works fine and does not resemble a vexing parse. Why did the author say that its a vexing parse. Is that a typo or am I missing something ? M.M Rectangle s(origin()); is a vexing parse too. It declares a function s which returns rectangle , and takes as argument pointer to function

How to properly use a vector range constructor?

我的梦境 提交于 2019-12-01 05:11:10
I want to load all the lines from a text file into a vector<string by using its range constructor and then output them through cout : #include<iostream> #include<fstream> #include<vector> #include<iterator> using namespace std; int main() { ifstream file("file.txt"); vector<string> strings(istream_iterator<string>(file) , istream_iterator<string>()); for(auto s : strings) cout << s << endl; return 0; } When trying to compile the above code I get several errors, for instance: error: no matching function for call to ‘begin(std::vector<std::basic_string<char> > (&) (std::istream_iterator<std:

most vexing parse prevents in-class initializing a std::vector<int>

☆樱花仙子☆ 提交于 2019-12-01 03:16:43
C++11 allows in-class initialization: struct Foo{ std::vector<std::string> v{3}; // vector of 3 empty strings }; If we wanted to initialize in-class a vector of ints , we would get something else: struct Foo{ std::vector<int> v{3}; // vector of one element with value 3 }; This issue seems to be a limitation of the language, as discussed in previous questions . However, if this were not an in-class initialization, we would be able to use parentheses instead of braces, and get the desired result: std::vector<int> v(3); // vector of three zeros However, we cannot do this in a class because of

How to properly use a vector range constructor?

跟風遠走 提交于 2019-12-01 02:51:24
问题 I want to load all the lines from a text file into a vector<string by using its range constructor and then output them through cout : #include<iostream> #include<fstream> #include<vector> #include<iterator> using namespace std; int main() { ifstream file("file.txt"); vector<string> strings(istream_iterator<string>(file) , istream_iterator<string>()); for(auto s : strings) cout << s << endl; return 0; } When trying to compile the above code I get several errors, for instance: error: no

Default constructor c++

你。 提交于 2019-12-01 01:01:44
I am trying to understand how default constructor (provided by the compiler if you do not write one) versus your own default constructor works. So for example I wrote this simple class: class A { private: int x; public: A() { std::cout << "Default constructor called for A\n"; } A(int x) { std::cout << "Argument constructor called for A\n"; this->x = x; } }; int main (int argc, char const *argv[]) { A m; A p(0); A n(); return 0; } The output is : Default constructor called for A Argument constructor called for A So for the last one there is another constructor called and my question is which