function problem

I need to overload the operator+ and create a function template that adds two values and returns their sum.  What am I missing?


#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <string.h>

template <class T>
T addroutine(T x, T y)
{
      T result;
      result = x + y;
      return(result);
            cout<< "Result = "<<result<<endl;
};

class homeWork
{
private:
      char classname[15];
    int minutes;
    char assignment;
      
public:

      homeWork(char num[], int min, char code);
      void showCall();
      homeWork operator+(Assign assign)
};

Assign assign:operator+(Assign assign)
{
      Assign newassign;
      strcpy(newassign, "Assigment Summary");
      newassign.classname = classname + newassign.classname;
      return(newassign);
}


void main()
{
      int x = 60, y = 10;
      addroutine(x,y);

      cout<< "Result = "<<result<<endl;
}
rediceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
template <class T>
T addroutine(T x, T y)
{
    T result;
    result = x + y;
    return(result);

    // cout<< "Result = "<<result<<endl;  this line never executes
};

void main()
{
    int x = 60, y = 10, result;
    result = addroutine<int>(x,y);

    cout<< "Result = "<<result<<endl;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteHCommented:
Have you tried compiling it?
What type is Assign? To overload operator+ I would suggest something like:
homeWork& operator+ (const homeWork& toadd);
0
rstaveleyCommented:
(1) You need to define your class Assign
(2) The cout in your addRoutine will not show because it is out of scope; you have the return before it.
(3) You are missing a semicolon after homeWork operator+(Assign assign)
(4) You are not assigning the value returned from addroutine(x,y) to a variable.
(5) cout in main is attempting to display an undeclared variable result
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

rediceAuthor Commented:
Thanks for the comments. I will give them a try.
0
dhyaneshCommented:
Hi

There are quite a few errors in your code.

First of all there is no class called 'Assign'. You have only one class 'homeWork'. So your function 'assign' (with a small 'a') should have declaration as:

homeWork operator+(homeWork assign)

In function definition it should be:

homeWork assign:operator+(homeWork assign)

Also your function is not correct. classname is an array of characters. It cannot be added directly as you have done.

It should be something like this:

homeWork assign:operator+(homeWork assign)
{
    homeWork newassign;
    strcpy(newassign.classname,classname);                       //Copy current classname to newassign.classname
    strcat(newassign.classname,assign.classname);           // Add the assign.classname at end of above classname
    return(newassign);
}

You will also have to create contructors for the class which will initialize values.

Also some sort of display function will be required to test the function you created.

Also in main you have to create two objects to test this function

e.g.

homeWork a("First"),b("Second"),c;
c = a+b;
c.display();


Dhyanesh
0
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: AlexFM {http:#9825131}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.