Thread: What is wrong with this C++ code?

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    4

    What is wrong with this C++ code?

    Code:
    #include "std_lib_facilities.h"
    
    
    class name_pairs
    {
    public:
      double age;
      string name;
    
    
      name_pairs() // constructor
      {
        string name; // empty string
        double age = double {}; // zero
        vector <string> names_vec = vector <string>{};
        vector <double> ages_vec = vector <double>{};
      }
    
    
      void read_ages(&name, n) // member function
      {
        double age;
        names_vec.push_back(n);
        cout << "Please enter an age for " << n << endl;
        cin >> age;
        ages_vec.push_back(age);
      }
    
    
    private:
      vector <string> names_vec;
      vector <double> ages_vec;
    };
    
    
    // driver program
    
    
    int main()
    {
      name_pairs object;
    
    
      cout << "Input a name - 'e' to exit" << endl;
      cin >> object.name;
    
    
      while(object.name != 'e')
        {
          cout << "Input an age: " << endl;
          cin >> object.age;
          object.read_ages(object.age);
          bool response = 0; // false
    
    
          cout << "Is there another name you wish to input? 1 for y; 0 for n " << endl;
          cin >> response;
          
    	if (response == 1) // 
    	  {
    	   cout << "Input a name - 'e' to exit:";
    	   cin >> object.name;
    	  }
    	else // response is false
    	  {
    	   cout << "Ending input now" << endl;
    	   object.name = 'e';
    	  }
        }
      return 0;
    }
    This should be simple. Im reading Programming Principles & Practice, chapter 9, exercise 2:

    Design and implement a Name_pairs class holding name, age pairs where name is a string and age is a double. Represent that as a vector<string> called name and a vector<double> cales age member.
    Provide an input operation read_names() that reads a series of names. Provide a read_ages() operation that prompts the user for an age for each name. Provide a print() operation (Not yet implemented) that prints out the name[i].age[i] pairs , one per line. in the porder determined by the name vector. provide a sort() operation - not yet implemented...
    Sadly the above code is what I have attempted to put together but it does not even compile. Can anyone help me?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Post the error message!
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    4

    Errors generated by code

    Quote Originally Posted by stahta01 View Post
    Post the error message!
    The errors are longer than the program code:

    Code:
     exercise2.cpp:17:19: error: C++ requires a type specifier for all declarations
      void read_ages(&name, n) // member function
                      ^
    exercise2.cpp:17:25: error: unknown type name 'n'
      void read_ages(&name, n) // member function
                            ^
    exercise2.cpp:20:25: error: use of undeclared identifier 'n'
        names_vec.push_back(n);
                            ^
    exercise2.cpp:21:43: error: use of undeclared identifier 'n'
        cout << "Please enter an age for " << n << endl;
                                              ^
    exercise2.cpp:40:21: error: invalid operands to binary expression ('std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') and 'int')
      while(object.name != 'e')
            ~~~~~~~~~~~ ^  ~~~
    /usr/include/c++/v1/system_error:606:1: note: candidate function not viable: no known conversion from 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'const std::__1::error_code' for 1st argument
    operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
    ^
    /usr/include/c++/v1/system_error:611:1: note: candidate function not viable: no known conversion from 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'const std::__1::error_code' for 1st argument
    operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
    ^
    /usr/include/c++/v1/system_error:616:1: note: candidate function not viable: no known conversion from 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'const std::__1::error_condition' for 1st argument
    operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
    ^
    /usr/include/c++/v1/system_error:621:1: note: candidate function not viable: no known conversion from 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'const std::__1::error_condition' for 1st argument
    operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
    ^
    /usr/include/c++/v1/utility:564:1: note: candidate template ignored: could not match 'pair' against 'basic_string'
    operator!=(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
    ^
    /usr/include/c++/v1/iterator:710:1: note: candidate template ignored: could not match 'reverse_iterator' against 'basic_string'
    operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    ^
    /usr/include/c++/v1/iterator:932:1: note: candidate template ignored: could not match 'istream_iterator' against 'basic_string'
    operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
    ^
    /usr/include/c++/v1/iterator:1033:6: note: candidate template ignored: could not match 'istreambuf_iterator' against 'basic_string'
    bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
         ^
    /usr/include/c++/v1/iterator:1151:1: note: candidate template ignored: could not match 'move_iterator' against 'basic_string'
    operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
    ^
    /usr/include/c++/v1/iterator:1440:5: note: candidate template ignored: could not match '__wrap_iter' against 'basic_string'
        operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT_DEBUG;
        ^
    /usr/include/c++/v1/iterator:1556:1: note: candidate template ignored: could not match '__wrap_iter' against 'basic_string'
    operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT_DEBUG
    ^
    /usr/include/c++/v1/tuple:1152:1: note: candidate template ignored: could not match 'tuple' against 'basic_string'
    operator!=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
    ^
    /usr/include/c++/v1/memory:1965:6: note: candidate template ignored: could not match 'allocator' against 'basic_string'
    bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
         ^
    /usr/include/c++/v1/memory:2915:1: note: candidate template ignored: could not match 'unique_ptr' against 'basic_string'
    operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
    ^
    /usr/include/c++/v1/memory:2962:1: note: candidate template ignored: could not match 'unique_ptr' against 'basic_string'
    operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
    ^
    /usr/include/c++/v1/memory:2970:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'char'
    operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
    ^
    /usr/include/c++/v1/memory:4750:1: note: candidate template ignored: could not match 'shared_ptr' against 'basic_string'
    operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
    ^
    /usr/include/c++/v1/memory:4807:1: note: candidate template ignored: could not match 'shared_ptr' against 'basic_string'
    operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
    ^
    /usr/include/c++/v1/memory:4815:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'char'
    operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
    ^
    /usr/include/c++/v1/string_view:640:6: note: candidate template ignored: could not match 'basic_string_view' against 'basic_string'
    bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
         ^
    /usr/include/c++/v1/string_view:649:6: note: candidate template ignored: could not match 'basic_string_view' against 'basic_string'
    bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
         ^
    /usr/include/c++/v1/string_view:659:6: note: candidate template ignored: could not match 'basic_string_view<type-parameter-0-0, type-parameter-0-1>' against 'char'
    bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs, 
         ^
    /usr/include/c++/v1/string:540:6: note: candidate template ignored: could not match 'fpos' against 'basic_string'
    bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
         ^
    /usr/include/c++/v1/string:3621:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'char'
    operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
    ^
    /usr/include/c++/v1/string:3630:1: note: candidate template ignored: could not match 'const _CharT *' against 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
    operator!=(const _CharT* __lhs,
    ^
    /usr/include/c++/v1/string:3639:1: note: candidate template ignored: could not match 'const _CharT *' against 'char'
    operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
    ^
    /usr/include/c++/v1/functional:1965:1: note: candidate template ignored: could not match 'function' against 'basic_string'
    operator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
    ^
    /usr/include/c++/v1/functional:1970:1: note: candidate template ignored: could not match 'function<type-parameter-0-0 (type-parameter-0-1...)>' against 'char'
    operator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
    ^
    /usr/include/c++/v1/list:2379:1: note: candidate template ignored: could not match 'list' against 'basic_string'
    operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
    ^
    /usr/include/c++/v1/forward_list:1673:6: note: candidate template ignored: could not match 'forward_list' against 'basic_string'
    bool operator!=(const forward_list<_Tp, _Alloc>& __x,
         ^
    /usr/include/c++/v1/vector:3318:1: note: candidate template ignored: could not match 'vector' against 'basic_string'
    operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
    ^
    /usr/include/c++/v1/unordered_map:1485:1: note: candidate template ignored: could not match 'unordered_map' against 'basic_string'
    operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
    ^
    /usr/include/c++/v1/unordered_map:2035:1: note: candidate template ignored: could not match 'unordered_multimap' against 'basic_string'
    operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
    ^
    /usr/include/c++/v1/array:241:1: note: candidate template ignored: could not match 'array' against 'basic_string'
    operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
    ^
    /usr/include/c++/v1/deque:2860:1: note: candidate template ignored: could not match 'deque' against 'basic_string'
    operator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
    ^
    /usr/include/c++/v1/regex:4834:1: note: candidate template ignored: could not match 'sub_match' against 'basic_string'
    operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
    ^
    /usr/include/c++/v1/regex:4883:1: note: candidate template ignored: could not match 'sub_match<type-parameter-0-0>' against 'char'
    operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
    ^
    /usr/include/c++/v1/regex:4936:1: note: candidate template ignored: could not match 'sub_match' against 'basic_string'
    operator!=(const sub_match<_BiIter>& __x,
    ^
    /usr/include/c++/v1/regex:4989:1: note: candidate template ignored: could not match 'sub_match<type-parameter-0-0>' against 'char'
    operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
    ^
    /usr/include/c++/v1/regex:5043:1: note: candidate template ignored: could not match 'sub_match' against 'basic_string'
    operator!=(const sub_match<_BiIter>& __x,
    ^
    /usr/include/c++/v1/regex:5098:1: note: candidate template ignored: could not match 'sub_match<type-parameter-0-0>' against 'char'
    operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
    ^
    /usr/include/c++/v1/regex:5154:1: note: candidate template ignored: could not match 'sub_match' against 'basic_string'
    operator!=(const sub_match<_BiIter>& __x,
    ^
    /usr/include/c++/v1/regex:5527:1: note: candidate template ignored: could not match 'match_results' against 'basic_string'
    operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
    ^
    /usr/include/c++/v1/random:2439:1: note: candidate template ignored: could not match 'mersenne_twister_engine' against 'basic_string'
    operator!=(const mersenne_twister_engine<_UInt, _Wp, _Np, _Mp, _Rp, _Ap, _Up, _Dp, _Sp,
    ^
    /usr/include/c++/v1/random:2763:1: note: candidate template ignored: could not match 'subtract_with_carry_engine' against 'basic_string'
    operator!=(
    ^
    /usr/include/c++/v1/random:2946:1: note: candidate template ignored: could not match 'discard_block_engine' against 'basic_string'
    operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
    ^
    /usr/include/c++/v1/random:3198:1: note: candidate template ignored: could not match 'independent_bits_engine' against 'basic_string'
    operator!=(
    ^
    /usr/include/c++/v1/random:3430:1: note: candidate template ignored: could not match 'shuffle_order_engine' against 'basic_string'
    operator!=(
    ^
    5 errors generated.

  4. #4
    Guest
    Guest
    Do you come from another programming language? You should have learned about function calls and scopes before touching the class construct. Go back to the section about functions and how to define function parameters. A free resource like learncpp.com will work too.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    4
    The function is passed a reference that is represented by n. It doesn’t return anything. That’s seems simple enough.

    I’m not trying to learn about functions. I just asked for help with a class which is what I am trying to learn about.

    If I thought going back and reading a book again was the solution I would not have asked this question here. But thanks anyway. I will seek to discuss this elsewhere and won’t bother you again

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Khufu
    The function is passed a reference that is represented by n. It doesn’t return anything. That’s seems simple enough.
    Yes, except that your problem has to do with you mixing up a function declaration versus a function call.

    Quote Originally Posted by Khufu
    The errors are longer than the program code:
    Thankfully, multiple errors are sometimes due to the same mistake. Take these four errors for example:
    Code:
    exercise2.cpp:17:19: error: C++ requires a type specifier for all declarations
      void read_ages(&name, n) // member function
                      ^
    exercise2.cpp:17:25: error: unknown type name 'n'
      void read_ages(&name, n) // member function
                            ^
    exercise2.cpp:20:25: error: use of undeclared identifier 'n'
        names_vec.push_back(n);
                            ^
    exercise2.cpp:21:43: error: use of undeclared identifier 'n'
        cout << "Please enter an age for " << n << endl;
                                              ^
    The above four are all due to the same mistake on this line:
    Code:
      void read_ages(&name, n) // member function
    You're trying to define (and hence declare) a member function named read_ages, and so you cannot pass arguments to the function. You could declare parameters instead, e.g.,
    Code:
      void read_ages(const string &name, double n) // member function
    However, while this might permit your code to compile, it is still wrong because name is actually unnecessary: read_ages is supposed to "prompts the user for an age for each name", i.e., read_ages should make use of the names_vec member variables, not have a parameter. Likewise, n is actually unnecessary: read_ages should append to the ages_vec member variable (and you did do that, except that you need to do it in a loop that loops over the names). Note that you should not be calling push_back on names_vec in read_ages: that should have been done in read_names.

    Quote Originally Posted by Khufu
    I’m not trying to learn about functions. I just asked for help with a class which is what I am trying to learn about.
    Note that this is basic function declaration/definition versus a function call, which is why Guest told you that "You should have learned about function calls and scopes before touching the class construct". It has nothing to do with your defining a member function in a class: you would get a compile error making the same mistake defining a non-member function too.

    The next error is likewise fairly simple:
    Code:
    exercise2.cpp:40:21: error: invalid operands to binary expression ('std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') and 'int')
      while(object.name != 'e')
            ~~~~~~~~~~~ ^  ~~~
    All this means is that you tried to compare a string with a char (promoted to int). You should have written:
    Code:
    while(object.name != "e")
    This should fix the error messages that follow too: they are basically telling you that they tried to match with other possible operations, but couldn't find a match.

    That said, you should get rid of the age and name member variables: these should be local variables instead. You only need names_vec and ages_vec to be member variables (except maybe until you want to sort... you might need to introduce another member vector).
    Last edited by laserlight; 11-07-2019 at 05:11 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Guest
    Guest
    Code:
    exercise2.cpp:17:19: error: C++ requires a type specifier for all declarations
      void read_ages(&name, n) // member function
                      ^
    exercise2.cpp:17:25: error: unknown type name 'n'
      void read_ages(&name, n) // member function
    The error tells you in no unspecific terms what's wrong. It just doesn't make sense that you're struggling with this while writing methods - this is a running before walking scenario. That's why I asked if you're coming from another language and jumped into this head first.

    What you may have wanted is something like:
    Code:
    void read_ages(const std::string& name)
    {
        names_vec.push_back(name);
        double age;
        cout << "Please enter an age for " << name << endl;
        cin >> age;
        ages_vec.push_back(age);
    }
    But that wouldn't actually help you because there are several other problems waiting and they're due to not being familiar with certain concepts yet. Nobody can fill that gap except you.
    Last edited by Guest; 11-07-2019 at 05:22 PM. Reason: formatting

  8. #8
    Registered User
    Join Date
    Nov 2019
    Posts
    4
    Thank you both laser light & Adrian - for taking time to give me answers that have helped me to resolve this problem.
    Last edited by Khufu; 11-08-2019 at 04:21 PM. Reason: Spelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this C code?
    By sunil9211 in forum C Programming
    Replies: 1
    Last Post: 05-12-2018, 12:58 AM
  2. What's wrong with this code?
    By andysalter in forum C Programming
    Replies: 2
    Last Post: 04-03-2013, 05:37 AM
  3. what is wrong in my code?
    By rac1 in forum C Programming
    Replies: 12
    Last Post: 12-25-2011, 02:58 PM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  5. What's wrong with this code???
    By /\/\ E /\/ @ in forum C++ Programming
    Replies: 1
    Last Post: 07-25-2002, 12:55 AM

Tags for this Thread