Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to output Classes and Objects

#include <iostream>

using namespace std;

class PrintName{
public:
    void studentName(){
        cout<<"Name       : "<<studentName<<endl;
    }
};

class MathClass{
public:
    void multiplicationFunc(int x, int y){
        cout<<"Result     : "<<(x*y)<<endl;
    }
};

int main()
{
    cout<<endl;
    PrintName PN;
    PN.studentName("Mark", "Santo");

    MathClass MC;
    MC.multiplicationFunc(10,5);
}

I am new to C++ and am learning about classes and objects. From what I gathered classes are ways to group functions and objects is the ability to access them? I am having trouble getting this code to work, I receive an error on line 15 for ‘error: no match for ‘operator<<‘. I am trying to fix my class in order for the main function to work. The output should simply be
‘Name : Mark Santo’
‘Result : 50’

Thank you for your help everyone!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It seems like there is no error on line 15. Instead, you did not define the variable studentName in your void studentName() function in line 8. Also, you used 2 arguments for studentName() in line 23, where the original function is not taking any. This is the corrected code:

#include <iostream>

using namespace std;

class PrintName{
public:
    void studentName(string x, string y){
        cout<<"Name: " << x << " " << y << endl;
    }
};

class MathClass{
public:
    void multiplicationFunc(int x, int y){
        cout<<"Result: " << x*y << endl;
    }
};

int main()
{
    cout << endl;
    PrintName PN;
    PN.studentName("Mark", "Santo");

    MathClass MC;
    MC.multiplicationFunc(10,5);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading