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


ADVERTISEMENT

C++ :: Implicit Conversions To / From Library Type Only In Implementation

Apr 1, 2014

1. I designed two classes: Processor and Data.

2. Data represents some data object the user can pass to Processor's public methods.

3. Internally, Processor needs to use InternalData type which is based on the content of Data (I can use Data's public interface to get the required information from it, or construct a Data object using its public constructor when needed, and that's how I have done it so far).

4. To avoid repeating code and localize changes required when Data's interface would change someday, I made conversion functions from Data to InternalData and back inside Processor, as private methods.

Now here comes the kicker:

5. But I'd like these conversions to be implicit inside Processor's methods instead of explicit. And only there.

6. These conversion functions are only for Processor implementation's use. They shouldn't be visible nor accessible from the outside world.

Where the problem lays:

7. InternalData is a library type. I don't have control over it and I cannot modify its interface.

That is, I cannot just add converting constructors or conversion operator member functions to them.You can consider it to be built-in type if you wish.

8. I don't want to put those converters inside Data class either, since it's not its business and it shouldn't know that Processor converts it to something else internally.

Long story short, I'd like to teach the Processor's implementation how to make type conversions between Data and InternalData implicitly, but no one else except Processor should be affected by it. Outside world shouldn't be able to do these conversions or even know about them being done inside Processor's implementation.Is there any way to do it in C++?

