A strange error with definition and declaration of a member function - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 40 of 40

Thread: A strange error with definition and declaration of a member function

  1. #31
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: A strange error with definition and declaration of a member function

    1. Yes.

    2. Not quite. If you are doing it like this you need to make sure that you are not assigning to itself - otherwise you are deleting what is to be copied and you end up with an empty list. But why do it this way? Why duplicate the destructor and copy constructor code?

    3. This is a range based for loop. From the example, for each iteration of the loop l contains the next entry in the container. See http://www.learncpp.com/cpp-tutorial...static_assert/ In the example the type of l is automatically determined, is set to a ref to avoid a copy and is const as its contents are not being changed.

    4. d is of type iterator, so dereferencing d produces type Elem as required. So the code should be

    Code:
    for (auto d = N.begin(); d != N.end(); ++d) {
    			const Elem e = *d;
    			this->push_back(e);
    }
    5. No. It just calls the copy constructor. It doesn't first call the default constructor. But if you initialised the class variables when they are defined, as per some of my previous posts, then you don't need to initialise them in the class functions.
    Last edited by 2kaud; August 22nd, 2017 at 05:24 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  2. #32
    Join Date
    Jun 2015
    Posts
    149

    Re: A strange error with definition and declaration of a member function

    OK and thanks.
    Here:

    Code:
    Mylist& operator=(const Mylist& N) {    // Copy assignment
    		auto lhs = N;
    
    		std::swap(lhs.sz, sz);
    		std::swap(lhs.first, first);
    		std::swap(lhs.last, last);
    
    		return *this;
    }
    Isn't lhs just like N? I think no. Because its member variables are swapped by another. So auto lhs = N; is just a copy, yes, please? And shouldn't we delete that lhs after swap?

    Can't we call another constructor/destructor from/inside another constructor/destructor? For example, if it's possible, we could call the default constructor each time we needed for initializing member variables.

  3. #33
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: A strange error with definition and declaration of a member function

    lhs uses the copy constructor (auto lhs = N is a constructor not an assignment) to be a copy of N (complete with new storage allocation etc). So lhs.first points to a completely new list which is a copy of the original. Then the class member variables are swapped. So now lhs.first points to the original list and this->first points to the copy - which is what is wanted. When operator=() goes out of scope, the destructor for lhs is called which deletes the memory that was allocated for the original list (which is now referenced by lhs because of the swap) and everything works as expected. So the main 'work' is done by the copy constructor and the tidy up is done by the destructor.

    When a class variable is instigated, one of its constructor(s) is called as appropriate (which may be the default constructor) to initialise the class variables and any other required initialisation.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  4. #34
    Join Date
    Jun 2015
    Posts
    149

    Re: A strange error with definition and declaration of a member function

    Thank you.

    >> When operator=() goes out of scope, the destructor for lhs is called
    You meant our own destructor, ~Mylist<Elem>(), not?

    My previous question was:
    Can't we call another constructor/destructor from/inside another constructor/destructor? For example, if it's possible, we could call the default constructor each time we needed for initializing member variables.

    And one question on std::swap:
    I've included std_lib_facilities_4.h header file and also used using namespace std;, that is way I write cout << not std::cout <<, because it's not needed. But why do we need to use std:: in front of swap, please?

  5. #35
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: A strange error with definition and declaration of a member function

    But why do we need to use std:: in front of swap, please?
    Because swap has been defined as a class member function, the compiler needs to know its not that version to use but the one from the std namespace.
    If you change the name of the class function to be different from swap (eg listswap()), then you won't need to use std::swap().

    You meant our own destructor, ~Mylist<Elem>(), not?
    Yes.

    if it's possible, we could call the default constructor each time
    A constructor can call another constructor of the same class within its initialization list - including its default constructor or a base constructor if the class is derived. Consider

    Code:
    template<class Elem>
    Mylist<Elem>::Mylist(initializer_list<Elem> il) : Mylist()
    {
    This constructor first calls the default constructor. Note that you cannot do

    Code:
    template<class Elem>
    Mylist<Elem>::Mylist(initializer_list<Elem> il) {
        Mylist();
    However, default initialisations of member variables is better done using assignment when they are defined in the class.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  6. #36
    Join Date
    Jun 2015
    Posts
    149

    Re: A strange error with definition and declaration of a member function

    Thanks very much.

    As the last question of this topic, I want to use that specific list, Mylist, in other programs, maybe instead of std::list. In this program I only included <iostream>. I want to somehow will be able to include, say, the header file Mylist.h and use its possibilities.
    Is is enough if I create a header file named Mylist.h and put the Mylist's declarations into it and another file named Mylist.cpp and the definitions into this. Then put both files in the Include folder path of my Windows OS?

  7. #37
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: A strange error with definition and declaration of a member function

    Is is enough if I create a header file named Mylist.h and put the Mylist's declarations into it and another file named Mylist.cpp and the definitions into this. Then put both files in the Include folder path of my Windows OS?
    No. When using templates, the declarations and the definitions need to go into the same file. This is how the STL works. The whole code for Mylist needs to go into one file (say Mylist.hpp) which is included in other programs as needed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  8. #38
    Join Date
    Jun 2015
    Posts
    149

    Re: A strange error with definition and declaration of a member function

    Quote Originally Posted by 2kaud View Post
    The whole code for Mylist needs to go into one file (say Mylist.hpp) which is included in other programs as needed.
    I'm rather astonished by that extension, .hpp!
    What is that dear 2Kaud, please?

    I searched for *.hpp in the path below but no matching file!

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.10.25017\include
    I also found iostream there but apparently it is not shown what type it is!

    I think I'm being used to a new extension towards C++ programming.
    How to build such file and paste all the code into it for the future use, please?

  9. #39
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: A strange error with definition and declaration of a member function

    .hpp is an extension that is a header file that includes c++ source. Note that any file can be used in an #include statement - not just .h or .cpp files. Using .hpp differentiates it from a pure header file (.h).

    Since c++98 the STL include files don't have an extension. See the c/c++ section in https://en.wikipedia.org/wiki/Include_directive
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  10. #40
    Join Date
    Jun 2015
    Posts
    149

    Re: A strange error with definition and declaration of a member function

    Thank you very much. Your help is highly appreciated.

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)