Solved

function problem

Posted on 2003-11-26
7
254 Views
Last Modified: 2010-04-02
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;
}
0
Comment
Question by:redice
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 9825131
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
 
LVL 13

Expert Comment

by:SteH
ID: 9825134
Have you tried compiling it?
What type is Assign? To overload operator+ I would suggest something like:
homeWork& operator+ (const homeWork& toadd);
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9825223
(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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:redice
ID: 9825235
Thanks for the comments. I will give them a try.
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9825270
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
 
LVL 9

Expert Comment

by:tinchos
ID: 10286112
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now