Thread: compilation error in a simple program.

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    13

    compilation error in a simple program.

    Hello,

    Trying to pick up C++ in my spare time. I wrote a very basic program but I am getting a compiler error upon compilation.

    here is my code. I think the initializer list initialization of the vector is causing the issue. Can someone point out my mistake?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class person
    {
    private:
        std::string name = nullptr;
        std::vector<person> friends;
    
    public:
    
        person() : name() {}; // default constructor
        person(std::string name, std::vector<person> friendlist) : name(name), friends(friendlist) {};
    
    };
    
    
    int main()
    {
        person p1();
        person p2();
        vector<person> v{ p1, p2 };
        person p3("Tom", v);
        
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    This declares a variable:
    Code:
    person p1;
    This declares a function:
    Code:
    person p1();
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Ah! ok changed it to below and its working now. Thanks a lot.



    Code:
    person p1{};
    person p2{};

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you trying to assign nullptr to name? Name is a string not a pointer and only a pointer can be assigned to a nullptr.


    You seem to be using a fairly recent compiler so I would recommend something more like:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
     
    using namespace std;
     
    class person
    {
        private:
            std::string name;
            std::vector<person> friends;
        public:
            person() = default; // Let the compiler handle the default constructor.
            person(std::string name, std::vector<person> friendlist) : name(name), friends(friendlist) {};
    };
    
    int main()
    {
        person p1;  // No need for any brackets or parentheses.
        person p2;
        vector<person> v{ p1, p2 };
        person p3{"Tom", v}; // You used brackets above, so stay consistent use brackets here also.
         
    }

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Quote Originally Posted by jimblumberg View Post
    Why are you trying to assign nullptr to name? Name is a string not a pointer and only a pointer can be assigned to a nullptr.
    I thought since string is internally const char * underneath, its a good idea to initialize it to nullptr. If we don't do that, what does the string point to by default? Also, same query applies to the vector. if we don't set a default value to it, what will the vector contents be?


    You seem to be using a fairly recent compiler so I would recommend something more like:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
     
    using namespace std;
     
    class person
    {
        private:
            std::string name;
            std::vector<person> friends;
        public:
            person() = default; // Let the compiler handle the default constructor.
            person(std::string name, std::vector<person> friendlist) : name(name), friends(friendlist) {};
    };
    
    int main()
    {
        person p1;  // No need for any brackets or parentheses.
        person p2;
        vector<person> v{ p1, p2 };
        person p3{"Tom", v}; // You used brackets above, so stay consistent use brackets here also.
         
    }
    Yes. This makes sense. I will try to be more consistent with the brackets.

    person() = default; // Let the compiler handle the default
    I was reading about the = default next to the constructor. So all it does is make a default constructor for us. But when there is another constructor with parameters available, I was reading that the default constructor won't be generated by the compiler. So what is the use of using this = default in this case?

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    A vector and a string are empty(i.e. size() == 0, empty() == true when you use their default ctor.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I thought since string is internally const char * underneath, its a good idea to initialize it to nullptr.
    No, since a string is not a pointer it is a bad idea to initialize it to a nullptr.

    If we don't do that, what does the string point to by default?
    It doesn't "point" to anything, only a pointer "points". When you use the default constructor you get an empty string.

    if we don't set a default value to it, what will the vector contents be?
    All of the standard containers will be empty when you use the default constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compilation error in simple function
    By livin in forum C Programming
    Replies: 5
    Last Post: 04-17-2011, 09:07 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Simple C++ Compilation Question
    By mercury529 in forum C++ Programming
    Replies: 14
    Last Post: 07-30-2006, 09:40 PM
  5. error on compilation of an allegro program
    By paresh in forum Linux Programming
    Replies: 5
    Last Post: 01-18-2005, 02:27 PM

Tags for this Thread