The core of the problem seems to be the fact that in C++ defining implicit conversions is possible only from/to a user-defined type when defining this type. I don't know of any way to define such conversions for some other type's internal use only. (Especially when I don't have control about one of these converted types.)

View 6 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++ :: 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++ ::  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

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 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

C++ :: Return Type For Assignment Operator

Apr 7, 2014

I am wondering why return type for an assignment operator cant be a void or int? Cant I write assignment operator for student class like this as we do nothing with returned value?

Student {
char name[20];
int marks;
public:
student(char*name,int marks)

[code].....

View 2 Replies View Related

C/C++ :: Use Modulus Operator With Double Type Variables?

Mar 13, 2013

how can we use modulus operator with double type variables

View 2 Replies View Related

C++ :: No Operator Found Which Takes A Right-hand Operand Of Type Double

May 17, 2014

while (getline(inStream, line))
{
while (inStream >> Student.getId() >> Student.FNAME >> Student.MINIT >> Student.LNAME >> Student.GENDER >> Student.UNITS >> Student.getGpa())
{
while (Student.getId() != id)
{
outStream << line << endl;
}
}
}

This is what I have right now. It shouldn't be a problem, but for some reason I am getting an error trying to >> Student.getGpa()

Error1error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)c:location1301Project 5

I will post more code if needed, but... I just don't know. I have a TON of code so I would rather not if I don't have to.

View 2 Replies View Related

C++ :: Overloading Input Operator For Rational Type Objects To Work

Nov 13, 2014

I cannot get my function overloading the input operator for rational type objects to work.

lab9.cpp: In function ‘std::istream& operator>>(std::istream&, const rational&)’:
lab9.cpp:186:20: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from expression of type ‘rational’
return (inputFrac);

lab9.h

#include <iostream>
using namespace std;
class rational {
public:
rational();
rational(int a, int b);

[Code] ......

View 4 Replies View Related

C++ :: Threads Giving Error - Call Of Object Of A Class Type Without Appropriate Operator Or Conversion

Jan 27, 2015

I made a simple binary tree then decide to try out threads too. I got the following error:

call of an object of a class type without appropriate operator or conversion

Code:
#include "Tree.h"
#include <iostream>
#include <thread>
void main(void){

[Code] ....

I am having a hard time figuring out why the error exists. I tried adding the new operator but that did not work.

View 11 Replies View Related

Visual C++ :: Operator Overload Not Defined Error When Type Accessed Through Const Struct

Oct 17, 2012

I have a basic vector/point class where I've overloaded a bunch of arithmetical operators:

Code:
#pragma once
class point {
public:
point() {
}
point(float p_x, float p_y, float p_z) : x(p_x), y(p_y), z(p_z)

[Code] ...

I can use it fine like

Code:
point p(50,50,50);
point q(50,50,50);
point t = p * q + q;

However when I put this point type into a struct and try to access the members after passing it through by const reference:

Code:
struct sextic {
point a,b,c,d,e,f,g;
};
inline static sextic sexticDifference(const sextic &p_sextic1, c

[Code] ....

This gives me an "operator not defined" error for compilation.

View 2 Replies View Related

C :: Integer / Floating Conversions

Apr 27, 2013

I got the following to compile and execute, but I don't understand how this works. Here is the code verbatim:

Code:
// Basic conversions in C
#include <stdio.h>
int main () {
float f1 = 123.125, f2;
int i1, i2 = -150;
char c = 'a';

[Code] .....

Okay, so first we have some variables declared as integers, floats, (and that char what it's doing). On the same lines we have values assigned to some of those variables. At first the "f2" and the "i1" confused me, but I think they're just variables whose type has been declared but have not received a value. So far I think I'm good. Then we get into the routines

I think​ I understand the first one. i1 didn't originally receive a value assigned to it, so when it says f1 = i1, then f1 (123.125) just becomes an integer, simple enough.

Then we get to the next routine and I'm like what dafuq??? So first we have f1 (123.125) being set to i2 (-150). wtf? So does that mean f1 is now going to have the value of -150? Vice versa? How are they becoming equal? Or does it have nothing to do with the values of the variables at all and just the type??? I'm just totally lost.

The output of that line is "-150 assigned to a float produces -150.000000." Which makes perfect sense to me written in plain English, but I don't understand how the C code works ....

View 4 Replies View Related

C++ ::  Conversions From Text Box To String

Jul 13, 2013

I use VC++ 2010 Express. I want to make windows form app. I want to make something like this

#include <fstream>
...
...
...
ofstream File(vardas->Text + " " + pavarde->Text + ".txt");

And I get this:

Form1.h(201): error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

I got those textboxes. What conversions do i have to do?

View 6 Replies View Related

C/C++ :: Number Conversions Using Bitwise Operators

Feb 13, 2014

I have a project assignment for school to write a program that does number conversions using bitwise operators. The premise is that the user enters a number with one of three letter prefixes -- Q1232, O6322, H762FA, etc. -- and the program will take that number and convert it to the other two number bases. Q is for quarternary, O is for octal, and H is for hexadecimal. The transformations should be done using bitwise operators and bit shifting. I am guessing I need to scan the number, convert it to binary, then convert it to the other two bases.

However, I am completely new to bitwise operators and bit shifting, so how to convert numbers of different bases to binary and then binary to other bases using these bit and bitwise functions. I don't have much code done yet, since I am still unsure of how to approach it, but I'll post what little I have.

Here it is:

#include <stdio.h>
#include <string.h>
int main() {
char numType;
printf("
The user will enter a number up to 32 digits in quarternary
");
printf("(base 4), octal (base 8), or hexadecimal (base 16). If in
");

[Code] ....

I figure in each case I can write a function that converts the entered number to binary, then maybe two more functions that convert said binary number to the other bases. For default in the switch I will tell the user they entered an invalid number. I don't have the program looping until the user types 'EXIT' yet, but I will once I figure out anything about these bitwise operators.

View 2 Replies View Related

C++ :: Conversions Between Integer Primitive Data Types

May 8, 2014

In my platform (Windows 7 Ultimate 64 bits with Service Pack 1 over a compatible PC with a AMD x86 microprocessor), the next sample C++ code,

#include <iostream>
#include <limits>
using std::cout;
using std::endl;
using std::hex;
using std::showbase;
using std::numeric_limits;

[Code] ....

Compiled with Microsoft Visual C++ 2010 Express, prints this output:

ui = 0xffffffff
ull = 0xffffffff
sll = 0xffffffff
si = 0xffffffff
ull = 0xffffffffffffffff
sll = 0xffffffffffffffff

So, in my platform, conversion from an unsigend integer primitive data type to any bigger integer primitive data type never extends the most significant bit of the former integer and conversion from an signed integer primitive data type to any bigger integer primitive data type always extends the most significant bit of the former integer. This is convenient to mantain the same value when converting between integer primitive data types of the same signedness (i.e, signed integers or unsigned integers).

View 4 Replies View Related

C++ :: Program That Create And Display Table Of Temperature Conversions

Feb 12, 2015

Write a program that creates and displays a table of temperature conversions. Get the starting temperature from the keyboard in degrees Celsius (do not allow input of a value below absolute zero). Also get an integer value to represent the number of degrees to increment for each of a 20 row table (do not allow the increment value to be less than one. The first column will be a row number starting with one, follow by the Celsius value and then the conversions into Fahrenheit, Kelvin, and Rankine. Be sure that all columns are neatly right aligned for a variety of inputs.

Thats what i wrote so far:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double C,F,K,R,n,a;
cout <<"Enter starting temperature in Celsius: ";
cin >> C;
[Code] ....

Thats what the instructor looking for:

Enter starting temperature in Celsius: -500
ERROR: Temp must be >= -273.15: -273.15
Enter increments in degrees Celsius: 100

# Cels Fahr Kelv Rank
1 -273.15 -459.67 0.00 0.00
2 -173.15 -279.67 100.00 180.00
3 -73.15 -99.67 200.00 360.00
4 26.85 80.33 300.00 540.00
5 126.85 260.33 400.00 720.00
6 226.85 440.33 500.00 900.00
7 326.85 620.33 600.00 1080.00
8 426.85 800.33 700.00 1260.00
9 526.85 980.33 800.00 1440.00
10 626.85 1160.33 900.00 1620.00
11 726.85 1340.33 1000.00 1800.00
12 826.85 1520.33 1100.00 1980.00
13 926.85 1700.33 1200.00 2160.00
14 1026.85 1880.33 1300.00 2340.00
15 1126.85 2060.33 1400.00 2520.00
16 1226.85 2240.33 1500.00 2700.00
17 1326.85 2420.33 1600.00 2880.00
18 1426.85 2600.33 1700.00 3060.00
19 1526.85 2780.33 1800.00 3240.00
20 1626.85 2960.33 1900.00 3420.00
Press any key to continue . . .

My question is to know how to make the sequence from 1 to 20 and whats the best statement for increment...

View 2 Replies View Related

Visual C++ :: Runtime Check Failure - Value Of ESP Error With Conversions

Dec 2, 2014

I get this error:

run-time check failure #0 - the value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention

when i try to run my code. It has compiled fine on another computer, but it simply will not work on this one. This is the part of code where it is receiving the error. it has to do with the stoi

Code:
#include <string> // for use of string
#include <fstream> //for file handling
#include <iostream> // for file handling
#include <cstdlib>
#include <iomanip> //for the setprecision used further below
using namespace std;
struct MasterData //struct created named 'MasterData' to hold one line from master file

[Code] .....

View 2 Replies View Related

C++ :: Namespaces / Classes - Perform Operator Overload With Insertion Operator

Mar 22, 2013

I'm doing a refresher for C++ and have gotten to operator overloading. I'm trying to perform an operator overload with the insertion (<<) operator, but I have encountered a problem.

Here's my class [In a header file "Shinigami.h"]

#include<string>
namespace K{
class Quincy;
class Shinigami{
friend std::ostream& operator<<(std::ostream&, const Shinigami&);

[Code] .....

If the operator function is a friend of the 'Shinigami' class, why doesn't it recognize any of it's private members? I need it to be in this file because I'm doing a bit of association with the 'Quincy' class.

I thought it was the namespace, but I included that.

View 9 Replies View Related

C++ :: Overloaded Constructor Of String

Sep 2, 2013

I have to implement the following class:

class MyString {
private:
char *str; // Pointer to the char array that holds the string
int strLength; // Variable to store the length of the string

public:
// Default constructor to initialize the string to empty string
MyString();

[Code] .....

How can I define overloaded constructor at line 12.

View 1 Replies View Related

C++ :: Getline Overloaded Error

Dec 5, 2014

I am currently having trouble to have getline to read line from the file. Error is: "no instance of overloaded function "getline" matches the argument list"

code is as follows:

std::ifstream config("config.txt");
string process[4];
int linecount = 1;
if (config.is_open)
{
while (config.peek() !=EOF)
{
getline(config, process);
linecount++;
}
}

View 5 Replies View Related

C++ :: Templates / Lambdas And Overloaded Operators

Feb 16, 2015

What I'm trying to do is create a class for constructing an 'op tree' for parsing infix notation.

I started with a base class that uses a map of lambdas to actually calculate the operations (since they are mostly 1 line functions) of passed in integer or float values.

This base class just uses a templated T type as the lvalue and rvalue. I realized though that if I overload the math operators, +, -, etc.. I could also use the class itself as a type for the lvalue and rvalue. This lead me to think I could easily create the op tree by using Operation class members themselves as operands, which I think makes sense but I'm having some trouble expressing the code.

Heres what I have thus far

Code:
#include <map>
#include <string>
#include <algorithm>
#include <iostream>

namespace Calc {

[Code] .....

Example, if you look at the main() function I create normal operations easily with integer values. I then try to create a "tree" operation that includes 2 sub-operations as it's rvalue and lvalue, that is where I'm having some conceptual problems as far as implementing the code to do that.

View 2 Replies View Related







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