Solved

function problem

Posted on 2003-11-26
7
263 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 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