Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

function problem

Posted on 2003-11-26
7
268 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ getting the first 10 characters of a char* string 11 98
gdb doesn't stop on breakpoint 2 92
Why isn't object file created? 6 68
How do i run a c++ file? 15 46
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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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