C++ :: Extra Parameter Passing To Overloaded Binary Operator Function

Jun 11, 2013

I have a class matrixType that has some overloaded operators (+, -, *, and <<). With a view to having clearly-delineated, perfectly-formatted, four-sided matrices, as shown below:

A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9

and NOT this jagged ones shown below:

A = 1 2 3
4 5 6
7 8 9

or

A + B = 1 2 3
4 5 6
7 8 9
,

I want a scheme in which the string literals (A, A+B, etc.) could be passed as parameters to the overloaded stream insertion (<<) operator function so that I could use the string’s length to determine how much offset from the display screen’s left to apply to each matrix’s row (by using the setw() function). However, I do know that the << operator is a binary operator, meaning the function cannot take more than two parameters: that is what compounds my problem!

View 10 Replies


ADVERTISEMENT

C++ :: Passing Extra Parameter To Operator Function?

Jul 29, 2013

I have a class and I would like to be able to pass an extra parameter to the function that is executed.

BigInt operator / (BigInt N,BigInt D) {
...
}

is what I have now. but I would like to do something like this. so the default value for a is 10. and if the user does something like N/D (12) Then the value of a is 12.

BigInt operator / (BigInt N,BigInt D, int a=10) {
...
}

View 2 Replies View Related

C++ :: Why Can't Operator Be Overloaded As A Member Function

Apr 3, 2013

why can't << operator be overloaded as a member function is it because that is the way c++ is written and you just can't or is there another reason because I'm confused.

View 2 Replies View Related

C++ :: Overloaded Operator Function In Main

Sep 17, 2013

I don't exactly know how to test my ==friend function in my main.

Here is my .h file:

