Solved

function problem

Posted on 2003-11-26
7
246 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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

20 Experts available now in Live!

Get 1:1 Help Now