most-vexing-parse

Absence of compilation error when using parametrized constructor

断了今生、忘了曾经 提交于 2021-02-04 16:53:07
问题 Today at work I came across a behavior in C++ which I don't understand. I have produced the following example code to illustrate my problem: #include <string> #include <iostream> class MyException { public: MyException(std::string s1) {std::cout << "MyException constructor, s1: " << s1 << std::endl;} }; int main(){ const char * text = "exception text"; std::cout << "Creating MyException object using std::string(const char *)." << std::endl; MyException my_ex(std::string(text)); std::cout <<

Unclear most vexing parse [duplicate]

一曲冷凌霜 提交于 2020-02-25 10:14:22
问题 This question already has answers here : Why does printing a function name returns a value? (3 answers) Closed 7 months ago . Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " << foo1.j << endl; // " 0 " } then I thought I'd call the default constructor with the following line but it doesn't: int main(void) {

Unclear most vexing parse [duplicate]

半城伤御伤魂 提交于 2020-02-25 10:14:09
问题 This question already has answers here : Why does printing a function name returns a value? (3 answers) Closed 7 months ago . Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " << foo1.j << endl; // " 0 " } then I thought I'd call the default constructor with the following line but it doesn't: int main(void) {

Calling class constructor with parameter - request of a member in 'x' which is of non-class type

只愿长相守 提交于 2020-02-24 03:56:46
问题 I have a class A that accepts class B as a constructor parameter. Class B can be constructed from int value. My original code is quite complex, but I hope I've reduced it to the very base case: class B { public: explicit B(int a) : val(a) {} private: int val; }; class A { public: A(const B & val) : value(val) {}; void print() { //does nothing } private: B value; }; int main() { int someTimeVar = 22; A a(B(someTimeVar)); a.print(); } And this is the error code I'm getting: $ g++ test.cpp -Wall

C++ function definition and variable declaration mismatch?

丶灬走出姿态 提交于 2020-01-21 11:47:06
问题 Consider this very simple code: #include <memory> class Foo { public: Foo() {}; }; class Bar { public: Bar( const std::shared_ptr<Foo>& foo ) {} }; int main() { Foo* foo = new Foo; Bar bar( std::shared_ptr<Foo>( foo ) ); return 0; } Why does Visual Studio reports warning C4930: 'Bar bar(std::shared_ptr<Foo>)': prototyped function not called (was a variable definition intended?) and there is no bar object created...how can this line Bar bar( std::shared_ptr<Foo>( foo ) ); be interpreted as a

C++11 Difference in Constructors (Braces)

删除回忆录丶 提交于 2020-01-21 11:43:37
问题 I am quite new to C++ and have observed, that the following lines of code act differently MyClass c1; c1.do_work() //works MyClass c2(); c2.do_work() //compiler error c2228: left side is not a class, structure, or union. MyClass c3{}; c3.do_work() //works with a header file as class MyClass { public: MyClass(); void do_work(); }; Can you explain me, what the difference between the three ways of creating the object is? And why does the second way produce a compiler error? 回答1: Ways one and

C++11 Difference in Constructors (Braces)

感情迁移 提交于 2020-01-21 11:42:09
问题 I am quite new to C++ and have observed, that the following lines of code act differently MyClass c1; c1.do_work() //works MyClass c2(); c2.do_work() //compiler error c2228: left side is not a class, structure, or union. MyClass c3{}; c3.do_work() //works with a header file as class MyClass { public: MyClass(); void do_work(); }; Can you explain me, what the difference between the three ways of creating the object is? And why does the second way produce a compiler error? 回答1: Ways one and

Cannot access vector when constructing with istream_iterator range

主宰稳场 提交于 2020-01-13 20:28:11
问题 I tried to compile this code snippet but I got compiler error :( ! Compile with Visual Studio 2010 #include <vector> #include <string> #include <sstream> #include <iterator> #include <iostream> using namespace std; int main() { string s( "Well well on" ); istringstream in( s ); vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() ); copy( v.begin(), v.end(), ostream_iterator<string>( cout, "\n" ) ); } Errors: Error 1 error C2228: left of '.begin' must have class/struct

Cannot access vector when constructing with istream_iterator range

依然范特西╮ 提交于 2020-01-13 20:28:06
问题 I tried to compile this code snippet but I got compiler error :( ! Compile with Visual Studio 2010 #include <vector> #include <string> #include <sstream> #include <iterator> #include <iostream> using namespace std; int main() { string s( "Well well on" ); istringstream in( s ); vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() ); copy( v.begin(), v.end(), ostream_iterator<string>( cout, "\n" ) ); } Errors: Error 1 error C2228: left of '.begin' must have class/struct

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

久未见 提交于 2020-01-11 09:35:12
问题 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