#include<iostream>
using namespace std;
class Car{
public:
Car();
Car(int yer, string mke);

[Code] ....

View 3 Replies View Related

C++ :: Passing Function As Parameter?

Jan 7, 2015

gcc v.8.3 -std=gnu++11

[URL]

I'm trying to pass a function as a parameter and failing. It seems simple, until I get the error messages.

Here is the code:

class MinimalSolver {
typedef double (*func)(double sum, double char);
void driver();

[Code]....

View 2 Replies View Related

C :: Passing Array As A Parameter To Function

Oct 6, 2014

How come when we pass an array as a parameter to a function, the entire array is copied and is available to function?

View 1 Replies View Related

C++ :: Passing Array Into Function As Parameter

May 2, 2013

Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. By removing, I mean that you should make it appear as if the elements hadn't been there. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.

Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10 The array contains: 1 2 3 4 5 6 7 8 9 10

Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9 The array contains: 1 3 6 7 8 9

Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1 The array contains: 1

The bolded area is where I'm having trouble. How I can go about doing this, passing an array into the function as a parameter?

Here is my code:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
const int MAX = 10;
int a[MAX] = {0};
int i;

[Code]...

View 5 Replies View Related

C++ :: Passing A Function As Template Parameter

Dec 26, 2013

Pseudocode:
template<typename T /*, some parameter for member_function */>
class Foo {
public:
void someFunction() {
T t;
t.member_fuction(...);
} }

I'm trying to make the call to T::member_function a templated value because member_function might vary by name in my scenario. Since std::mem_fn isn't a 'type', i can't do something like Foo<std::string, std::mem_fn(&std::string::clear)> foo;

I also want to take into account that member_function might have more than one parameter. That is, the first parameter will always be something known but there might be other defaulted parameters.

The only thing I can think of is to make a proxy structure, something like this:

template<typename T, T> struct proxy;
template<typename T, typename R, typename... Args, R (T::*member_function)(Args...)>
struct proxy<R (T::*)(Args...), member_function> {
R operator()(T &obj, Args.. args) {
return (obj.*member_function)(std::forward<Args>(args)...);
} }

Which would then allow me to do (for example) this:

Foo<std::string, proxy<void(std::string::*)(), &std::string::clear> >

when Foo is implemented like this:

template<typename T, typename member_function>
class Foo {
public:
void someFunction() {
T t;
member_function()(t);
} };

That implementation works for me.

View 10 Replies View Related

C++ :: Passing A Function Parameter By Reference?

Sep 25, 2012

I created the following code to pass the the variable 'inputVoltage' by reference to the function 'input'. It certainly works when I run the program, but I dont think it is a standard way of doing it, i.e. the use of '*' and '&' is not according to convention ? Or perhaps the way did it is acceptable ?

int input (double *inputVoltage);
int main ( {
double inputVoltage;
input(&inputVoltage);

[Code]....

View 2 Replies View Related

C++ :: Extra Parameter In Call To Factorial

Mar 2, 2013

While running it gives the runtime error: "Extra parameter in call to factorial."

#include<iostream.h>
#include<conio.h>
int factorial(long int);
void main() {
clrscr();
long int a;
cout<<"This program displays the factorial of the number you enter."<<endl;

[Code] .....

View 15 Replies View Related

C++ :: Template Function Parameter Passing By Reference Instead Of Copy / Pointer

Sep 19, 2014

Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.

You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.

#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}

[code]....

In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.

View 3 Replies View Related

C++ :: Enter A String Up To 99 Characters - Extra Parameter In Call To Count

Feb 3, 2013

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void count();
void main() {
char c[100];
cout<<"Enter a string upto 99 characters(spaces included):"<<endl;

[Code] ....

The thing is, when I don't create a prototype of count and straightaway define it before void main() my program runs, but it gives error in this program that "extra parameter in call to count()" But what to do then?

View 2 Replies View Related

C++ :: Type Conversions In Overloaded Operator

Dec 2, 2014

What is another way I could convert string to int in this overloaded operator? This way gives me an error.

Code:
istream &operator>>(istream& in, MasterData& d) {
string value;
getline(in, d.playerId, ',');
getline(in, d.firstName, ',');
getline(in, d.lastName, ',');

[Code] .....

View 4 Replies View Related

C++ :: Using Ternary Operator With Overloaded Constructor

Jun 27, 2014

I have two possible questions; can you use a ternary operator to initialize objects with overloaded constructors like

class thing
{
int x;
int y;

[Code].....

I can get around it if I need to but I'd like to learn more about the ternary operator if I can, since I couldn't find anything online that addressed this particular issue, at least in a way I could detect.

View 4 Replies View Related

C/C++ :: Overloaded Operator Killing A Pointer?

Mar 29, 2015

I'm using some overloaded operators (addition, subtraction and variants of) in part of my final major project and, when coming to test it, I've noted that they appear to be killing my pointers eventually.

I say pointers, it's always the same one. But I have isolated it to being the operators. The only two I'm really using are += and -=, though I've defined the others for consistency.

Either A ) what it is I've done wrong (if I have) or B ) why I would see this behaviour. Or, you know, if there's something glaringly obviously wrong with the code that I'm glossing over.

Code is as follows

#pragma once
#include "stdafx.h"
namespace gunpei {
/**
A paired register in the form of r1r2
Enables using two separate arrays for register processing
provides logic for assembling pair and breaking back into individual registers
*/
class GBPairedRegister {

[code]....

View 4 Replies View Related

C/C++ :: Binary Operator Overloading Using Member Function

Dec 26, 2014

I want to create a program that shows the total of 2 subjects & percentage of student using binary operator overloading with member function in C++.

View 10 Replies View Related

C++ ::  assigning To A Member From Struct Obtained By Overloaded Operator

Jan 27, 2014

I'm trying to assign a value to a member of a struct that I called via an overloaded [] operator. I have the following code for the struct:

typedef struct {
float r, g, b, a;
float operator [](int pos) {
switch (pos) {

[Code] ....

And what I wish to do is

MyStruct a;
a[0] = 0.5;

Is it possible with a struct? How to express this to search engines so I haven't been able to find anything about it. If this is not possible with a struct, is there a way to define something that can do all the following things:

SomeStruct test = {0.5, 0.5, 0.5, 1};
test.g = 1.0;
test[0] = 0.0; // test[0] would be equivalent to calling test.r
float somevalue = test[3]; // test[3] would be equivalent to calling test.a

I hope I've been sufficiently clear.

View 2 Replies View Related

C++ ::  Overloaded Assignment Operator For Class Template Objects?

May 23, 2013

I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.

Here is the class definition:

template <class dataType>
class myArray {
public:
void setArrayData();

[code]....

And here is the definition of the overloaded assignment operator:

template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) {
int i;
if(this != &rightArray) {
delete [] arrayPtr;

[Code] ....

And here is my main function that tests the operations on objects of the class:

int main(){
//object declarations
myArray<double> list(5); //a single-parameter object declaration of class myArray
myArray<double> myList(2,13); //a two-parameter object declaration of class myArray

[code]....

The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!

View 19 Replies View Related

Visual C++ :: Pass More Parameter In Overriding New Operator

Jul 24, 2014

[URL]

class CMyclass
{
public:
CMyClass(BOOL bUseWhichMemManager);
void* operator new(size_t);
void operator delete(void*);
};

I create two memory manager called CMemManager1 and CMemMangaer2, using different algorithms to allocate buffer. Now I want to control which memory manager to be used, when calling new.

I try to add a parameter bUseWhichMemManager to the constructor, but in overrided new function, there are no way to access the parameter. Is there a way to pass more parameters to new operator, such as:

void* operator new(size_t size, BOOL bUseWhichManager);

View 1 Replies View Related

C++ :: Function Parameter Scope - NumArray Not Recognized As Valid Parameter

Sep 28, 2014

My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.

#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;

/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);

[Code] ....

View 1 Replies View Related

C++ :: Overloaded Operator Defined In Header File - Gives Error In CPP File Of Class

Apr 12, 2014

I am working on an assignment in which i have to perform th following task

myClass itsObject1,itsObject2;
itsObject2=5000+itsObject1;

I have defined overloaded operator as follows in the header file but in the cpp file of the class it gives error.

friend vli &vli::operator + (int &a,vli &obj);

How to define it in cpp file of my class?

View 1 Replies View Related

CLI :: Unable To Read CPP Parameter Definition Using Bitwise Operator

Jul 16, 2014

In a .h file there is a function that takes in this parameter:

void (^callback)(float * arg)=NULL

as in a function definition:

void func(void (^callback)(float * arg)=NULL);

What I am able to read is that it takes a function pointer and if not defined it overrides with NULL. The part I do not get is the ^ in (^callback). I only know ^ as a bitwise XOR operator. It also generates issues in my VS2012 compiler (something with CLR). So I would really like to rewrite this part to something else, without the bitwise operator...

View 2 Replies View Related

C :: Passing Filename As A Parameter

Aug 23, 2013

Code:
#include <stdio.h>
void ASCII_to_EBCDIC( size_t, unsigned char *);
void EBCDIC_to_ASCII( size_t, unsigned char *);
void to_ASCII(unsigned char *);
void to_EBCDIC(unsigned char *);
/* conversion tables */
static unsigned char

[Code] ....

The above snippet is for a buffer/string, where as i want to pass file name as a parameter and want function to process the file line by line?

View 2 Replies View Related

C++ :: Passing Node As Parameter

May 1, 2013

#include <cstdlib>
#include <iostream>
struct tax_node {
char form; // tax form letter
int version; // tax form number

[Code] ....

I cannot seem to get why function print_contents will not work. The couts at the end of the program is just to test that it printed correctly. But, if I need it to print the contents such when print_contents(ptr2) is called. I think it should be tax_ptr in the parameter list but I am not quite sure.

View 1 Replies View Related

C# :: Passing Event As A Parameter

Nov 16, 2014

I want to create events and then, functions which are subscribed to the event can access information about the event. For example, in Class 2 below, I want it to be able to access things such as touch.position, etc. of class 1.

Class 1:

public delegate void TouchEventHandler (EventArgs e);
public event TouchEventHandler TouchBegan;
public Vector2 touchPosition;
void Update () {
if (Input.touchCount > 0) {

[Code] ...

Class 2:

void OnTouchBegan (EventArgs e) {
Debug.Log ("Ran");
}

View 5 Replies View Related

C++ :: Passing Lambdas As Template Parameter

Oct 6, 2013

I've been playing around with this piece of code:

#include <iostream>
#include <string>
#include <algorithm>
template <void(*funky)(const std::string&)>
void callback()
{funky("Hello World!");}

[Code] ....

But when I try to build it, I get this error on line 24:could not convert template argument 'lambda' to 'void (*)(const string&) {aka void (*)(const std::basic_string<char>&)}'|

I thought the lambda expression I wrote would decay to a function pointer matching the template parameter. I can guess that the constexpr qualifier might have changed the type, but without it my compiler complains that lambda needs to be declared as constexpr...

So is there a way to pass lambda expressions as template parameters?

Without having to use std::function

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved