C++ How to call a void function without errors?

  • A+
Category:Languages

This is an assignment of mine that I picked to do but I am not sure how to fix the error message I am getting at cout << contact.getInformation() << endl;without changing the Void function to a different type or changing the main function (which I am trying to avoid). I think my lack of understanding is in how cout and void functions work together. I tried to remove the cout from the function but that did not work and the only way I could make the code run was when I replaced cout << contact.getInformation() << endl; with contact.getInformation() which I am trying to avoid. I just want the inside of the void function to print when I call cout << contact.getInformation() << endl; Any help is welcome! Thank you!

#include <stdio.h> #include <iostream> #include <string>  using namespace std;  class Contact{  public:     Contact(int id, string name, string telephone, int age)     : _id{ id }, _name{ name }, _telephone{ telephone }, _age{ age } {}      int id() { return _id; }     string name() { return _name; }     string telephone() { return _telephone; }     int age() { return _age; }      void getInformation() {         cout << "ID: " + to_string(_id) + "/n" +         "NAME: " + _name + "/n" +         "TEL: " + _telephone + "/n" +         "AGE: " + to_string(_age) + "/n";     } private:     int _id;     string _name;     string _telephone;     int _age;  };  int main() {     Contact contact{1, "Michael", "555-555-5555", 15};     cout << contact.getInformation() << endl; }.  

EDIT: Thanks all! I see now that it is impossible to do with those restrictions.

 


The code you've provided have many issues. You can avoid them if you read some good C++ book, my advice is Scott Meyers Effective C++: 55 Specific Ways to Improve Your Programs and Designs.

  1. don't use using directive unless really necessary. In most cases for std namespace - it is not.
  2. Pass function arguments of non primitive type by reference/const reference rather by value or pointer
  3. Understand const keyword and it usage
  4. Understand constructor static initialization bocks
  5. Understand c++ streams

This is how you code should looks like:

#include <iostream> #include <string>  class Contact {  public:     Contact(int id,const std::string& name,const std::string& telephone, int age):         _id( id ),         _name( name ),         _telephone( telephone ),         _age( age )     {}      int id() const {         return _id;     }     std::string name() const {         return _name;     }     std::string telephone() const {         return _telephone;     }     int age() const {         return _age;     }   private:     int _id;     std::string _name;     std::string _telephone;     int _age;  };  std::ostream& operator<<(std::ostream& to,const Contact& c) {     to << "ID: " << c.id() << '/n';     to << "NAME: " << c.name() << '/n';     to << "TEL: " << c.telephone() << '/n';     to << "AGE: " << c.age() << '/n';     to.flush();     return to; }  int main(int argc, const char** argv) {     Contact contact = {1, "Michael", "555-555-5555", 15};     std::cout << contact << std::endl;      return 0; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: